C#连接sqlite获取数据并添加到datagridview中

2024-11-24 21:41:51
推荐回答(2个)
回答1:

获取datagridview单元格修改后的值,然后用这个值去更新数据库就可以了,下面上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

try
{
SqlConnection scon = new SqlConnection("数据库连接字");
scon.Open();
SqlCommand scmd;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string id = dataGridView1.Rows[i].Cells["id"].EditedFormattedValue.ToString();
string name = dataGridView1.Rows[i].Cells["name"].EditedFormattedValue.ToString();
string age = dataGridView1.Rows[i].Cells["age"].EditedFormattedValue.ToString();
string address = dataGridView1.Rows[i].Cells["address"].EditedFormattedValue.ToString();
string scmdStr = "update studentInfo set name='" + name + "',age='" + age + "',address='" + address + "' where id ='" + id + "'";
scmd = new SqlCommand(scmdStr, scon);
scmd.ExecuteNonQuery();
}
scon.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

回答2:

网上的解答:网页链接