用C# 来操作ORACLE 数据库.docx
- 文档编号:11350901
- 上传时间:2023-02-28
- 格式:DOCX
- 页数:15
- 大小:17.44KB
用C# 来操作ORACLE 数据库.docx
《用C# 来操作ORACLE 数据库.docx》由会员分享,可在线阅读,更多相关《用C# 来操作ORACLE 数据库.docx(15页珍藏版)》请在冰豆网上搜索。
用C#来操作ORACLE数据库
用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;
}
else
{
cmdresult=int.Parse(obj.ToString());
}
if(cmdresult==0)
{
returnfalse;
}
else
{
returntrue;
}
}
#endregion
#region 执行简单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语句,实现数据库事务。
///
///
publicstaticvoidExecuteSqlTran(ArrayListSQLStringList)
{
using(OracleConnectionconn=newOracleConnection(connectionString))
{
conn.Open();
OracleCommandcmd=newOracleCommand();
cmd.Connection=conn;
OracleTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
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语句。 /// /// /// /// 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(); } } } /// ///向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// /// /// /// 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)。 /// /// /// 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 /// /// /// 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 /// /// /// 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语句,返回影响的记录数 /// /// /// 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语句,实现数据库事务。 /// /// 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)。 /// /// /// 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 /// /// /// publicstaticOracleDataReaderExecuteReader(stringSQLString,p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 用C# 来操作ORACLE 数据库 C# 操作 ORACLE