2017-06-05 67 views
1

我想在ORACLE中運行更新查詢,但要反映該查詢的更改(意味着使用接受值更新數據庫中的值),我必須運行一個提交;在那個查詢之後的(那麼只有值更改將在數據庫中更新)。那麼,我如何通過C#將更新語句和提交傳遞到同一行。如何在ORACLE中運行一個更新並提交單個命令

我運行像下面的查詢,

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

COMMIT; 
+0

你怎麼說*「反映了查詢的變化」 *的意思是:但是,如果你堅持COMMIT你可以用Oracle的匿名塊的幫助下做到這一點? – eocron

+0

你確定'columnname3'的類型是'Char' \'VarChar2'嗎? 'columnname3 ='-2085371064''?它似乎應該是'columnname3 = -2085371064'(無撇號) –

+0

通常,您不希望* explict *'commit':關閉連接時,所有更改將由* autocommit *應用。 –

回答

2

假設你正在使用ADO.NET,您可以利用提交()方法:

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()); 
    } 
} 
+0

感謝人工作:) –

2

通常情況下,你不想要明確commit因爲所有的變化將是自動提交關閉連接。

//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(); 
    } 
} 
相關問題