asp.net Oracle数据库访问操作类
using system;
using system.collections;
using system.collections.specialized;
using system.data;
using system.data.oracleclient;
using system.configuration;
using system.data.common;
using system.collections.generic;
/// <summary>
/// 数据访问抽象基础类
///
/// </summary>
public class dbbase
{
//数据库连接字符串(web.config来配置),可以动态更改connectionstring支持多数据库.
public static string connectionstring = system.configuration.configurationmanager.connectionstrings["connectionstring1"].tostring();
public dbbase()
{
}
#region 检查用户名是否存在
/// <summary>
/// 检查用户名是否存在,存在返回true,不存在返回false
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public static bool exists(string strsql)
{
using (oracleconnection connection = new oracleconnection(connectionstring))
{
connection.open();
oraclecommand mycmd = new oraclecommand(strsql, connection);
try
{
object obj = mycmd.executescalar(); //返回结果的第一行一列
mycmd.parameters.clear();
if ((object.equals(obj, null)) || (object.equals(obj, system.dbnull.value)))
{
return false;
}
else
{
return true;
}
}
catch (exception ex)
{
throw ex;
}
}
}
#endregion
#region 执行简单sql语句 返回影响的记录数
/// <summary>
/// 执行sql语句,返回影响的记录数
/// </summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>影响的记录数</returns>
public static int executesql(string sqlstring)
{
oracleconnection connection = null;
oraclecommand cmd = null;
try
{
connection = new oracleconnection(connectionstring);
cmd = new oraclecommand(sqlstring, connection);
connection.open();
int rows = cmd.executenonquery();
return rows;
}
finally
{
if (cmd != null)
{
cmd.dispose();
}
if (connection != null)
{
connection.close();
connection.dispose();
}
}
}
#endregion
#region 执行查询语句,返回sqldatareader
/// <summary>
/// 执行查询语句,返回sqldatareader ( 注意:调用该方法后,一定要对sqldatareader进行close )
/// </summary>
/// <param name="strsql">查询语句</param>
/// <returns>sqldatareader</returns>
public static oracledatareader executereader(string strsql)
{
oracleconnection connection = new oracleconnection(connectionstring);
oraclecommand cmd = new oraclecommand(strsql, connection);
try
{
connection.open();
oracledatareader myreader = cmd.executereader(commandbehavior.closeconnection);
return myreader;
}
catch (system.data.oracleclient.oracleexception e)
{
throw e;
}
finally
{
connection.close();
}
}
#endregion
#region 执行sql查询语句,返回datatable数据表
/// <summary>
/// 执行sql查询语句
/// </summary>
/// <param name="sqlstr"></param>
/// <returns>返回datatable数据表</returns>
public static datatable getdatatable(string sqlstr)
{
oracleconnection mycon = new oracleconnection(connectionstring);
oraclecommand mycmd = new oraclecommand(sqlstr, mycon);
datatable dt = new datatable();
oracledataadapter da = null;
try
{
mycon.open();
da = new oracledataadapter(sqlstr, mycon);
da.fill(dt);
}
catch (exception ex)
{
throw new exception(ex.tostring());
}
finally
{
mycon.close();
}
return dt;
}
#endregion
#region 存储过程操作
/// <summary>
/// 运行存储过程,返回datatable;
/// </summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static datatable runproceduredatatable(string storedprocname, idataparameter[] parameters)
{
using (oracleconnection connection = new oracleconnection(connectionstring))
{
dataset ds = new dataset();
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = buildquerycommand(connection, storedprocname, parameters);
sqlda.fill(ds);
connection.close();
return ds.tables[0];
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">参数</param>
/// <returns></returns>
public static int runprocedure(string storedprocname, idataparameter[] parameters)
{
using (oracleconnection connection = new oracleconnection(connectionstring))
{
try
{
connection.open();
oraclecommand command = new oraclecommand(storedprocname, connection);
command.commandtype = commandtype.storedprocedure;
foreach (oracleparameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以dbnull.value.
if ((parameter.direction == parameterdirection.inputoutput || parameter.direction == parameterdirection.input) &&
(parameter.value == null))
{
parameter.value = dbnull.value;
}
command.parameters.add(parameter);
}
}
int rows = command.executenonquery();
return rows;
}
finally
{
connection.close();
}
}
}
/// <summary>
/// 构建 oraclecommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedprocname">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>oraclecommand</returns>
private static oraclecommand buildquerycommand(oracleconnection connection, string storedprocname, idataparameter[] parameters)
{
oraclecommand command = new oraclecommand(storedprocname, connection);
command.commandtype = commandtype.storedprocedure;
foreach (oracleparameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以dbnull.value.
if ((parameter.direction == parameterdirection.inputoutput || parameter.direction == parameterdirection.input) &&
(parameter.value == null))
{
parameter.value = dbnull.value;
}
command.parameters.add(parameter);
}
}
return command;
}
#endregion
#region 事务处理
/// <summary>
/// 执行多条sql语句(list的形式),实现数据库事务。
/// </summary>
/// <param name="sqlstringlist">多条sql语句</param>
/// 调用 transaction 对象的 commit 方法来完成事务,或调用 rollback 方法来取消事务。
public static int executesqltran(list<string> sqlstringlist)
{
using (oracleconnection connection = new oracleconnection(connectionstring))
{
connection.open();
// 为事务创建一个命令
oraclecommand cmd = new oraclecommand();
cmd.connection = connection;
oracletransaction tx = connection.begintransaction();// 启动一个事务
cmd.transaction = tx;
try
{
int count = 0;
for (int n = 0; n < sqlstringlist.count; n++)
{
string strsql = sqlstringlist[n];
if (strsql.trim().length > 1)
{
cmd.commandtext = strsql;
count += cmd.executenonquery();
}
}
tx.commit();//用commit方法来完成事务
return count;//
}
catch
{
tx.rollback();//出现错误,事务回滚!
return 0;
}
finally
{
cmd.dispose();
connection.close();//关闭连接
}
}
}
#endregion
#region 事务处理
/// <summary>
/// 执行多条sql语句(字符串数组形式),实现数据库事务。
/// </summary>
/// <param name="sqlstringlist">多条sql语句</param>
/// 调用 transaction 对象的 commit 方法来完成事务,或调用 rollback 方法来取消事务。
public static int executetransaction(string[] sqlstringlist,int p)
{
using (oracleconnection connection = new oracleconnection(connectionstring))
{
connection.open();
// 为事务创建一个命令
oraclecommand cmd = new oraclecommand();
cmd.connection = connection;
oracletransaction tx = connection.begintransaction();// 启动一个事务
cmd.transaction = tx;
try
{
int count = 0;
for (int n = 0; n < p; n++)
{
string strsql = sqlstringlist[n];
if (strsql.trim().length > 1)
{
cmd.commandtext = strsql;
count += cmd.executenonquery();
}
}
tx.commit();//用commit方法来完成事务
return count;//
}
catch
{
tx.rollback();//出现错误,事务回滚!
return 0;
}
finally
{
cmd.dispose();
connection.close();//关闭连接
}
}
}
#endregion
/// <summary>
/// 执行存储过程获取所需编号(各表主键)
/// </summary>
/// <param name="flowname">存储过程参数</param>
/// <param name="steplen">存储过程参数(默认为1)</param>
/// <returns>编号(各表主键)</returns>
public static string get_flownum(string flowname, int steplen = 1)
{
oracleconnection mycon = new oracleconnection(connectionstring);
try
{
mycon.open();
oraclecommand mycommand = new oraclecommand("alarm_get_flownumber", mycon);
mycommand.commandtype = commandtype.storedprocedure;
mycommand.parameters.add(new oracleparameter("i_flowname", oracletype.varchar, 50));
mycommand.parameters["i_flowname"].value = flowname;
mycommand.parameters.add(new oracleparameter("i_seriesnum", oracletype.number));
mycommand.parameters["i_seriesnum"].value = steplen;
mycommand.parameters.add(new oracleparameter("o_flowvalue", oracletype.number));
mycommand.parameters["o_flowvalue"].direction = parameterdirection.output;
mycommand.executenonquery();
return mycommand.parameters["o_flowvalue"].value.tostring();
}
catch
{
return "";
}
finally
{
mycon.close();
}
}
}