using CropOffline; using Oracle.ManagedDataAccess.Client; public OracleConnection oraConnection; private bool oraConnect() { try { //连接字符串 string strOraConnect = "User ID=建立连接用的账号;Password=建立连接用的密码;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =localhost)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME =数据库名称)))"; oraConnection = new OracleConnection(strOraConnect); //给全局变量赋值 DbHelperOra.connectionString = strOraConnect; oraConnection.Open(); return true;//string.Empty; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } } private void Form1_Load(object sender,EventArgs e) { oraConnect(); dataGridView1.DataSource=DbHelperOra.QueryToDataTable("select 哼哼哈嘿 from dual"); }
需要DbHelperOra这个类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //For Connect Oracle using Oracle.ManagedDataAccess.Client; //For ArrayList C# using System.Collections; //For DataSet C# using System.Data; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; namespace CropOffline { [Serializable] //表示该类可被序列化 class DbHelperOra { // //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. public static string connectionString = "null";//PubConstant.ConnectionString; public DbHelperOra() { } #region 公用方法 //J 为啥要+1呢,我就想不通了。表格中的数据类型需要是number public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ") from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1; } else { return int.Parse(obj.ToString()); } } //J 调试用,用来检查连接字符串是否有误 public static string GetMaxIDT(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ") from " + TableName; object obj = GetSingle(strsql); if (obj == null) { return 1.ToString(); } else { return obj.ToString(); //return strsql; } } /// <summary> /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// </summary> /// <param name="strSQL">SQL语句</param> /// <param name="fs">图像字节,数据库的字段类型为image的情况</param> /// <returns>影响的记录数</returns> public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand cmd = new OracleCommand(strSQL, connection); OracleParameter myParameter = new OracleParameter("@fs", OracleDbType.LongRaw); myParameter.Value = fs; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (OracleException E) { throw new Exception(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand(SQLString, connection) ) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (OracleException e) { connection.Close(); throw new Exception(e.Message); } } } } /// 将一个object对象序列化,返回一个byte[] /// </summary> /// <param name="obj">能序列化的对象</param> /// <returns></returns> public static byte[] ObjectToBytes(object obj) { if(obj!=null) { using (MemoryStream ms = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.ToArray(); } } return null; } #region 测试序列化byte类型 public static byte[] SerializeObject(object obj) { IFormatter formatter = new BinaryFormatter(); //string result = string.Empty; using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, obj); byte[] byt = new byte[stream.Length]; byt = stream.ToArray(); // result = Encoding.UTF8.GetString(byt, 0, byt.Length); //result = Convert.ToBase64String(byt); stream.Flush(); return byt; } } #endregion /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString, params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (OracleException e) { throw new Exception(e.Message); } } } } private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (OracleParameter parm in cmdParms) cmd.Parameters.Add(parm); } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">多条SQL语句</param> public static void ExecuteSqlTran(ArrayList SQLStringList) { using (OracleConnection conn = new OracleConnection(connectionString)) { conn.Open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = conn; OracleTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { for (int n = 0; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n].ToString(); if (strsql.Trim().Length > 1) { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch (OracleException E) { tx.Rollback(); throw new Exception(E.Message); } } } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (OracleException E) { connection.Close(); throw new Exception(E.Message); } } } } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, params OracleParameter[] cmdParms) { using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand cmd = new OracleCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (OracleException E) { throw new Exception(E.Message); } } } } public static bool Exists(string strSql) { object obj = GetSingle(strSql); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); OracleDataAdapter command = new OracleDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (OracleException ex) { throw new Exception(ex.Message); } return ds; } } /// <summary> /// 执行查询语句,返回DataTable /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataTable QueryToDataTable(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataTable dt = new DataTable(); try { connection.Open(); OracleDataAdapter command = new OracleDataAdapter(SQLString, connection); command.Fill(dt); } catch (OracleException ex) { throw new Exception(ex.Message); } return dt; } } #endregion } }