读取数据库类

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
        
{ }
    }



}
 获取内容类
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);
    }

}

前台页面代码

GetContent content  =   new  GetContent();
SqlDataReader dr;
dr 
=  content.GetDG();
this .DataGrid1.DataSource  =  dr;
    
this .DataGrid1.DataBind();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值