在SQL表中插入多行时出错

时间:2012-09-08 12:42:41

标签: c# sql-server ado.net sqlcommand

我正在尝试将多个记录插入SQL数据库。 SQL表包含smalldatetime列。每个记录都是一个小时的时间段,所以当你想插入时间跨度时,例如。 5个小时,你将5条记录填入数据库。

代码看起来像这样:

int flag = 0;
try{
   if (String.Empty.Equals(tboxFromTime.Text)){
      MessageBox.Show("Error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
      return;
   }
   DateTime n, m;
   if (!DateTime.TryParse(tboxFromTime.Text, out n)){
      MessageBox.Show("Error!");
      return;
   }
   FormMain.connection.Open();
   command = new SqlCommand();
   command.Connection = FormMain.connection;
   string classParam, personParam, startParam;
   classParam = "class";
   personParam = "person";
   startParam = "start";

   command.CommandText = "INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start)";
   command.Parameters.AddWithValue("@class", int.Parse(cboxRoomID.Text.Substring(4)));
   command.Parameters.AddWithValue("@person", int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));
   command.Parameters.AddWithValue("@start", DateTime.Parse(cboxWeekDate.Text + " " + n.ToShortTimeString() + ":00"));
   if (!String.Empty.Equals(tboxToTime.Text)){
      if (!DateTime.TryParse(tboxToTime.Text, out m)){
         MessageBox.Show("Error!");
         return;
      }
      else if(DateTime.Compare(m.AddHours(-1), n) > 0){
             TimeSpan timeDiff = m - n;
             int intDiff = timeDiff.Hours;
             for (int i = 1; i < intDiff; i++){
                 classParam = "class" + i.ToString();
                 personParam = "person" + i.ToString();
                 startParam = "start" + i.ToString();
                 command.CommandText += ", " + String.Format("(@{0}, @{1}, @{2})", classParam, personParam, startParam);

                 command.Parameters.AddWithValue(String.Format("@{0}", classParam), int.Parse(cboxRoomID.Text.Substring(4)));
                 command.Parameters.AddWithValue(String.Format("@{0}", personParam), int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));
                 command.Parameters.AddWithValue(String.Format("@{0}", startParam), DateTime.Parse(cboxWeekDate.Text + " " + n.AddHours(i).ToShortTimeString() + ":00"));
             }
         }
     }

   flag = command.ExecuteNonQuery();
   if (flag == 1) { MessageBox.Show("Uspelo!"); }
   }

当执行一小时时,它一切顺利,但在任何时间段它都会破裂。命令文本字符串几个小时是这样的:

INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start), (@class1, @person1, @start1), (@class2, @person2, @start2), (@class3, @person3, @start3)

但是,我再次收到此类错误消息:

','附近的语法不正确。

任何人都可以找出问题所在吗?

2 个答案:

答案 0 :(得分:2)

创建的sql语法不正确。您必须为自己插入每个值。

INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class, @person, @start)
INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class1, @person1, @start1)
INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class2, @person2, @start2)
INSERT INTO Reservations (resClassID, resPersonID, resStart) VALUES (@class3, @person3, @start3)

或者

INSERT INTO Reservations
SELECT @class, @person, @start
UNION
SELECT @class1, @person1, @start1
UNION
SELECT @class2, @person2, @start2
UNION
SELECT @class3, @person3, @start3

答案 1 :(得分:1)

我认为你不需要这一行:

 command.CommandText += ", " + String.Format("(@{0}, @{1}, @{2})", classParam, personParam, startParam); 

以及

command.Parameters.AddWithValue(String.Format("@{0}", classParam), int.Parse(cboxRoomID.Text.Substring(4)));                   
command.Parameters.AddWithValue(String.Format("@{0}", personParam), int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));       
command.Parameters.AddWithValue(String.Format("@{0}", startParam), DateTime.Parse(cboxWeekDate.Text + " " + n.AddHours(i).ToShortTimeString() + ":00"));

shold将被原始语句替换为不同的参数值,如:

command.Parameters.AddWithValue("@class", int.Parse(cboxRoomID.Text.Substring(4)));          
command.Parameters.AddWithValue("@person", int.Parse(cboxPersonSelection.Text.Substring(0, 2).Trim()));        
command.Parameters.AddWithValue("@start", DateTime.Parse(cboxWeekDate.Text + " " + n.AddHours(i).ToShortTimeString() + ":00"

希望这能解决问题。