using System; using Oracle.ManagedDataAccess.Client; using System.IO; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json;
我們可以在設定連接字符串中直接設定連接池(connection pool)。選項 Pooling 設置為 false,會禁用連接池; 預設是啟用的: Pooling=true。 Min Pool Size 和 Max Pool Size 允許配置池中的連接數。預設情況下,Min Pool Size 的值為 1,Max Pool Size 的值為 100。Connection Lifetime 定義了連線在釋放前在池中保持不活躍狀態的時間。
using System; using Oracle.ManagedDataAccess.Client; using System.IO; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json;
namespaceOracleSample.src { publicclassCommandSample { publicstaticvoidCreateCommand() { using (var connection = new OracleConnection(GetConnectionString())) { string sql = "SELECT empno, ename, hiredate, sal, deptno From emp"; var command = new OracleCommand(sql, connection); connection.Open(); //... } } privatestaticstringGetConnectionString() { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config.json"); IConfiguration config = configurationBuilder.Build(); string connectionString = config["Data:DefaultConnection:ConnectionString"]; return connectionString; } } }
using System; using System.IO; using Oracle.ManagedDataAccess.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json;
namespaceOracleSample.src { publicclassExecuteSample { publicstaticvoidExecuteReader(int deptnoParameter) { string sql = "SELECT empno, ename, hiredate, sal, comm, deptno From emp where deptno = :deptno order by empno";
try { using (var connection = new OracleConnection(GetConnectionString())) using (var command = new OracleCommand(sql, connection)) { command.BindByName = true;
OracleParameter deptnoBind = new OracleParameter("deptno", OracleDbType.Int32); deptnoBind.Value = deptnoParameter; command.Parameters.Add(deptnoBind);
connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { short empno = reader.GetInt16(0); string ename = reader.GetString(1); DateTime hiredate = reader.GetDateTime(2); float sal = reader.GetFloat(3); float? comm = reader.IsDBNull(4) ? (float?)null : reader.GetFloat(4); short deptno = reader.GetInt16(5);
short empno = (short)reader[0]; string ename = (string)reader[1]; DateTime hiredate = (DateTime)reader[2]; float sal = (float)reader[3]; float? comm = reader.IsDBNull(4) ? (float?) null : (float?)reader[4]; short deptno = (short)reader[5];
OracleDataReader 的索引器還允許使用 string 而不是 int 傳遞列名 (column name)。在這些不同的選項中,這是最慢的方法,但它的可讀性最佳,與發出服務調用所需的時間相比,訪問索引器所需的額外時間其實可以忽略不計。
src/ExecuteSample.cs
1 2 3 4 5 6
short empno = (short)reader["empno"]; string ename = (string)reader["ename"]; DateTime hiredate = (DateTime)reader["hiredate"]; float sal = (float)reader["sal"]; float? comm = reader.IsDBNull(4) ? (float?)null : (float?)reader["comm"]; short deptno = (short)reader["deptno"];
強烈型的語言有時在映對資料庫的資料型態實在是很困擾,既然是從資料庫返回的資料,不管它是否為 null,總是會有初始值,我們就直接使用 var 類型推斷。 要記得使用 var 類型推斷,一定要賦予初始值,否則它無法推斷:
src/ExecuteSample.cs
1 2 3 4 5 6
var empno = reader["empno"]; var ename = reader["ename"]; var hiredate = reader["hiredate"]; var sal = reader["sal"]; var comm = reader["comm"]; var deptno = reader["deptno"];
try { using (var connection = new OracleConnection(GetConnectionString())) using (var command = new OracleCommand(sql, connection)) { command.BindByName = true;
OracleParameter empnoBind = new OracleParameter("empno", OracleDbType.Int32); empnoBind.Value = newEmployee.empno; command.Parameters.Add(empnoBind);
OracleParameter enameBind = new OracleParameter("ename", OracleDbType.Varchar2); enameBind.Value = newEmployee.ename; command.Parameters.Add(enameBind);
OracleParameter jobBind = new OracleParameter("job", OracleDbType.Varchar2); jobBind.Value = newEmployee.job; command.Parameters.Add(jobBind);
OracleParameter mgrBind = new OracleParameter("mgr", OracleDbType.Int32); mgrBind.Value = newEmployee.mgr; command.Parameters.Add(mgrBind);
OracleParameter hiredateBind = new OracleParameter("hiredate", OracleDbType.Date); hiredateBind.Value = newEmployee.hiredate; command.Parameters.Add(hiredateBind);
OracleParameter salBind = new OracleParameter("sal", OracleDbType.Decimal); salBind.Value = newEmployee.sal; command.Parameters.Add(salBind);
OracleParameter commBind = new OracleParameter("comm", OracleDbType.Decimal); commBind.Value = newEmployee.comm; command.Parameters.Add(commBind);
OracleParameter deptnoBind = new OracleParameter("deptno", OracleDbType.Int32); deptnoBind.Value = newEmployee.deptno; command.Parameters.Add(deptnoBind);
using System; using System.IO; using Oracle.ManagedDataAccess.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json;
namespaceReceive.Services { publicclassOraDemoMessage { publicstaticvoidSave(string messageParameter, string createdbyParameter) { string sql = "INSERT INTO DEMO_MESSAGES (message, createdby) " + "VALUES (:message, :createdby)"; try { using (var connection = new OracleConnection(GetConnectionString())) using (var command = new OracleCommand(sql, connection)) { command.BindByName = true;
OracleParameter messageBind = new OracleParameter("message", OracleDbType.Varchar2); messageBind.Value = messageParameter; command.Parameters.Add(messageBind);
OracleParameter createdbyBind = new OracleParameter("createdby", OracleDbType.Varchar2); createdbyBind.Value = createdbyParameter; command.Parameters.Add(createdbyBind);