更新时间:2023-02-07 10:32:36
如果移动for循环,则无需建立多个连接。只需对您的代码块进行快速编辑(绝对不完全正确):
If you move your for loop, you won't have to make multiple connections. Just a quick edit to your code block (by no means completely correct):
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(ConnString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for(int i=0; i< dataGridView1.Rows.Count;i++)
{
StrQuery= @"INSERT INTO tableName VALUES ("
+ dataGridView1.Rows[i].Cells["ColumnName"].Text+", "
+ dataGridView1.Rows[i].Cells["ColumnName"].Text+");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
要一次执行多个SQL命令,请查看以下链接:
单个语句中有多个语句SqlCommand
As to executing multiple SQL commands at once, please look at this link: Multiple statements in single SqlCommand