DBhelper&DBhelper&DBhelper&DBhelper来看看这个有缺陷吗?
这个DBhelper可以吗?有些没用到SqlParameter的方法不安全?谁有更好一点的DBHELPER?
微软官网有DBSqlHelper通用类的。。而不是什么SqlHelper
private static SqlConnection sc;
public static SqlConnection Sc
{
get
{
string StrConWeb = ConfigurationManager.ConnectionStrings["YunNiao"].ConnectionString;
try
{
if (sc == null)
{
sc = new SqlConnection(StrConWeb);
sc.Open();
}
else if (sc.State == ConnectionState.Closed)
{
sc.Open();
}
else if (sc.State == ConnectionState.Broken)
{
sc.Close();
sc.Open();
}
return sc;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
public static int ExeSql(string sql)
{
try
{
SqlCommand cmd = new SqlCommand(sql, Sc);
int i = cmd.ExecuteNonQuery();
return i;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static int ExeSql(string sql, params SqlParameter[] sp)
{
try
{
SqlCommand cmd = new SqlCommand(sql, Sc);
cmd.Parameters.AddRange(sp);
return cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static string ExScala(string sql)
{
try
{
SqlCommand cmd = new SqlCommand(sql, Sc);
string ExeScalar =cmd.ExecuteScalar().ToString();
return ExeScalar;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static SqlDataReader GetSelect(string sql)
{
try
{
SqlCommand cmd = new SqlCommand(sql, Sc);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] para)
{
try
{
SqlCommand cmd = new SqlCommand(sql,Sc);
cmd.Parameters.AddRange(para);
return cmd.ExecuteReader();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static DataTable GetAll(string sql)
{
try
{
SqlDataAdapter sda = new SqlDataAdapter(sql, Sc);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0];
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static DataTable GetTable(string sql, params SqlParameter[] para)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql,Sc);
da.SelectCommand.Parameters.AddRange(para);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}