这个asp.net数据库操作类好不好?
using System;using System.Data;
using System.Data.SqlClient;
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;
/// <summary>
/// Index_DataBase 的摘要说明
/// </summary>
public class Index_DataBase
{
private static Index_DataBase index_instance;
public Index_DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static Index_DataBase Index_GetIntance()
{
if (index_instance == null)
{
index_instance = new Index_DataBase();
}
return index_instance;
}
public static SqlConnection Index_ReturnConn()
{
string strConn = System.Configuration.ConfigurationManager.AppSettings["Connection"];
SqlConnection Conn=new SqlConnection(strConn);
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
}
return Conn;
}
public static SqlCommand Index_CreateCmd(string procName,SqlParameter[] prams,SqlConnection Conn)
{
SqlConnection SqlConn = Conn;
if (SqlConn.State.Equals(ConnectionState.Closed))
{
SqlConn.Open();
}
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = SqlConn;
Cmd.CommandText = procName;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
public static SqlCommand Index_CreateCmd(string procName, SqlParameter[] prams)
{
SqlConnection Conn = Index_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 Index_CreateCmd(string procnName, SqlConnection Conn)
{
SqlConnection SqlConn = Conn;
if (SqlConn.State.Equals(ConnectionState.Closed))
{
SqlConn.Open();
}
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = SqlConn;
Cmd.CommandText = procnName;
return Cmd;
}
public static SqlCommand Index_CreateCmd(string procnName)
{
SqlConnection Conn = Index_ReturnConn();
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Connection = Conn;
Cmd.CommandText = procnName;
return Cmd;
}
public static SqlDataReader Index_RunProcGetReader(string procName, SqlParameter[] prams)
{
SqlCommand Cmd = Index_CreateCmd(procName, prams);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
public static SqlDataReader Index_RunProcGetReader(string procName, SqlParameter[] prams,SqlConnection Conn)
{
SqlCommand Cmd = Index_CreateCmd(procName, prams, Conn);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
public static SqlDataReader Index_RunProcGetReader(string procName, SqlConnection Conn)
{
SqlCommand Cmd = Index_CreateCmd(procName, Conn);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
public static SqlDataReader Index_RunProcGetReader(string procName)
{
SqlCommand Cmd = Index_CreateCmd(procName);
SqlDataReader Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
return Dr;
}
public static DataTable Index_RunProcGetTable(string procName, SqlParameter[] prams, SqlConnection Conn)
{
SqlCommand Cmd = Index_CreateCmd(procName,prams, Conn);
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 Index_RunExecute(string procName)
{
SqlConnection Conn = Index_ReturnConn();
SqlCommand Cmd = Index_CreateCmd(procName, Conn);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}
public static int Index_RunExecute(string procName, SqlParameter[] prams)
{
SqlConnection Conn = Index_ReturnConn();
SqlCommand Cmd = Index_CreateCmd(procName,prams, Conn);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}
public static int Index_RunExecuteScalar(string procName)
{
SqlConnection Conn = Index_ReturnConn();
SqlCommand Cmd = Index_CreateCmd(procName, Conn);
int intResult = Convert.ToInt32(Cmd.ExecuteScalar());
Conn.Close();
return intResult;
}
public static int Index_RunExecuteScalar(string procName, SqlParameter[] prams)
{
SqlConnection Conn = Index_ReturnConn();
SqlCommand Cmd = Index_CreateCmd(procName, prams, Conn);
int intResult = Convert.ToInt32(Cmd.ExecuteScalar());
Conn.Close();
return intResult;
}
}