private void button1_Click(object sender, EventArgs e)
{
int i = 1;
int j = dataGridView1.Rows.Count;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (i < j)
{
i++;
string a = row.Cells[0].Value.ToString();
string b = row.Cells[1].Value.ToString();
SqlConnection conn = new SqlConnection(@"Data Source=PC-200908101229/SQL2005;Initial Catalog=student;Integrated Security=True");
conn.Open();
SqlCommand comm = new SqlCommand("insert into T_student(Name)values('" + b + "')", conn);
comm.ExecuteNonQuery();
conn.Close();
}
}
//MessageBox.Show(j.ToString());
}
本文介绍了一个使用C#将DataGridView中的数据批量插入到SQL Server数据库的具体实现过程。通过遍历DataGridView的每一行,构造SQL插入语句,并执行这些语句来完成数据的导入。该示例适用于学习如何在Windows Forms应用中操作数据库。
2430

被折叠的 条评论
为什么被折叠?



