我是个菜鸟,请各位大哥帮帮忙帮我看看,小妹不胜感激
代码:
/// <summary>
/// DBOperate 的摘要说明
/// </summary>
public class DBOperate
{
public DBOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private SqlConnection conn=null;
private SqlCommand cmd=null;
private SqlDataReader dr = null;
private SqlDataAdapter adp = null;
private DataSet ds = null;
private DataTable dt = null;
//connect db
public SqlConnection DBConnection()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShopConnectionString"].ConnectionString);
return conn;
}
//close db , dispose resources
public void DBClose()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
//create command
public SqlCommand createCommand(string str, CommandType cmdType)
{
cmd = new SqlCommand();
cmd.Connection = DBConnection();
cmd.CommandText = str;
cmd.CommandType = cmdType;
return cmd;
}
//create command with sqlParameters
public SqlCommand createCommand(string str, CommandType cmdType, SqlParameter[] param)
{
cmd = new SqlCommand();
cmd.Connection = DBConnection();
cmd.CommandText = str;
cmd.CommandType = cmdType;
foreach (SqlParameter par in param)
{
cmd.Parameters.Add(par);
}
return cmd;
}
//execute dataReader
public SqlDataReader executeReader(string str, CommandType cmdType)
{
conn = DBConnection();
try
{
conn.Open();
cmd = createCommand(str, cmdType);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //if transfers dr.Close(), then will also transfer conn.Close()
}
catch (Exception ee)
{
throw ee;
}
return dr;
}
//execute dataReader with sqlParameters
public SqlDataReader executeReader(string str, CommandType cmdType, SqlParameter [] param)
{
conn = DBConnection();
try
{
conn.Open();
cmd = createCommand(str, cmdType, param);
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //if transfers dr.Close(), then will also transfer conn.Close()
}
catch (Exception ee)
{
throw ee;
}
return dr;
}
//execute dataSet
public DataSet executeDataSet(string str, CommandType cmdType)
{
conn = DBConnection();
try
{
conn.Open();
cmd = createCommand(str, cmdType);
adp = new SqlDataAdapter(cmd);
ds = new DataSet();
adp.Fill(ds);
}
catch (Exception ee)
{
throw ee;
}
finally
{
conn.Close();