其他oracle 的常用方法:
#region 初始化常用变量
/// <summary>
/// 获取连接字符串
/// </summary>
private readonly string constr = Constant.CON_DATABASE_DB;
private OracleConnection con; //创建SQL连接
private OracleCommand cmd; //创建SQL命令对象
private OracleDataAdapter sda; //创建SQL数据适配器
private OracleDataReader sdr; //创建SQL数据阅读器
private OracleParameter param; //创建SQL参数
private DataTable dt; //创建数据表
#endregion
#region 创建并打开数据库连接
/// <summary>
/// 创建并打开数据库连接
/// </summary>
private void OpenCon()
{
con = new OracleConnection(constr);
con.Open();
}
#endregion
#region 关闭已打开的连接
/// <summary>
/// 关闭已打开的连接
/// </summary>
private void CloseCon()
{
if (con != null)
{
con.Close();
con.Dispose();
}
}
#endregion
#region 返回数据集
/// <summary>
/// 返回数据集
/// </summary>
/// <param name="sqlstring">SQL语句</param>
/// <returns>数据集</returns>
public DataTable GetDt(string sqlString)
{
OpenCon();
sda = new OracleDataAdapter(sqlString, con);
dt = new DataTable();
sda.Fill(dt);
CloseCon();
return dt;
}
#endregion
#region 获取数据视图
/// <summary>
/// 获取数据视图
/// </summary>
/// <param name="sqlstring">SQL语句</param>
/// <returns>数据视图</returns>
public DataView GetDv(string sqlString)
{
return GetDt(sqlString).DefaultView;
}
#endregion
#region 获取阅读器
/// <summary>
/// 获取阅读器
/// </summary>
/// <param name="sqlString">SQL语句</param>
/// <returns>阅读器</returns>
public OracleDataReader GetDataReader(string sqlString)
{
OpenCon();
cmd = new OracleCommand(sqlString, con);
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sdr;
}
#endregion
#region 执行SQL语句
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sqlString">SQL语句</param>
public int ExecSql(string sqlString)
{
OpenCon();
int flag = -20;
cmd = new OracleCommand(sqlString, con);
try
{
flag = (int)cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + ex.Message);
}
CloseCon();
return flag;
}
#endregion
#region 执行带参SQL语句
/// <summary>
/// 执行带参SQL语句
/// </summary>
/// <param name="sqlString">SQL带参语句</param>
/// <returns>执行SQL语句返回值</returns>
public int ExecParmSql(string sqlString, OracleParameter[] parms)
{
cmd = BuildSqlCommand(sqlString, parms);
cmd.ExecuteNonQuery();
CloseCon();
return (int)cmd.Parameters["ReturnValue"].Value;
}
#endregion
#region 执行带参SQL语句,返回数据阅读对象
/// <summary>
/// 执行带参SQL语句,返回数据阅读对象
/// </summary>
/// <param name="sqlString">带参SQL语句</param>
/// <param name="parms">SQL语句参数</param>
/// <returns>数据阅读器</returns>
public OracleDataReader ExecParmSqlReader(string sqlString, OracleParameter[] parms)
{
cmd = BuildSqlCommand(sqlString, parms);
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sdr;
}
#endregion
#region 执行带参SQL语句
/// <summary>
/// 执行带参SQL语句
/// </summary>
/// <param name="sqlString">SQL带参语句</param>
/// <param name="parms">参数</param>
/// <returns>数据集</returns>
public DataTable ExecParmSqlDs(string sqlString, OracleParameter[] parms)
{
cmd = BuildSqlCommand(sqlString, parms);
sda = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
#endregion
#region 执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名</param>
/// <returns>返回值</returns>
public int ExecProc(string procName)
{
cmd = BulidProcCommand(procName, null);
cmd.ExecuteNonQuery();
CloseCon();
return (int)cmd.Parameters["ReturnValue"].Value;
}
#endregion
#region 执行带参数的存储过程
/// <summary>
/// 执行带参数的存储过程
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="parms">存储过程参数</param>
/// <returns>存储过程返回值</returns>
public int ExecProc(string procName, OracleParameter[] parms)
{
cmd = BulidProcCommand(procName, parms);
cmd.ExecuteNonQuery();
CloseCon();
return (int)cmd.Parameters["ReturnValue"].Value;
}
#endregion
#region 执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="dataReader">数据阅读器</param>
public void ExecProc(string procName, OracleDataReader dataReader)
{
cmd = BulidProcCommand(procName, null);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion
#region 执行存储过程
/// <summary>
///执行存储过程
/// </summary>
/// <param name="procName">过程名</param>
/// <param name="parms">过程参数</param>
/// <param name="dataReader">阅读器</param>
public void ExecProc(string procName, OracleParameter[] parms, OracleDataReader dataReader)
{
cmd = BulidProcCommand(procName, parms);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion
#region 执行存储过程
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">过程名</param>
/// <param name="parms">过程参数</param>
/// <returns>返回数据集</returns>
public DataTable ExecProcDs(string procName, OracleParameter[] parms)
{
cmd = BulidProcCommand(procName, parms);
sda = new OracleDataAdapter(cmd);
dt = new DataTable();
sda.Fill(dt);
CloseCon();
return dt;
}
#endregion
#region 生成存储过程命令对象
/// <summary>
/// 生成存储过程命令对象
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="parms">存储过程参数</param>
/// <returns>命令对象</returns>
public OracleCommand BulidProcCommand(string procName, OracleParameter[] parms)
{
OpenCon();
cmd = new OracleCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
if (parms != null)
{
foreach (OracleParameter parameter in parms)
{
cmd.Parameters.Add(parameter);
}
}
cmd.Parameters.Add(new OracleParameter("ReturnValue", OracleType.Int16, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return cmd;
}
#endregion
#region 生成SQL命令对象
/// <summary>
/// 生成SQL命令对象
/// </summary>
/// <param name="sqlstr">SQL带参语句</param>
/// <param name="parms">SQL语句参数</param>
/// <returns>SQL命令对象</returns>
public OracleCommand BuildSqlCommand(string sqlstr, OracleParameter[] parms)
{
OpenCon();
cmd = new OracleCommand(sqlstr, con);
cmd.CommandType = CommandType.Text;
if (parms != null)
{
foreach (OracleParameter parameter in parms)
{
cmd.Parameters.Add(parameter);
}
}
cmd.Parameters.Add(new OracleParameter("ReturnValue", OracleType.Int16, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return cmd;
}
/// <summary>
/// 生成SQL命令对象
/// </summary>
/// <param name="sqlstr">SQL带参语句</param>
/// <param name="parms">SQL语句参数</param>
/// <returns>SQL命令对象</returns>
public OracleCommand BuildSqlCommand(string sqlstr)
{
OpenCon();
cmd = new OracleCommand(sqlstr, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new OracleParameter("ReturnValue", OracleType.Int16, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return cmd;
}
#endregion
#region 生成过程参数
/// <summary>
/// 生成过程参数
/// </summary>
/// <param name="proName">过程名</param>
/// <param name="dbTpye">数据类型</param>
/// <param name="size">数据大小</param>
/// <param name="direction">参数方向</param>
/// <param name="value">参数值</param>
/// <returns>过程参数</returns>
private OracleParameter MakeParm(string parmName, OracleType dbTpye, Int32 size, ParameterDirection direction, object value)
{
if (size > 0)
{
param = new OracleParameter(parmName, dbTpye, size);
}
else
{
param = new OracleParameter(parmName, dbTpye);
}
param.Direction = direction;
if (!(direction == ParameterDirection.Output && value == null))
{
param.Value = value;
}
return param;
}
#endregion
#region 生成存储过程输入参数
/// <summary>
/// 生成存储过程输入参数
/// </summary>
/// <param name="procName">过程名</param>
/// <param name="dbType">参数类型</param>
/// <param name="size">参数大小</param>
/// <param name="value">参数值</param>
/// <returns>过程参数</returns>
public OracleParameter MakeInParm(string parmName, OracleType dbType, Int32 size, object value)
{
return MakeParm(parmName, dbType, size, ParameterDirection.Input, value);
}
#endregion
#region 生成存储过程输出参数
/// <summary>
/// 生成存储过程输出参数
/// </summary>
/// <param name="procName">过程名</param>
/// <param name="dbType">参数类型</param>
/// <param name="size">参数大小</param>
/// <param name="value">参数值</param>
/// <returns>过程参数</returns>
public OracleParameter MakOutParm(string parmName, OracleType dbType, Int32 size)
{
return MakeParm(parmName, dbType, size, ParameterDirection.Output, null);
}
#endregion
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有