求解释一下这个DBhelper
namespace Web.DAL{
public class DBHelper
{
private static SqlConnection _conn;
static DBHelper()
{
if (_conn == null)
{
string strConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
_conn = new SqlConnection(strConn);
}
}
public static bool ExecuteSql(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
_conn.Close();
}
}
public static DataTable GetTable(string sql, string tableName)
{
try
{
SqlDataAdapter adp = new SqlDataAdapter(sql, _conn);
DataSet ds = new DataSet();
adp.Fill(ds, tableName);
return ds.Tables[0];
}
catch
{
return null;
}
}
public static DataTable GetTableByPages(string sql, int startIndex,int pagesize,string tableName)
{
try
{
SqlDataAdapter adp = new SqlDataAdapter(sql, _conn);
DataSet ds = new DataSet();
adp.Fill(ds, startIndex, pagesize, tableName);
return ds.Tables[0];
}
catch
{
return null;
}
}
public static SqlDataReader GetReader(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
return null;
}
}
public static object GetScalar(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteScalar();
}
catch
{
return null;
}
finally
{
_conn.Close();
}
} --------------------编程问答-------------------- 自己先理解一些,不明白的拿出来问。 --------------------编程问答-------------------- 我是个菜鸟,基于答辩之用,求大神指导 --------------------编程问答--------------------
--------------------编程问答--------------------
public class DBHelper
{
/// <summary>
/// 数据库连接对象
/// </summary>
private static SqlConnection _conn;
//构造函数
static DBHelper()
{
if (_conn == null)
{
//数据库连接串
string strConn = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
_conn = new SqlConnection(strConn);
}
}
public static bool ExecuteSql(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
//打开数据库连接
_conn.Open();
//创建命令
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
//指定sql语句
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
_conn.Close();
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sql"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public static DataTable GetTable(string sql, string tableName)
{
try
{
//数据适配器对象
SqlDataAdapter adp = new SqlDataAdapter(sql, _conn);
DataSet ds = new DataSet();
//填充数据
adp.Fill(ds, tableName);
return ds.Tables[0];
}
catch
{
return null;
}
}
/// <summary>
/// 分页查询数据
/// </summary>
/// <param name="sql"></param>
/// <param name="startIndex"></param>
/// <param name="pagesize"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public static DataTable GetTableByPages(string sql, int startIndex, int pagesize, string tableName)
{
try
{
SqlDataAdapter adp = new SqlDataAdapter(sql, _conn);
DataSet ds = new DataSet();
adp.Fill(ds, startIndex, pagesize, tableName);
return ds.Tables[0];
}
catch
{
return null;
}
}
/// <summary>
/// 获得DataReader对象
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
return null;
}
}
/// <summary>
/// 获得第一行第一列的值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object GetScalar(string sql)
{
try
{
if (_conn.State != ConnectionState.Open)
_conn.Open();
SqlCommand cmd = _conn.CreateCommand();
cmd.CommandText = sql;
return cmd.ExecuteScalar();
}
catch
{
return null;
}
finally
{
_conn.Close();
}
}
这个详细 --------------------编程问答-------------------- /// <summary>
/// 分页查询数据
/// </summary>
/// <param name="sql"></param>
/// <param name="startIndex"></param>
/// <param name="pagesize"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public static DataTable GetTableByPages(string sql, int startIndex, int pagesize, string tableName)
{
try
{
SqlDataAdapter adp = new SqlDataAdapter(sql, _conn);
DataSet ds = new DataSet();
adp.Fill(ds, startIndex, pagesize, tableName);
return ds.Tables[0];
}
catch
{
return null;
}
}
这种分页效率会有问题吧?
补充:.NET技术 , C#