英文:
C# MS Access OleDb can't make write operations
问题
我有一个在MS Access中的数据库,我正在尝试编辑表(db)。SELECT对我有效,但是像INSERT、SET和DELETE这样的编辑操作没有改变任何内容。
我有下面的代码,用于删除所有行。初始时表中有3行,当我运行代码时,受影响的行数输出为3,但表中没有任何变化。
static void Main(string[] args)
{
DeleteRow();
}
static void DeleteRow()
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "DELETE FROM db";
Console.WriteLine(command.ExecuteNonQuery());
connection.Close();
Console.ReadKey();
}
我正在使用VS 2022,当我通过服务器资源管理器运行查询时,它成功地删除了行。
英文:
I have a database in MS Access and I am trying to edit the table (db). SELECT works for me but edit operations like INSERT, SET and DELETE does not change anything.
I have this code below that deletes all rows. Initally there are 3 rows in the table, when I run the code output of affected rows is 3 but nothing is changed in the table.
static void Main(string[] args)
{
DeleteRow();
}
static void DeleteRow()
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "DELETE FROM db";
Console.WriteLine(command.ExecuteNonQuery());
connection.Close();
Console.ReadKey();
}
I am using VS 2022 and when I run the query through server explorer it succesfully deletes the rows.
答案1
得分: 1
Close方法会回滚任何待处理的事务。然后将连接释放到连接池,或者如果禁用连接池,则关闭连接。
在关闭之前,您应该尝试提交事务。
英文:
From the OleDBConnection documentation:
> The Close method rolls back any pending transactions. It then releases
> the connection to the connection pool, or closes the connection if
> connection pooling is disabled.
You should try to commit the transaction before closing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论