如何使用外键编写插入代码?

时间:2015-05-22 15:46:55

标签: c# asp.net

我的网站上有一个Q和A页面,显示从我的数据库中的问题表中提取的问题。问题下面是两个文本框 - 一个用于人名,另一个用于答案,将被插入答案表。答案表上有一个FK questionID,这就是我被困住的地方。如何在我的代码中编写答案表的脚本,以便它使用问题表中的那个问题ID?

这是我到目前为止所做的:

protected void btnSubmitAnswer_onClick(object sender, EventArgs e)
{
     String connectionString = "Server=root;Database=test;User=name;Password=test;";

     using (SqlConnection conn = new SqlConnection(connectionString))
     {
          SqlCommand cmd = new SqlCommand("INSERT INTO Answers (QuestionID, Answer_Name, Answer)" +
                   "VALUES (%%, '" + txtName.Text + "', '" + txtAnswer.Text + "')");
          cmd.Connection = conn;
          conn.Open();
          cmd.ExecuteScalar().ToString();
     }   

}

%%是我需要修复的。我正在考虑使用String变量,但后来我仍然不知道该数据类型应该使用什么。

6 个答案:

答案 0 :(得分:0)

您需要从问题表中执行get Query(Select Statement)以检索Question Id,然后执行插入操作。

另外,使用参数字符串,不要连接,你的代码可能会被注入恶意行为,可能会删除你的数据库表!

答案 1 :(得分:0)

您需要将问题的ID存储在隐藏字段中,并在insert语句中使用内部选择来引用问题的ID。

隐藏字段的设置如下:

<input type="hidden" id="questionId" value="questionId" />

然后:

SqlCommand cmd = new SqlCommand("INSERT INTO Answers (QuestionID, Answer_Name, Answer)" +
           "VALUES (questionId.Value, '" + txtName.Text + "', '" + txtAnswer.Text + "')");

内部选择将采用

的形式
insert into table ((select id from questiontable where question = [the question text]), 'name', 'answer')

答案 2 :(得分:0)

您需要创建一个int QuestionID变量来保存当前QuestionID。只需在用户更改问题时进行更改即可。您可以将其存储在Session

附注:我建议参数化txtName.TexttxtAnswer.Text以避免SQL注入。

答案 3 :(得分:0)

当您收到数据库的问题时,也要获取Id并将其存储在某处。

另一方面,您的代码对SQL注入是开放的。使用参数来存储文本框中的值,如下所示:

SqlParameter param = new SqlParameter("@Name", SqlDbType.VarChar);
param.Value = txtName.Text;
cmd.Parameters.Add(param);

答案 4 :(得分:0)

所以你的控件是 Gridview ,可能就是这种情况..尝试一下。

&#13;
&#13;
protected void myRowCommand(object sender, GridViewCommandEventArgs e) 
{
 String connectionString = "Server=root;Database=test;User=name;Password=test;";
  
 GridViewRow row = this.GridView1.SelectedRow; 
            int EntryID = row.RowIndex;
     using (SqlConnection conn = new SqlConnection(connectionString))
     {
          SqlCommand cmd = new SqlCommand("INSERT INTO Answers (QuestionID, Answer_Name, Answer)" +
                   "VALUES (EntryID, '" + txtName.Text + "', '" + txtAnswer.Text + "')");
          cmd.Connection = conn;
          conn.Open();
          cmd.ExecuteScalar().ToString();
     }

  
  }
}
&#13;
&#13;
&#13;

答案 5 :(得分:0)

当您获取问题ID时,您应该将其存储到变量中,以便在插入命令时重复使用它。

像:

SELECT Id, /*And anything else you need*/ FROM QUESTION

并将其存储在Int32 QuestionId;

之类的变量中

另外,为了完整起见,永远不要连接SQL字符串,它会使你的代码容易受到攻击,而是使用sql参数。

protected void btnSubmitAnswer_onClick(object sender, EventArgs e)
{
 String connectionString = "Server=root;Database=test;User=name;Password=test;";

 using (SqlConnection conn = new SqlConnection(connectionString))
 {
      SqlCommand cmd = new SqlCommand(
           "INSERT INTO Answers (QuestionID, Answer_Name, Answer)" +
           "VALUES (@prmQuestionId, @prmAnswerName, @prmAnswer)");
      cmd.Parameters.Add(new SqlParameter("@prmQuestionId", QuestionId/*HERE INSERT THE ID VALUE OF THE FETCHED QUESTION*/));
      cmd.Parameters.Add(new SqlParameter("@prmAnswerName", txtName.Text));
      cmd.Parameters.Add(new SqlParameter("@prmAnswer", txtAnswer.Text));
      cmd.Connection = conn;
      conn.Open();
      cmd.ExecuteScalar().ToString();
 }   
}
相关问题