多次使用相同变量插入语句

时间:2015-03-27 14:57:49

标签: c# sql-server

需要为数据库中的多个类分配一些浮动教师。此声明与Error of the variable name @Teacher has already been declared. Variable names must be unique wihtin a query batch or stored procedure

错误

这是我的语法。我应该改变什么来实现这一目标:

private void btnOne_Click()
{
string insertstatement = "INSERT INTO tbl_TeacherInfo (Teacher,Grade) VALUES (@Teacher, @Grade)";

using (SqlConnection conn = new SqlConnection(connString))
{
    using (SqlCommand comm1 = new SqlCommand(insertstatement, conn))
    {
        conn.Open();
        comm1.CommandText = insertstatement;
        comm1.Parameters.AddWithValue("@Teacher", "Mrs Hart");
        comm1.Parameters.AddWithValue("@Grade", "1st");
        comm1.Parameters.AddWithValue("@Teacher", "Mrs Hart");
        comm1.Parameters.AddWithValue("@Grade", "2nd");
        comm1.Parameters.AddWithValue("@Teacher", "Mrs Hart");
        comm1.Parameters.AddWithValue("@Grade", "3rd");
        comm1.Parameters.AddWithValue("@Teacher", "Mrs Hart");
        comm1.Parameters.AddWithValue("@Grade", "4th");
        comm1.Parameters.AddWithValue("@Teacher", "Mrs Hart");
        comm1.Parameters.AddWithValue("@Grade", "5th");
        comm1.ExecuteNonQuery();
        conn.Close();
    }
}
}

2 个答案:

答案 0 :(得分:5)

var teacher = "Mrs Hart";
var grades = new[]{ "1st", "2nd", "3rd", "4th", "5th" };
var sql = "INSERT INTO tbl_TeacherInfo (Teacher,Grade) "
        + "VALUES (@Teacher, @Grade)";

using (var conn = new SqlConnection(/* connectionString */))
{
  conn.Open(); // Open once and share it

  // share the command
  using (var cmd = new SqlCommand(sql, conn))
  {
    // assign the shared value across all queries, and
    // add a placeholder for the revolving parameter
    cmd.Parameters.AddWithValue("@Teacher", teacher);
    cmd.Parameters.AddWithValue("@Grade", String.Empty); // placeholder

    // iterate over the grades (1st, 2nd, etc.)
    foreach (var grade in grades)
    {
      cmd.Parameters["@Grade"].Value = grade; // change @Grade
      cmd.ExecuteNonQuery(); // Execute with these two values
    }
  }

  conn.Close(); // cleanup
}

答案 1 :(得分:3)

您的错误是多次分配具有相同名称(@Teacher@Grade)的参数。我建议创建一个单独的方法,接受两个参数(teachergrade)并将一条记录插入tbl_TeacherInfo,如下所示

private void InsertRecord(string teacher, string grade)
{
    string insertstatement = "INSERT INTO tbl_TeacherInfo (Teacher,Grade) VALUES (@Teacher, @Grade)";

    using (SqlConnection conn = new SqlConnection(connString))
    {
        using (SqlCommand comm1 = new SqlCommand(insertstatement, conn))
        {
            conn.Open();
            comm1.Parameters.AddWithValue("@Teacher", teacher);
            comm1.Parameters.AddWithValue("@Grade", grade);
            comm1.ExecuteNonQuery();
            conn.Close();
        }
    }
}

然后在btnOne_Click方法

中调用上述方法
private void btnOne_Click()
{
    InsertRecord("Mrs Hart", "1st");
    InsertRecord("Mrs Hart", "2nd");
    InsertRecord("Mrs Hart", "3rd");
    InsertRecord("Mrs Hart", "4th");
    InsertRecord("Mrs Hart", "5th");
}