用C#来操作ORACLE数据库.docx
《用C#来操作ORACLE数据库.docx》由会员分享,可在线阅读,更多相关《用C#来操作ORACLE数据库.docx(15页珍藏版)》请在冰豆网上搜索。
用C#来操作ORACLE数据库
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Specialized;
usingSystem.Data;
usingSystem.Data.OracleClient;
usingSystem.Configuration;
namespaceLiTianPing.OracleDAL//修改成实际项目的命名空间名称
{
///
///Copyright(C)2004-2008LiTianPing
///数据访问基础类(基于Oracle)
///可以用户可以修改满足自己项目的需要。
publicabstractclassDbHelperOra
//数据库连接字符串(web.config来配置)
//
protectedstaticstringconnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
publicDbHelperOra()
}
#region公用方法
publicstaticintGetMaxID(stringFieldName,stringTableName)
stringstrsql="selectmax("+FieldName+")+1from"+TableName;
objectobj=GetSingle(strsql);
if(obj==null)
return1;
else
returnint.Parse(obj.ToString());
publicstaticboolExists(stringstrSql,paramsSqlParameter[]cmdParms)
objectobj=GetSingle(strSql,cmdParms);
intcmdresult;
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
cmdresult=0;
cmdresult=int.Parse(obj.ToString());
if(cmdresult==0)
returnfalse;
returntrue;
#endregion
#region 执行简单SQL语句
///执行SQL语句,返回影响的记录数
///SQL语句
///影响的记录数
publicstaticintExecuteSql(stringSQLString)
using(OracleConnectionconnection=newOracleConnection(connectionString))
using(OracleCommandcmd=newOracleCommand(SQLString,connection))
try
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
catch(System.Data.OracleClient.OracleExceptionE)
connection.Close();
thrownewException(E.Message);
///执行多条SQL语句,实现数据库事务。
///多条SQL语句
publicstaticvoidExecuteSqlTran(ArrayListSQLStringList)
using(OracleConnectionconn=newOracleConnection(connectionString))
conn.Open();
OracleCommandcmd=newOracleCommand();
cmd.Connection=conn;
OracleTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
for(intn=0;n { stringstrsql=SQLStringList[n].ToString(); if(strsql.Trim().Length>1) { cmd.CommandText=strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch(System.Data.OracleClient.OracleExceptionE) { tx.Rollback(); thrownewException(E.Message); } } } /// ///执行带一个存储过程参数的的SQL语句。 /// ///SQL语句 ///参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加 ///影响的记录数 publicstaticintExecuteSql(stringSQLString,stringcontent) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { OracleCommandcmd=newOracleCommand(SQLString,connection); System.Data.OracleClient.OracleParameter myParameter=newSystem.Data.OracleClient.OracleParameter("@content",OracleDbType.NText); myParameter.Value=content; cmd.Parameters.Add(myParameter); try { connection.Open(); introws=cmd.ExecuteNonQuery(); returnrows; } catch(System.Data.OracleClient.OracleExceptionE) { thrownewException(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// ///向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// ///SQL语句 ///图像字节,数据库的字段类型为image的情况 ///影响的记录数 publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { OracleCommandcmd=newOracleCommand(strSQL,connection); System.Data.OracleClient.OracleParameter myParameter=newSystem.Data.OracleClient.OracleParameter("@fs",OracleDbType.Image); myParameter.Value=fs; cmd.Parameters.Add(myParameter); try { connection.Open(); introws=cmd.ExecuteNonQuery(); returnrows; } catch(System.Data.OracleClient.OracleExceptionE) { thrownewException(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// ///执行一条计算查询结果语句,返回查询结果(object)。 /// ///计算查询结果语句 ///查询结果(object) publicstaticobjectGetSingle(stringSQLString) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { using(OracleCommandcmd=newOracleCommand(SQLString,connection)) { try { connection.Open(); objectobj=cmd.ExecuteScalar(); if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { returnnull; } else { returnobj; } } catch(System.Data.OracleClient.OracleExceptione) { connection.Close(); thrownewException(e.Message); } } } } /// ///执行查询语句,返回OracleDataReader /// ///查询语句 ///OracleDataReader publicstaticOracleDataReaderExecuteReader(stringstrSQL) { OracleConnectionconnection=newOracleConnection(connectionString); OracleCommandcmd=newOracleCommand(strSQL,connection); try { connection.Open(); OracleDataReadermyReader=cmd.ExecuteReader(); returnmyReader; } catch(System.Data.OracleClient.OracleExceptione) { thrownewException(e.Message); } } /// ///执行查询语句,返回DataSet /// ///查询语句 ///DataSet publicstaticDataSetQuery(stringSQLString) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { DataSetds=newDataSet(); try { connection.Open(); OracleDataAdaptercommand=newOracleDataAdapter(SQLString,connection); command.Fill(ds,"ds"); } catch(System.Data.OracleClient.OracleExceptionex) { thrownewException(ex.Message); } returnds; } } #endregion #region执行带参数的SQL语句 /// ///执行SQL语句,返回影响的记录数 /// ///SQL语句 ///影响的记录数 publicstaticintExecuteSql(stringSQLString,paramsOracleParameter[]cmdParms) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { using(OracleCommandcmd=newOracleCommand()) { try { PrepareCommand(cmd,connection,null,SQLString,cmdParms); introws=cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); returnrows; } catch(System.Data.OracleClient.OracleExceptionE) { thrownewException(E.Message); } } } } /// ///执行多条SQL语句,实现数据库事务。 /// ///SQL语句的哈希表(key为sql语句,value是该语句的OracleParameter[]) publicstaticvoidExecuteSqlTran(HashtableSQLStringList) { using(OracleConnectionconn=newOracleConnection(connectionString)) { conn.Open(); using(OracleTransactiontrans=conn.BeginTransaction()) { OracleCommandcmd=newOracleCommand(); try { //循环 foreach(DictionaryEntrymyDEinSQLStringList) { string cmdText=myDE.Key.ToString(); OracleParameter[]cmdParms=(OracleParameter[])myDE.Value; PrepareCommand(cmd,conn,trans,cmdText,cmdParms); intval=cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); trans.Commit(); } } catch { trans.Rollback(); throw; } } } } /// ///执行一条计算查询结果语句,返回查询结果(object)。 /// ///计算查询结果语句 ///查询结果(object) publicstaticobjectGetSingle(stringSQLString,paramsOracleParameter[]cmdParms) { using(OracleConnectionconnection=newOracleConnection(connectionString)) { using(OracleCommandcmd=newOracleCommand()) { try { PrepareCommand(cmd,connection,null,SQLString,cmdParms); objectobj=cmd.ExecuteScalar(); cmd.Parameters.Clear(); if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value))) { returnnull; } else { returnobj; } } catch(System.Data.OracleClient.OracleExceptione) { thrownewException(e.Message); } } } } /// ///执行查询语句,返回OracleDataReader /// ///查询语句 ///OracleDataReader publicstaticOracleDataReaderExecuteReader(stringSQLString,par
stringstrsql=SQLStringList[n].ToString();
if(strsql.Trim().Length>1)
cmd.CommandText=strsql;
cmd.ExecuteNonQuery();
tx.Commit();
tx.Rollback();
///执行带一个存储过程参数的的SQL语句。
///参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
publicstaticintExecuteSql(stringSQLString,stringcontent)
OracleCommandcmd=newOracleCommand(SQLString,connection);
System.Data.OracleClient.OracleParameter myParameter=newSystem.Data.OracleClient.OracleParameter("@content",OracleDbType.NText);
myParameter.Value=content;
cmd.Parameters.Add(myParameter);
finally
cmd.Dispose();
///向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
///图像字节,数据库的字段类型为image的情况
publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs)
OracleCommandcmd=newOracleCommand(strSQL,connection);
System.Data.OracleClient.OracleParameter myParameter=newSystem.Data.OracleClient.OracleParameter("@fs",OracleDbType.Image);
myParameter.Value=fs;
///执行一条计算查询结果语句,返回查询结果(object)。
///计算查询结果语句
///查询结果(object)
publicstaticobjectGetSingle(stringSQLString)
objectobj=cmd.ExecuteScalar();
returnnull;
returnobj;
catch(System.Data.OracleClient.OracleExceptione)
thrownewException(e.Message);
///执行查询语句,返回OracleDataReader
///查询语句
///OracleDataReader
publicstaticOracleDataReaderExecuteReader(stringstrSQL)
OracleConnectionconnection=newOracleConnection(connectionString);
OracleDataReadermyReader=cmd.ExecuteReader();
returnmyReader;
///执行查询语句,返回DataSet
///DataSet
publicstaticDataSetQuery(stringSQLString)
DataSetds=newDataSet();
OracleDataAdaptercommand=newOracleDataAdapter(SQLString,connection);
command.Fill(ds,"ds");
catch(System.Data.OracleClient.OracleExceptionex)
thrownewException(ex.Message);
returnds;
#region执行带参数的SQL语句
publicstaticintExecuteSql(stringSQLString,paramsOracleParameter[]cmdParms)
using(OracleCommandcmd=newOracleCommand())
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
cmd.Parameters.Clear();
///SQL语句的哈希表(key为sql语句,value是该语句的OracleParameter[])
publicstaticvoidExecuteSqlTran(HashtableSQLStringList)
using(OracleTransactiontrans=conn.BeginTransaction())
//循环
foreach(DictionaryEntrymyDEinSQLStringList)
string cmdText=myDE.Key.ToString();
OracleParameter[]cmdParms=(OracleParameter[])myDE.Value;
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
trans.Commit();
catch
trans.Rollback();
throw;
publicstaticobjectGetSingle(stringSQLString,paramsOracleParameter[]cmdParms)
publicstaticOracleDataReaderExecuteReader(stringSQLString,par
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1