主题:dataGridView中更新数据
namespace WindowsApplication2
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private OleDbDataAdapter odb = new OleDbDataAdapter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\a\db1.mdb";
OleDbConnection MyCon = new OleDbConnection(strCon);
MyCon.Open();
OleDbCommand scd = new OleDbCommand("select * from [table]", MyCon);
odb.SelectCommand = scd;
odb.Fill(DT);
dataGridView1.DataSource = DT;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
OleDbCommandBuilder cmdbuilder = new OleDbCommandBuilder(odb);
odb.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
}
}
}
运行程序时已经可以在dataGridView中显示出数据,但是在dataGridView中更改数据后,点击按钮后,会报“system.data.oledb.oledbexception update 声明结构错误”,我上网查,有的说是权限问题,我刚学不太清楚,如果不是,那到底是什么问题,请各位高手仔细帮我看看,给出详细的解决办法
{
public partial class Form1 : Form
{
private DataTable DT = new DataTable();
private OleDbDataAdapter odb = new OleDbDataAdapter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\a\db1.mdb";
OleDbConnection MyCon = new OleDbConnection(strCon);
MyCon.Open();
OleDbCommand scd = new OleDbCommand("select * from [table]", MyCon);
odb.SelectCommand = scd;
odb.Fill(DT);
dataGridView1.DataSource = DT;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
OleDbCommandBuilder cmdbuilder = new OleDbCommandBuilder(odb);
odb.Update(DT);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("更新成功!");
}
}
}
运行程序时已经可以在dataGridView中显示出数据,但是在dataGridView中更改数据后,点击按钮后,会报“system.data.oledb.oledbexception update 声明结构错误”,我上网查,有的说是权限问题,我刚学不太清楚,如果不是,那到底是什么问题,请各位高手仔细帮我看看,给出详细的解决办法