基于ManagedDataAccess开发的OracleDBHelpe工具集伸手党的福音
Posted shyfrog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于ManagedDataAccess开发的OracleDBHelpe工具集伸手党的福音相关的知识,希望对你有一定的参考价值。
在使用前先加入ManagedDataAccessDLL文件方可使用
添加方法:右键项目。点击管理NuGet程序包,点击浏览,在输入框内输入ManagedDataAccess,再点击安装即可
OracleDBHelper代码如下(Copy直接用)
/// <summary> ///基於.net( 向下兼容4.0)開發 OracleDBHelper工具類 ///<para>作者: 害羞的青蛙</para> ///<para>時間: 2019-12-6</para> /// </summary> public static class OracleDBHelper { /// <summary> /// 執行SQL語句返回DataTable /// </summary> /// <param name="SQL">SQL語句</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <returns></returns> public static DataTable GetDataTableBySQL(string SQL,string DBUrl) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = SQL; OracleDataAdapter adapter = new OracleDataAdapter(cmd); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return dataTable; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 執行非查詢的SQL語句 /// </summary> /// <param name="SQL">SQL語句</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <returns></returns> public static int GetNonQueryBySQL(string SQL, string DBUrl) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = SQL; int num = cmd.ExecuteNonQuery(); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return num; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 執行多條SQL語句,實現數據庫事務。 /// </summary> /// <param name="SQLStringList">多條SQL語句</param> /// <param name="BDUrl">數據庫鏈接地址</param> public static int GetNonQueryByManySQL(ArrayList SQLStringList,string BDUrl) { using (OracleConnection conn = new OracleConnection(BDUrl)) { conn.Open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = conn; OracleTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { int num = 0; for (int i = 0; i < SQLStringList.Count; i++) { string SQL = SQLStringList[i].ToString();//獲取SQL語句 if (SQL.Trim().Length > 1) { cmd.CommandText = SQL; num=cmd.ExecuteNonQuery(); } tx.Commit();//提交事務 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 } return num;//返回執行結果數量 } catch (OracleException E) { tx.Rollback();//事務回滾 throw new Exception(E.Message); } } } /// <summary> /// 調用存儲返回單個游標結果集(最後一個位置必須為游標,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_cursor=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值, out_cursor為游標不需要輸入值</para> /// </summary> /// <param name="storageName">存儲名稱</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <param name="obj">存儲參數對象</param> /// <returns></returns> public static DataTable GetDataTable(string storageName, string DBUrl, object obj ) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 1)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.RefCursor).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } DataTable dataTable = new DataTable(); OracleDataAdapter oda = new OracleDataAdapter(cmd); oda.Fill(dataTable); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return dataTable; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 調用存儲返回String字符串信息(最後一個位置必須為String類型字符,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_string=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值,out_string為輸出參數不需要輸入值</para> /// </summary> /// <param name="storageName"></param> /// <param name="DBUrl"></param> /// <param name="obj"></param> /// <returns></returns> public static string GetStringData(string storageName, string DBUrl, object obj) { OracleConnection conn = new OracleConnection(DBUrl); conn.Open(); //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 1)) { //設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2, short.MaxValue).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } cmd.ExecuteNonQuery(); string message = cmd.Parameters[properties[properties.Length - 1].Name].Value.ToString();//獲取返回的值 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return message; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 調用存儲返回String字符串信息和DataTable數據表格(最後兩個位置必須為返回參數,一個為輸出字符串另一個為游標,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_string="",out_cursor=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值,out_string為輸出參數不需要輸入值, out_cursor為游標不需要輸入值</para> /// </summary> /// <param name="storageName">存儲名稱</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <param name="obj">存儲參數對象</param> /// <param name="dataTable">返回結果集</param> /// <returns></returns> public static string GetStringAndDataTable(string storageName, string DBUrl, object obj,out DataTable dataTable) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 2)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2, short.MaxValue).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else if (i == (properties.Length - 1)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.RefCursor).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } DataTable dt = new DataTable(); OracleDataAdapter oda = new OracleDataAdapter(cmd); oda.Fill(dt); dataTable = dt;//返回數據結果集 string message= cmd.Parameters[properties[properties.Length - 2].Name].Value.ToString();//獲取輸出的字符串 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return message; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } }
/// <summary> /// 執行SQL語句返回DataTable /// </summary> /// <param name="SQL">SQL語句</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <returns></returns> public static DataTable GetDataTableBySQL(string SQL,string DBUrl) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = SQL; OracleDataAdapter adapter = new OracleDataAdapter(cmd); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return dataTable; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 執行非查詢的SQL語句 /// </summary> /// <param name="SQL">SQL語句</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <returns></returns> public static int GetNonQueryBySQL(string SQL, string DBUrl) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = SQL; int num = cmd.ExecuteNonQuery(); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return num; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 執行多條SQL語句,實現數據庫事務。 /// </summary> /// <param name="SQLStringList">多條SQL語句</param> /// <param name="BDUrl">數據庫鏈接地址</param> public static int GetNonQueryByManySQL(ArrayList SQLStringList,string BDUrl) { using (OracleConnection conn = new OracleConnection(BDUrl)) { conn.Open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = conn; OracleTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { int num = 0; for (int i = 0; i < SQLStringList.Count; i++) { string SQL = SQLStringList[i].ToString();//獲取SQL語句 if (SQL.Trim().Length > 1) { cmd.CommandText = SQL; num=cmd.ExecuteNonQuery(); } tx.Commit();//提交事務 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉
} return num;//返回執行結果數量 } catch (OracleException E) { tx.Rollback();//事務回滾 throw new Exception(E.Message); } } } /// <summary> /// 調用存儲返回單個游標結果集(最後一個位置必須為游標,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_cursor=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值, out_cursor為游標不需要輸入值</para> /// </summary> /// <param name="storageName">存儲名稱</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <param name="obj">存儲參數對象</param> /// <returns></returns> public static DataTable GetDataTable(string storageName, string DBUrl, object obj ) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 1)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.RefCursor).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } DataTable dataTable = new DataTable(); OracleDataAdapter oda = new OracleDataAdapter(cmd); oda.Fill(dataTable); cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return dataTable; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 調用存儲返回String字符串信息(最後一個位置必須為String類型字符,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_string=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值,out_string為輸出參數不需要輸入值</para> /// </summary> /// <param name="storageName"></param> /// <param name="DBUrl"></param> /// <param name="obj"></param> /// <returns></returns> public static string GetStringData(string storageName, string DBUrl, object obj) { OracleConnection conn = new OracleConnection(DBUrl); conn.Open(); //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 1)) { //設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2, short.MaxValue).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } cmd.ExecuteNonQuery(); string message = cmd.Parameters[properties[properties.Length - 1].Name].Value.ToString();//獲取返回的值 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return message; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } } /// <summary> /// 調用存儲返回String字符串信息和DataTable數據表格(最後兩個位置必須為返回參數,一個為輸出字符串另一個為游標,位置不能顛倒) /// <para>obj使用方法:new{ v_data=value, v_data1=value1,out_string="",out_cursor=""}</para> /// <para>注意:obj中v_data為存儲參數名稱,value為對應的值,out_string為輸出參數不需要輸入值, out_cursor為游標不需要輸入值</para> /// </summary> /// <param name="storageName">存儲名稱</param> /// <param name="DBUrl">數據庫鏈接地址</param> /// <param name="obj">存儲參數對象</param> /// <param name="dataTable">返回結果集</param> /// <returns></returns> public static string GetStringAndDataTable(string storageName, string DBUrl, object obj,out DataTable dataTable) { // 获取与数据库的连接对象並且绑定连接字符串 OracleConnection conn = new OracleConnection(DBUrl); conn.Open();//打開資源 //获取数据库操作对象 OracleCommand cmd = conn.CreateCommand(); try { cmd.CommandText = storageName;//存儲名稱 cmd.CommandType = CommandType.StoredProcedure; PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);//獲取object中的字段名和值 for (int i = 0; i < properties.Length; i++) { if (i == (properties.Length - 2)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2, short.MaxValue).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else if (i == (properties.Length - 1)) {//設定輸出的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.RefCursor).Direction = ParameterDirection.Output; cmd.Parameters[properties[i].Name].Value = DBNull.Value;//賦值 } else {//設定輸入的類型和值 cmd.Parameters.Add(properties[i].Name, OracleDbType.Varchar2).Direction = ParameterDirection.Input; cmd.Parameters[properties[i].Name].Value = properties[i].GetValue(obj, null);//賦值 } } DataTable dt = new DataTable(); OracleDataAdapter oda = new OracleDataAdapter(cmd); oda.Fill(dt); dataTable = dt;//返回數據結果集 string message= cmd.Parameters[properties[properties.Length - 2].Name].Value.ToString();//獲取輸出的字符串 cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 return message; } catch (Exception ex) { cmd.Dispose();//釋放資源 conn.Dispose();//釋放資源 conn.Close();//關閉 throw ex; } }
以上是关于基于ManagedDataAccess开发的OracleDBHelpe工具集伸手党的福音的主要内容,如果未能解决你的问题,请参考以下文章
基于ManagedDataAccess开发的OracleDBHelpe工具集伸手党的福音
C#连接Oracle的时候,Oracle.DataAccess.dll能连接数据库,Oracle.ManagedDataAccess却不能?
Oracle.ManagedDataAccess:TNS:无法解析指定的连接标识符
C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装