如何在ORACLE

时间:2017-06-05 12:30:25

标签: c# sql oracle

我想在ORACLE中运行更新查询但是要反映该查询的更改(意味着使用接收值更新数据库中的值)我必须在该查询之后运行提交; (然后只有值更改才会在数据库中更新)。那么,我如何通过C#传递更新语句和提交在同一行。

我正在运行如下的查询,

UPDATE table_name 
   SET columnname1 = 'N', 
       columnname2 = 1
 WHERE columnname3 = '-2085371064';

COMMIT;

2 个答案:

答案 0 :(得分:2)

假设您使用的是ADO.NET,则可以使用Commit()方法:

using (OracleConnection connection = new OracleConnection(connectionString))
{
    connection.Open();

    OracleCommand command = connection.CreateCommand();
    OracleTransaction transaction;

    transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
    command.Transaction = transaction;

    try
    {
        command.CommandText = 
            "UPDATE table_name    SET columnname1 = 'N',        columnname2 = 1 WHERE columnname3 = '-2085371064';";
        command.ExecuteNonQuery();
        transaction.Commit();
    }
    catch (Exception e)
    {
        transaction.Rollback();
        Console.WriteLine(e.ToString());
    }
}

答案 1 :(得分:2)

通常,您不希望显式 commit,因为所有更改都将在连接关闭时自动提交。但是,如果您坚持使用COMMIT,则可以借助Oracle的匿名阻止来实现:

 //TODO: put the right connection here
 using (OracleConnection con = new OracleConnection(ConnectionStringHere)) {
   con.Open();

   string sql = 
     @"BEGIN -- Anonymous block: run these queries (update, commit) together
         UPDATE table_name 
            SET columnname1 = 'N', 
                columnname2 = 1
          WHERE columnname3 = '-2085371064'; -- Is it really a string?

         COMMIT; -- Not necessary, but possible
       END;";

   using (var q = con.CreateCommand()) {
     q.CommandText = sql;

     q.ExecuteNonQuery();   
   } 
 }