PetShop 数据库 连接类

 
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;

///   <summary>
///  Database 的摘要说明
///   </summary>
public   class  Database
{
    
private  SqlConnection con;

    
//         不带参数的输入存储过程-----默认为输入存储过程
     public   int  RunProc( string  procName)
    {
        SqlCommand cmd 
=  CreateCommand(procName,  null );
        cmd.ExecuteNonQuery();
        
this .Close();
        
return  ( int )cmd.Parameters[ " ReturnValue " ].Value;
    }
    
//         带参数的输入存储过程
     public   int  RunProc( string  procName, SqlParameter[] prams)
    {
        con 
=   new  SqlConnection(ConfigurationSettings.AppSettings[ " createCon " ]);
        
if  (con.State  ==  ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd 
=  CreateCommand(procName, prams);
        cmd.ExecuteNonQuery();
        
this .Close();
        
return  ( int )cmd.Parameters[ " ReturnValue " ].Value;
    }
    
//         输出存储过程
     public   void  RunProc( string  procName,  out  SqlDataReader dataReader)
    {
        SqlCommand cmd 
=  CreateCommand(procName,  null );
        dataReader 
=  cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    }
    
//         带输出参数的存储过程------返回只读数据流
     public   void  RunProc( string  procName, SqlParameter[] prams,  out  SqlDataReader dataReader)
    {
        
//             SqlDataReader dataReader=null;
        SqlCommand cmd  =  CreateCommand(procName, prams);
        dataReader 
=  cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    }

    
public   int  RunProcduce( string  procName, SqlParameter[] prams)
    {
        
int  DeliverNameID  =   0 ;
        SqlCommand cmd 
=  CreateCommand(procName, prams);
        DeliverNameID 
=  Convert.ToInt32(cmd.ExecuteScalar());
        
return  DeliverNameID;
    }
    
//         带输出参数的存储过程------返回 DataTable
     public   void  RunProc( string  procName, SqlParameter[] prams,  out  DataTable dt)
    {
        SqlDataAdapter mySqlDataAdapter 
=   new  SqlDataAdapter();
        mySqlDataAdapter.SelectCommand 
=   new  SqlCommand();
        
if  (prams  !=   null )
        {
            
foreach  (SqlParameter parameter  in  prams)
            {
                mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
            }
        }
        dt 
=   new  DataTable();
        Open();
        mySqlDataAdapter.SelectCommand.Connection 
=  con;
        mySqlDataAdapter.SelectCommand.CommandText 
=  procName;
        mySqlDataAdapter.SelectCommand.CommandType 
=  CommandType.StoredProcedure;
        mySqlDataAdapter.Fill(dt);
    }

    
// 带输出参数的存储过程------返回 DataSet
     public   void  RunProc( string  procName, SqlParameter[] prams,  out  DataSet ds)
    {
        SqlDataAdapter mySqlDataAdapter 
=   new  SqlDataAdapter();
        mySqlDataAdapter.SelectCommand 
=   new  SqlCommand();
        
if  (prams  !=   null )
        {
            
foreach  (SqlParameter parameter  in  prams)
            {
                mySqlDataAdapter.SelectCommand.Parameters.Add(parameter);
            }
        }
        ds 
=   new  DataSet();
        Open();
        mySqlDataAdapter.SelectCommand.Connection 
=  con;
        mySqlDataAdapter.SelectCommand.CommandText 
=  procName;
        mySqlDataAdapter.SelectCommand.CommandType 
=  CommandType.StoredProcedure;
        mySqlDataAdapter.Fill(ds, 
" Tab " );
    }

    
private  SqlCommand CreateCommand( string  procName, SqlParameter[] prams)
    {
        Open();
        SqlCommand cmd 
=   new  SqlCommand(procName, con);
        cmd.CommandType 
=  CommandType.StoredProcedure;
        
//             添加存储过程参数
         if  (prams  !=   null )
        {
            
foreach  (SqlParameter parameter  in  prams)
                cmd.Parameters.Add(parameter);
        }
        
//             返回参数
        cmd.Parameters.Add(
            
new  SqlParameter( " ReturnValue " , SqlDbType.Int,  4 ,
            ParameterDirection.ReturnValue, 
false 0 0 ,
            
string .Empty, DataRowVersion.Default,  null ));

        
return  cmd;
    }
    
//         打开数据库连结
     private   void  Open()
    {
        
if  (con  ==   null )
        {
            con 
=   new  SqlConnection(ConfigurationSettings.AppSettings[ " createCon " ]);
            con.Open();
        }
    }
    
//         关闭数据库连结
     public   void  Close()
    {
        
if  (con  !=   null )
        {
            con.Close();
        }
    }
    
//         释放占有的资源
     public   void  Dispose()
    {
        
if  (con  !=   null )
        {
            con.Dispose();
            con 
=   null ;
        }
    }
    
//         创造输入存储过程
     public  SqlParameter MakeInParam( string  ParamName, SqlDbType DbType,  int  Size,  object  Value)
    {
        
return  MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
    }
    
//         创造输出存储过程
     public  SqlParameter MakeOutParam( string  ParamName, SqlDbType DbType,  int  Size)
    {
        
return  MakeParam(ParamName, DbType, Size, ParameterDirection.Output,  null );
    }
    
//         判断存储过程是输入还是输出
     public  SqlParameter MakeParam( string  ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction,  object  Value)
    {
        SqlParameter param;

        
if  (Size  >   0 )
            param 
=   new  SqlParameter(ParamName, DbType, Size);
        
else
            param 
=   new  SqlParameter(ParamName, DbType);

        param.Direction 
=  Direction;
        
if  ( ! (Direction  ==  ParameterDirection.Output  &&  Value  ==   null ))
            param.Value 
=  Value;

        
return  param;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值