sqlHealper类,读取数据库,返回DataReader
public
class
SqlHealper
... {
Conn conn = new Conn();
SqlConnection myconn;
public SqlHealper()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// open the database
/// </summary>
public void Open()
...{
if (myconn == null)
...{
myconn = new SqlConnection(conn.connstring());
myconn.Open();
}
if(myconn.State == ConnectionState.Closed)
...{
try
...{
myconn.Open();
}
catch (Exception ex)
...{
SystemError.CreatLog(ex.Message);
}
finally
...{
}
}
}
/**//// <summary>
/// Create a Command with Text
/// </summary>
/// <param name="cmdText">sql text</param>
/// <returns>return a Command</returns>
private SqlCommand CreatSqlCommand(string cmdText)
...{
Open();
SqlCommand cmd = new SqlCommand(cmdText, myconn);
return cmd;
}
/**//// <summary>
/// run commandText and return a datareader
/// </summary>
/// <param name="cmdText">sql text</param>
/// <param name="dataReader"></param>
public void RunCmd(string cmdText,out SqlDataReader dataReader)
...{
//创建SqlCommand
SqlCommand cmd = CreatSqlCommand(cmdText);
//读取数据
try
...{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
...{
//记录错误日志
dataReader = null;
SystemError.CreatLog(ex.Message);
}
finally
...{ }
}
}
... {
Conn conn = new Conn();
SqlConnection myconn;
public SqlHealper()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// open the database
/// </summary>
public void Open()
...{
if (myconn == null)
...{
myconn = new SqlConnection(conn.connstring());
myconn.Open();
}
if(myconn.State == ConnectionState.Closed)
...{
try
...{
myconn.Open();
}
catch (Exception ex)
...{
SystemError.CreatLog(ex.Message);
}
finally
...{
}
}
}
/**//// <summary>
/// Create a Command with Text
/// </summary>
/// <param name="cmdText">sql text</param>
/// <returns>return a Command</returns>
private SqlCommand CreatSqlCommand(string cmdText)
...{
Open();
SqlCommand cmd = new SqlCommand(cmdText, myconn);
return cmd;
}
/**//// <summary>
/// run commandText and return a datareader
/// </summary>
/// <param name="cmdText">sql text</param>
/// <param name="dataReader"></param>
public void RunCmd(string cmdText,out SqlDataReader dataReader)
...{
//创建SqlCommand
SqlCommand cmd = CreatSqlCommand(cmdText);
//读取数据
try
...{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
...{
//记录错误日志
dataReader = null;
SystemError.CreatLog(ex.Message);
}
finally
...{ }
}
}
public
class
GetContent
... {
SqlHealper sqlhealper = new SqlHealper();
public GetContent()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlDataReader GetDG()
...{
SqlDataReader dr;
try
...{
sqlhealper.RunCmd("select * from news where type ='headline'", out dr);
}
catch(Exception ex)
...{
SystemError.CreatLog(ex.Message);
throw new Exception(ex.Message, ex);
}
return (dr);
}
}
... {
SqlHealper sqlhealper = new SqlHealper();
public GetContent()
...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlDataReader GetDG()
...{
SqlDataReader dr;
try
...{
sqlhealper.RunCmd("select * from news where type ='headline'", out dr);
}
catch(Exception ex)
...{
SystemError.CreatLog(ex.Message);
throw new Exception(ex.Message, ex);
}
return (dr);
}
}
前台页面代码
GetContent content
=
new
GetContent();
SqlDataReader dr;
dr = content.GetDG();
this .DataGrid1.DataSource = dr;
this .DataGrid1.DataBind();
SqlDataReader dr;
dr = content.GetDG();
this .DataGrid1.DataSource = dr;
this .DataGrid1.DataBind();