尝试从ASP.NET页面将值插入数据库

时间:2015-05-03 20:49:42

标签: c# asp.net sql-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();
}

3 个答案:

答案 0 :(得分:1)

您获得空值的原因是您应该使用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注入。

请查看:https://stackoverflow.com/a/9162904/461810

答案 1 :(得分:1)

您应该通过Text Property访问TextBoxes的值:

string flightDate = flightDate.Text;
string flightTime = flightTime.Text;
string flightNum = flightNum.Text;
string flightDest = flightDest.Text;

但仍有改进的余地: 对于日期,您应该使用Calendar控件,因为目标字段可以很好地从ComboBox控件等中选择。

答案 2 :(得分:0)

这是最好的:

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