winform 根据dataGridView中的值,来循环使用
for (int i = 0; i < dataGridView.RowCount - 1; i++) //dataGridView.RowCount - 1 来获取列数,-1是因为第一列是标题一般
{
try
{
//这些A、B、C等等,是用来存值,为以后SQL语句填值用,Row是行,Cell是列
string A= dataGridView1.Rows[i].Cells[0].Value.ToString();
string B = dataGridView1.Rows[i].Cells[1].Value.ToString();
string C = dataGridView1.Rows[i].Cells[2].Value.ToString();
//。。。。。。。看你需要多少行
SqlConnection sqlConnection = new SqlConnection(constr);
sqlConnection.Open();
SqlCommand cmd = sqlConnection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "在这里填需要的SQL语句";
cmd.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}