C# 尝试从ASP.NET页向数据库中插入值

C# 尝试从ASP.NET页向数据库中插入值,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我说得很清楚,我正试图通过C#将值插入数据库,但它无法使用我的代码。没有必要把我的问题搁置。我想这可能是一个初学者可以找到帮助的地方,而不是被人看不起 我试图通过asp.net和C#将值插入数据库。我有一个基本的调查表格,里面有文本框和单选按钮。当我运行代码来插入值时,单选按钮值可以很好地插入。我对文本框的值有问题 ASP.NET标记: Date of Flight&nbsp;<asp:TextBox ID="flightdate" runat="server" /><

我说得很清楚,我正试图通过C#将值插入数据库,但它无法使用我的代码。没有必要把我的问题搁置。我想这可能是一个初学者可以找到帮助的地方,而不是被人看不起

我试图通过asp.net和C#将值插入数据库。我有一个基本的调查表格,里面有文本框和单选按钮。当我运行代码来插入值时,单选按钮值可以很好地插入。我对文本框的值有问题

ASP.NET标记:

Date of Flight&nbsp;<asp:TextBox ID="flightdate" runat="server" /><br />
Time of Flight&nbsp;<asp:TextBox ID="flightTime" runat="server" /><br />
Flight Number&nbsp;<asp:TextBox ID="flightNum" runat="server" /><br />
Flight Destination&nbsp;<asp:TextBox ID="flightDest" runat="server" /><br />

<strong>Friendliness of customer staff: </strong>
<asp:RadioButtonList ID="question1" runat="server">
<asp:ListItem Value="No Opinion"                                                                    Selected="True">No Opinion</asp:ListItem>
<asp:ListItem Value="Poor">Poor</asp:ListItem>
<asp:ListItem Value="Fair">Fair</asp:ListItem>
<asp:ListItem Value="Good">Good</asp:ListItem>
<asp:ListItem Value="Excellent">Excellent</asp:ListItem>
</asp:RadioButtonList>
航班日期
飞行时间
航班号
航班目的地
客户员工的友好程度: 没有意见 贫穷的 公平的 好的 杰出的
以及背后的C#代码:

  string flightDate = Request.QueryString["flightDate"];
  string flightTime = Request.QueryString["flightTime"];
  string flightNum = Request.QueryString["flightNum"];
  string flightDest = Request.QueryString["flightDest"];
  string selectedFriend = "";
  string selectedSpace = "";
  string selectedComfort = "";
  string selectedCleanliness = "";
  string selectedNoise = "";

  for (int i = 0; i < question1.Items.Count; ++i)
  {
      if (question1.Items[i].Selected)
      {
               selectedFriend = question1.Items[i].Value;
      }
  }

  SqlConnection dbConnection = new SqlConnection("Data Source=IDEA-PC\\SQLEXPRESS; Integrated Security=True");

  try
  {
      dbConnection.Open();
      dbConnection.ChangeDatabase("airlineSurvey");

      string results = "INSERT INTO results(flightDate, flightTime, flightNumber, flightDestination, friendliness) " + "VALUES('" + flightDate + "', '" + flightTime + "', '" + flightNum + "', '" + flightDest + "', '" + selectedFriend + "')";

      SqlCommand sqlCommand = new SqlCommand(results, dbConnection);
      sqlCommand.ExecuteNonQuery();

      regMessage.Text = "<p>Thank you for your feedback!</p>";
  }
  catch (SqlException exception)
  {
      Response.Write("<p>Error code " + exception.Number + ": " + exception.Message + "</p>");
    }

    dbConnection.Close();
}
string flightDate=Request.QueryString[“flightDate”];
string flightTime=Request.QueryString[“flightTime”];
字符串flightNum=Request.QueryString[“flightNum”];
字符串flightDest=Request.QueryString[“flightDest”];
字符串selectedFriend=“”;
字符串selectedSpace=“”;
字符串selectedComfort=“”;
字符串selectedCleansity=“”;
字符串selectedNoise=“”;
对于(int i=0;i”;
}
捕获(SqlException异常)
{
响应。写入(“错误代码”+异常。编号+”:“+异常。消息+”

”; } dbConnection.Close(); }
获取空值的原因是您应该使用
Request.Form
而不是
Request.QueryString

但是,由于文本框已经是服务器端控件, 然后您甚至不需要使用
Request.Form
,因为您可以简单地使用:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;
另外,您构建
SQL
查询的方式非常糟糕—您正在启用SQL注入


请查看:

您应该通过文本属性访问文本框的值:

string flightDate = flightDate.Text;
string flightTime = flightTime.Text;
string flightNum = flightNum.Text;
string flightDest = flightDest.Text;
但仍有改进的余地: 对于日期,您应该使用日历控件,对于目的地字段,最好从组合框控件中选择,等等。

这是最有效的:

var flightDate = this.flightdate.Text;
var flightTime = this.flightTime.Text;
var flightNum = this.flightNum.Text;
var flightDest = this.flightDest.Text;

-您不应该将SQL语句连接在一起-而是使用参数化查询来避免SQL注入文本框发生了什么?你并不是真的说问题出在哪里。初学者可以在这里找到帮助,但是他们应该在问一些不好的问题之前阅读,这些问题将被搁置(直到你纠正问题)。然后这个问题就可以重新讨论了。你从来没有说过你对文本框有什么“问题”。请注意,尽管你没有提出一个好问题,但你得到了很好的答案。我说谢谢。这很有效,谢谢。我意识到编码是不安全的。我刚刚开始学习C语言#