- 爱易网页
-
C#教程
- ACCESS 能新增记录但不能删除记录的有关问题!
日期:2014-05-18 浏览次数:20927 次
ACCESS 能新增记录但不能删除记录的问题!急
#region 在Access里更新记录
public void UpDateAccessRecord()
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + /*access mdb文件*/ + ";";
OleDbConnection oleDbConn = new OleDbConnection(strConn);
try
{
oleDbConn.Open();
}
catch (Exception ex)
{
throw (ex);
}
OleDbCommand ldeDbCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1]", oleDbConn);
OleDbDataAdapter oleDA = CommandAdapter.CreateCustomerAdapter(oleDbConn);
DataSet ds = new DataSet();
oleDA.SelectCommand = ldeDbCmdSelect;
try
{
oleDA.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
DataSetOption dsoption = new DataSetOption();
try
{
//dsoption.dataSetUpdate是我自己写的一个类的方法(更新ds),就是在ds中添加新记录和删除记录(只用row.delete作标记,不是真正删除)
oleDA.Update(dsoption.dataSetUpdate(ds));
//当新增记录时没问题,当删除记录时他提示:该ISAM不支持在链接表中删除数据
//我试了用sql的,没有问题。
}
catch (Exception ex)
{
throw (ex);
}
oleDA.Dispose();
oleDbConn.Close();
}
#endregion
//OleDbDataAdapter继承类,添加了命令语句属性值
class CommandAdapter
{
public static OleDbDataAdapter CreateCustomerAdapter( OleDbConnection connection)
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command;
// Create the SelectCommand.
command = new OleDbCommand("SELECT * FROM [Sheet1] " + "WHERE A = ?"
, connection);
command.Parameters.Add("@A", OleDbType.VarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new OleDbCommand(
"INSERT INTO [Sheet1] (A, B,C,item_no) " +
"VALUES (?, ? , ? , ?)", connection);
command.Parameters.Add("@A", OleDbType.Char, 5, "A");
command.Parameters.Add("@B", OleDbType.Char, 5, "B");
command.Parameters.Add("@C", OleDbType.Char, 5, "C");
command.Parameters.Add("@item_no", OleDbType.VarChar, 20, "item_no");
adapter.InsertCommand = command;
// Create the DeleteCommand
command = new OleDbCommand("DELETE FROM [Sheet1] " + "WHERE A = ?"
, connection);
//command.Parameters.Add("@A", OleDbType.VarChar, 15,"A").SourceVersion=DataRowVersion.Original;
command.Parameters.Add("@A", OleDbType.VarChar, 15, "A");
adapter.DeleteCommand = command;
return adapter;
}
}