OleDbDataAdapter只能执行select类型的查询语句吗??

来源:百度知道 编辑:UC知道 时间:2024/09/20 12:01:12
我用两条sql语句(1)(2)分别对access表进行操作。程序执行完语句(1)后,返回的表数据能在dataGridView1中正常显示,但是执行完语句(2)后,不能在 dataGridView1中显示结果,提示错误:无法找到 表[0],我看了不少例子,没有人用OleDbDataAdapter执行delete,update语句,请问高手:OleDbDataAdapter只能执行select类型的查询语句吗??

string connString = @"Provider=Microsoft.Jet.OleDB.4.0;Data
Source=0000.mdb";
OleDbConnection conn=new OleDbConnection(connString);

//(1) string sql = "select name,age,sex from test000";
(2) string sql = "update test000 set name='love' where
name='liyun'";

OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

DataSet ds = new DataSet();
da.Fill(ds);
conn.Open();
this.dataGridView1.DataSource = ds.Tables[0];
ninjuli,感谢你的回答,不过我不明白,我的代码到底是哪几条语句错了呢?你能不能把我的代码改一下?really appriciate !

用法错误
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

// Create the Insert, Update and Delete commands.
da.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");

da.UpdateCommand = new OleDbCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");

da.DeleteCommand = new OleDbCommand(
"DELETE FROM Customers WHERE CustomerID = ?");

你少一个Command对象吧?
楼上的对的
da.SelectCommand=new OledbCommand(sql,conn);