2015-07-01 72 views
-3

我已經在C#中創建了一個將數據插入Oracle數據庫的程序。這是非常程序化的,我想提高我的程序(和我的知識)使用類。我在調用帶參數的方法時遇到了一些麻煩。這是我的代碼:如何在C#中使用參數調用Oracle連接方法?

public class Oracle { 
    public void Insert() { 
    string oracleConnectionString = "User Id=" + l_orauser + "; Password=" + l_orapass + "; Data Source=" + l_oradb; 
    using (OracleConnection oracleConnection = new OracleConnection(oracleConnectionString)) { 
     oracleConnection.Open(); 
     OracleGlobalization oracleSession = oracleConnection.GetSessionInfo(); 
     oracleSession.DateFormat = "dd-mm-yyyy hh24:mi:ss"; 
     oracleConnection.SetSessionInfo(oracleSession); 
     OracleTransaction oracleTransaction = oracleConnection.BeginTransaction(); 
     OracleCommand oracleCommand = oracleConnection.CreateCommand(); 
     oracleCommand.Transaction = oracleTransaction; 
     oracleCommand.CommandType = CommandType.Text; 
     string oracleCommandText = "insert into T1 (C1, C2, C3) values (:l_c1, :l_c2, :l_c3)"; 
     oracleCommand.CommandText = oracleCommandText; 
     oracleCommand.BindByName = true; 
     oracleCommand.Parameters.Add("l_c1", OracleDbType.Byte, 3).Value = l_c1; 
     oracleCommand.Parameters.Add("l_c2", OracleDbType.Date).Value = l_c2; 
     oracleCommand.Parameters.Add("l_c3", OracleDbType.Varchar2, 1024).Value = l_c3; 
     try { 
      oracleCommand.ExecuteNonQuery(); 
      oracleTransaction.Commit(); 
     } 
     catch (Exception ex) { 
      oracleTransaction.Rollback(); 
      MessageBox.Show(ex.Message); 
     } 
     finally { 
      oracleCommand.Parameters.Clear(); 
      oracleCommand.Dispose(); 
      oracleTransaction.Dispose(); 
      oracleConnection.Close(); 
      oracleConnection.Dispose(); 
     } 
    } 
} 
} 

我想用一些參數來調用這個 - 變量:l_orauser,l_orapass,l_oradb,l_c1,l_c2,l_c3,這是從形式的因素所構成,例如文本框,的DateTimePicker。我怎樣才能做到這一點?

public static void Main(string[] args) { 
var testOracle = new Oracle(); 
testOracle.Insert(); 
} 
+0

公共無效插入(變量類型變量名,第二個變量類型的第二變量名稱等) http://www.tutorialspoint.com/csharp/csharp_methods.htm –

+0

謝謝你,我已經讀過關於這種類型的調用。我已經理解,但有可能使用構造函數?或者我錯了? – Geo

+0

是的,你也可以爲你的班級製作一個構造函數,這些變量需要這些變量 –

回答

-1

好的,所以在與Tim Freese討論之後,我決定使用構造函數和一組參數。

僅供參考我添加的代碼,也許有人會發現它有用:

public static void Main(string[] args) { 
string oracleUser, oraclePassword, oracleDatabase; 
List<string> oracleArguments = new List<string>(); 
//0 = oracleUser 
//1 = oraclePassword 
//2 = oracleDatabase 
//3 = oracleCommandText 
//4+ = oracleCommand.Parameters 
l_orauser = "schema1"; 
l_orapass = "schema1pass"; 
l_oradb = "db1"; 
oracleArguments.Add(l_orauser); 
oracleArguments.Add(l_orapass); 
oracleArguments.Add(l_oradb); 
Oracle testOracle = new Oracle(); 
testOracle.Insert(oracleArguments); 
} 

和Oracle類:

public class Oracle { 
public void Insert(List<string> oracleArguments) { 
    string oracleConnectionString = "User Id=" + oracleArguments[0] + "; Password=" + oracleArguments[1] + "; Data Source=" + oracleArguments[2]; 
    using (OracleConnection oracleConnection = new OracleConnection(oracleConnectionString)) { 
     //do something 
    } 
} 
} 
相關問題