C#连接sqlite获取数据并添加到datagridview中 20
C#连接sqlite获取数据并添加到datagridview中,提示fileisencryptedorisnotadatabase,代码见下图。是我连接字符串有问题吗?求...
C#连接sqlite获取数据并添加到datagridview中,提示file is encrypted or is not a database,代码见下图。是我连接字符串有问题吗?求各位大牛帮忙解答一下!!万分感谢!!这是我的连接字符串,public static string connstr = "Data Source=test.db;Version=3";
展开
2个回答
展开全部
获取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());
}
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());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询