C# MS Access OleDb无法进行写操作。

huangapple go评论89阅读模式
英文:

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

根据OleDBConnection文档

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.

huangapple
  • 本文由 发表于 2023年8月8日 22:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定