sql;">
/*xcy于11.09
修改,原
sqlserver的DBHelper*/
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
namespace JGS.Utility
{
public class
sqlHelper
{
//连接字符串
static string strConn = ConfigurationManager.ConnectionStrings["xcyCon"].ToString();
#region 执行
查询,返回DataTable对象-----------------------
public static DataTable GetTable(string str
sql)
{
return GetTable(str
sql,null);
}
public static DataTable GetTable(string str
sql,OracleParameter[] pas)
{
return GetTable(str
sql,pas,CommandType.Text);
}
///
/// 执行查询,返回DataTable对象
///
///
sql">
sql语句
///
///
///
DataTable对象
public static DataTable GetTable(string str
sql,OracleParameter[] pas,CommandType cmdtype)
{
DataTable dt = new DataTable(); ;
using (OracleConnection conn = new OracleConnection(strConn))
{
OracleDataAdapter da = new OracleDataAdapter(str
sql,conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 执行
查询,返回DataSet对象-------------------------
public static DataSet GetDataSet(string str
sql)
{
return GetDataSet(str
sql,null);
}
public static DataSet GetDataSet(string str
sql,OracleParameter[] pas)
{
return GetDataSet(str
sql,CommandType.Text);
}
///
/// 执行查询,返回DataSet对象
///
///
sql">
sql语句
///
///
///
DataSet对象
public static DataSet GetDataSet(string str
sql,CommandType cmdtype)
{
DataSet dt = new DataSet();
using (OracleConnection conn = new OracleConnection(strConn))
{
OracleDataAdapter da = new OracleDataAdapter(str
sql,conn);
da.SelectCommand.CommandType = cmdtype;
if (pas != null)
{
da.SelectCommand.Parameters.AddRange(pas);
}
da.Fill(dt);
}
return dt;
}
#endregion
#region 执行非
查询存储过程和
sql语句-----------------------------
public static int ExcuteProc(string ProcName)
{
return Excute
sql(ProcName,null,CommandType.StoredProcedure);
}
public static int ExcuteProc(string ProcName,OracleParameter[] pars)
{
return Excute
sql(ProcName,pars,CommandType.StoredProcedure);
}
public static int Excute
sql(string str
sql)
{
return Excute
sql(str
sql,null);
}
public static int Excute
sql(string str
sql,OracleParameter[] paras)
{
return Excute
sql(str
sql,paras,CommandType.Text);
}
/// 执行非
查询存储过程和
sql语句
/// 增、删、改
///
///
sql">要执行的
sql语句
///
///
///
返回影响行数
public static int Excute
sql(string str
sql,OracleParameter[] paras,CommandType cmdType)
{
int i = 0;
using (OracleConnection conn = new OracleConnection(strConn))
{
OracleCommand cmd = new OracleCommand(str
sql,conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
return i;
}
#endregion
#region 执行
查询返回第一行,第一列---------------------------------
public static int ExcuteScalar
sql(string str
sql)
{
return ExcuteScalar
sql(str
sql,null);
}
public static int ExcuteScalar
sql(string str
sql,OracleParameter[] paras)
{
return ExcuteScalar
sql(str
sql,CommandType.Text);
}
public static int ExcuteScalarProc(string str
sql,CommandType.StoredProcedure);
}
///
/// 执行sql语句,返回第一行,第一列
///
///
sql">要执行的
sql语句
///
///
返回影响行数
public static int ExcuteScalar
sql(string str
sql,conn);
cmd.CommandType = cmdType;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
i = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
}
return i;
}
#endregion
#region
查询获取单个值------------------------------------
///
/// 调用不带参数的存储过程获取单个值
///
///
///
public static object GetObjectByProc(string ProcName)
{
return GetObjectByProc(ProcName,null);
}
///
/// 调用带参数的存储过程获取单个值
///
///
///
///
public static object GetObjectByProc(string ProcName,OracleParameter[] paras)
{
return GetObject(ProcName,CommandType.StoredProcedure);
}
///
/// 根据sql语句获取单个值
///
///
sql">
///
public static object GetObject(string str
sql)
{
return GetObject(str
sql,null);
}
///
/// 根据sql语句 和 参数数组获取单个值
///
///
sql">
///
///
public static object GetObject(string str
sql,OracleParameter[] paras)
{
return GetObject(str
sql,CommandType.Text);
}
///
/// 执行sql语句,返回首行首列
///
///
sql">要执行的
sql语句
///
///
返回的首行首列
public static object GetObject(string str
sql,CommandType cmdtype)
{
object o = null;
using (OracleConnection conn = new OracleConnection(strConn))
{
OracleCommand cmd = new OracleCommand(str
sql,conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
o = cmd.ExecuteScalar();
conn.Close();
}
return o;
}
#endregion
#region
查询获取DataReader------------------------------------
///
/// 调用不带参数的存储过程,返回DataReader对象
///
///
名称
///
DataReader对象
public static OracleDataReader GetReaderByProc(string procName)
{
return GetReaderByProc(procName,null);
}
///
/// 调用带有参数的存储过程,返回DataReader对象
///
///
///
///
DataReader对象
public static OracleDataReader GetReaderByProc(string procName,OracleParameter[] paras)
{
return GetReader(procName,CommandType.StoredProcedure);
}
///
/// 根据sql语句返回DataReader对象
///
///
sql">
sql语句
///
DataReader对象
public static OracleDataReader GetReader(string str
sql)
{
return GetReader(str
sql,null);
}
///
/// 根据sql语句和参数返回DataReader对象
///
///
sql">
sql语句
///
///
DataReader对象
public static OracleDataReader GetReader(string str
sql,OracleParameter[] paras)
{
return GetReader(str
sql,CommandType.Text);
}
///
/// 查询sql语句获取DataReader
///
///
sql">
查询的
sql语句
///
///
查询到的DataReader(关闭该对象的时候,自动关闭连接)
public static OracleDataReader GetReader(string str
sql,CommandType cmdtype)
{
OracleDataReader
sqldr = null;
OracleConnection conn = new OracleConnection(strConn);
OracleCommand cmd = new OracleCommand(str
sql,conn);
cmd.CommandType = cmdtype;
if (paras != null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
//CommandBehavior.CloseConnection的作用是如果关联的DataReader对象
关闭,则连接
自动关闭
sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return
sqldr;
}
#endregion
/*
#region 批量插入数据---------------------------------------------
///
/// 往数据库中批量插入数据
///
///
///
public static void BulkToDB(DataTable sourceDt,string targetTable)
{
OracleConnection conn = new OracleConnection(strConn);
sqlBulkCopy bulkCopy = new
sqlBulkCopy(conn); //用其它源的数据有效批量加载
sql server表中
bulkCopy.DestinationTableName = targetTable; //服务器上目标表的
名称
bulkCopy.BatchSize = sourceDt.Rows.Count; //每一批次中的行数
try
{
conn.Open();
if (sourceDt != null && sourceDt.Rows.Count != 0)
bulkCopy.WriteToServer(sourceDt); //将提供的数据源中的所有行复制到目标表中
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
if (bulkCopy != null)
bulkCopy.Close();
}
}
#endregion
*/
}
}