我有一个代码,其中包含本地数据库“database1.sdf”以及带有一些值的表“Employees”。我正在使用 Datagridview.Datasource 属性将数据库中的所有行添加到 datagridview 中。所有行都从表中获取!美好的!当我尝试从 datagridview 更新列值时,它不会从数据库更新。删除行以及添加新行时会发生同样的问题。运行程序时没有出现异常。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace demoDatabaseOperations
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public SqlCeConnection con = new SqlCeConnection("Data source=Database1.sdf");
public DataTable dt = new DataTable();
public SqlCeDataAdapter adptr;
public SqlCeCommandBuilder cmdBuilder;
private void Form1_Load(object sender, EventArgs e)
{
con.Open();
if (con.State == ConnectionState.Open)
{
adptr = new SqlCeDataAdapter("select * from employees", con);
cmdBuilder = new SqlCeCommandBuilder(adptr);
adptr.UpdateCommand = cmdBuilder.GetUpdateCommand();
adptr.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
//Button For Update/delete/insert values to the database
private void button1_Click(object sender, EventArgs e)
{
try
{
adptr.Update(dt);
adptr.Dispose();
}
catch (Exception f) { MessageBox.Show(f.ToString()); }
}
}
}
有人有解决方案吗?
提前致谢