string SqlStr = "Insert Into Table values()"
SqlCommand SqlCmdInSert = new SqlCommand(SqlStr,Connection)
SqlCmdInSert.ExecuteNonQuery() 我在C#中向数据库中插入记录,单条插入时程序代码如上,且没有问题。
我想一次插入多条,请问上面的语句【string SqlStr = "Insert Into Table values()" 】
应该如何扩展?谢谢!
SqlCommand SqlCmdInSert = new SqlCommand(SqlStr,Connection)
SqlCmdInSert.ExecuteNonQuery() 我在C#中向数据库中插入记录,单条插入时程序代码如上,且没有问题。
我想一次插入多条,请问上面的语句【string SqlStr = "Insert Into Table values()" 】
应该如何扩展?谢谢!
string strSQL = "insert into testTable values({0},'120')"; SqlCommand cmd; string[] sql = new string[info.Length]; for (int i = 0; i < info.Length; i++) { sql[i] = string.Format(strSQL, info[i].ToString()); } foreach(var t in sql) { Console.WriteLine(t); cmd = new SqlCommand(t, conn); try { cmd.ExecuteNonQuery(); } catch { conn.Close(); conn.Dispose(); cmd.Dispose(); throw new Exception("Failed to insert"); } } Console.WriteLine("Succeed to insert"); }
多行插入。。union
SqlStr += " Insert Into Table values('2') "
SqlStr += " Insert Into Table values('3') "
SqlCommand SqlCmdInSert = new SqlCommand(SqlStr,Connection)
SqlCmdInSert.ExecuteNonQuery()
最好加入事务,防止执行过程出现问题。
SQLSTR+=“INTO tb_kj139_recorddatatemp_hdata1(i_senid,d_readtime,i_cardcode,i_cardstate) VALUES(94,'2008-11-4 0:16:45',10239,85);”
SQLSTR+=“INTO tb_kj139_recorddatatemp_hdata1(i_senid,d_readtime,i_cardcode,i_cardstate) VALUES(93,'2008-11-4 0:16:45',10239,85);”
你利用for循环把你要插入的记录组成一个SQL语句,就想上面的插入语句最后执行SQLSTR就OK了,这样不就一次插入多条记录了吗
Insert Into Table
select value1,value2....
Union All select value1,value2....
Union All select value1,value2....
这个就 可以了
如果是多个字段 ([aa],[bb])values (@aa,@bb)
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
就这样多写几个串起来,我写过的,不信你就在查询分析器里试试呀。