110,998
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private static DataBase _instance;
//私有化构造器
private DataBase()
{
}
//获取DataBase的实例
public static DataBase GetInstance()
{
if (_instance == null)
{
_instance = new DataBase();
}
return _instance;
}
//创建数据库连接,返回一个SqlConnection
public static SqlConnection ReturnConn()
{
string strConn = ConfigurationManager.ConnectionStrings["GDOUSCIMISConn"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
}
return Conn;
}
//根据参数,调用重载方法,执行存储过程,返回一个SqlCommand
public static SqlCommand CreateCmd(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = Conn;
Cmd.CommandText = procName;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
//根据参数调用重载方法,执行存储过程返回DataTable
public static DataTable RunProcGetTable(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataAdapter Dtr = new SqlDataAdapter();
DataSet Ds = new DataSet();
Dtr.SelectCommand = Cmd;
Dtr.Fill(Ds);
DataTable Dt = Ds.Tables[0];
Conn.Close();
return Dt;
}
<connectionStrings>
<add name="GDOUSCIMISConn" connectionString="Data Source=K513CSERVER;Initial Catalog=GDOUSCIMIS;User ID=sa;Password=123456;Max Pool Size = 300;" providerName="System.Data.SqlClient"/>
</connectionStrings>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
private static DataBase _instance;
//私有化构造器
private DataBase()
{
}
//获取DataBase的实例
public static DataBase GetInstance()
{
if (_instance == null)
{
_instance = new DataBase();
}
return _instance;
}
//创建数据库连接,返回一个SqlConnection
public static SqlConnection ReturnConn()
{
string strConn = ConfigurationManager.ConnectionStrings["GDOUSCIMISConn"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
}
return Conn;
}
//根据参数,调用重载方法,执行存储过程,返回一个SqlCommand
public static SqlCommand CreateCmd(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = Conn;
Cmd.CommandText = procName;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
public static SqlCommand CreateCmd(string procName)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = Conn;
Cmd.CommandText = procName;
return Cmd;
}
//根据传入的Sql语句,返回一个SqlCommand
public static SqlCommand CreateSqlCmd(string sqlSting)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = new SqlCommand(sqlSting, Conn);
return Cmd;
}
//根据参数调用重载方法,执行存储过程返回数据集SqlDataReader
public static SqlDataReader RunProcGetReader(string procName, SqlParameter[] prams)
{
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
public static SqlDataReader RunProcGetReader(string procName)
{
SqlCommand Cmd = CreateCmd(procName);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
///根据传入的Sql语句,返回数据集SqlDataReader
public static SqlDataReader RunStringGetReader(string sqlString)
{
SqlCommand Cmd = CreateSqlCmd(sqlString);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
//根据参数调用重载方法,执行存储过程返回DataTable
public static DataTable RunProcGetTable(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataAdapter Dtr = new SqlDataAdapter();
DataSet Ds = new DataSet();
Dtr.SelectCommand = Cmd;
Dtr.Fill(Ds);
DataTable Dt = Ds.Tables[0];
Conn.Close();
return Dt;
}
public static DataTable RunProcGetTable(string procName)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName);
SqlDataAdapter Dtr = new SqlDataAdapter();
DataSet Ds = new DataSet();
Dtr.SelectCommand = Cmd;
Dtr.Fill(Ds);
DataTable Dt = Ds.Tables[0];
Conn.Close();
return Dt;
}
//根据传入的Sql语句,返回数据集DataTable
public static DataTable RunStringGetTable(string sqlString)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateSqlCmd(sqlString);
SqlDataAdapter Dtr = new SqlDataAdapter();
DataSet Ds = new DataSet();
Dtr.SelectCommand = Cmd;
Dtr.Fill(Ds);
DataTable Dt = Ds.Tables[0];
Conn.Close();
return Dt;
}
//根据参数,调用重载方法,执行存储过程,返回受影响的行数
public static int RunExecuteProc(string procName)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}
public static int RunExecuteProc(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}
//根据传入的sql语句,返回受影响的行数
public static int RunExecuteString(string sqlString)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateSqlCmd(sqlString);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}
//根据参数,调用重载方法,执行存储过程,返回首行首列
public static string RunExecuteProcScalar(string procName)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName);
string stringScalar = Convert.ToString(Cmd.ExecuteScalar());
Conn.Close();
return stringScalar;
}
public static string RunExecuteProcScalar(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams);
string stringScalar = Convert.ToString(Cmd.ExecuteScalar());
Conn.Close();
return stringScalar;
}
/// <summary>
/// 根据传入的sql语句,返回首行首列
/// </summary>
/// <param name="sqlString"></param>
/// <returns></returns>
public static string RunExecuteStringScalar(string sqlString)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateSqlCmd(sqlString);
string stringScalar = Convert.ToString(Cmd.ExecuteScalar());
Conn.Close();
return stringScalar;
}
//根据传入的存储过程名和参数,获得分页数据源PagedDataSource
public static PagedDataSource RunProcGetPds(string procName, SqlParameter[] prams)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = RunProcGetTable(procName, prams).DefaultView;
pds.AllowPaging = true;
return pds;
}
//根据传入的存储过程名,获得分页数据源PagedDataSource
public static PagedDataSource RunProcGetPds(string procName)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = RunProcGetTable(procName).DefaultView;
pds.AllowPaging = true;
return pds;
}
//根据传入的sql语句,获得分页数据源PagedDataSource
public static PagedDataSource RunStringGetPds(string sqlString)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = RunStringGetTable(sqlString).DefaultView;
pds.AllowPaging = true;
return pds;
}
/// <summary>
/// 编辑时,根据传过来的id绑定对应的内容显示出来
/// </summary>
/// <returns></returns>
public static DataRowView RunViewGetString(String str)
{
SqlConnection Conn = ReturnConn();
SqlDataAdapter sda = new SqlDataAdapter(str, Conn);
DataSet ds = new DataSet();
sda.Fill(ds,"edittable");
return ds.Tables["edittable"].DefaultView[0];
}
}
//根据参数调用重载方法,执行存储过程返回DataTable
public static DataTable RunProcGetTable(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataAdapter Dtr = new SqlDataAdapter();
DataSet Ds = new DataSet();
Dtr.SelectCommand = Cmd;
Dtr.Fill(Ds);
DataTable Dt = Ds.Tables[0];
Conn.Close();
return Dt;
}