DbHelper数据库通用类使用方法

系统 2014 0
      
        代码



    
      
      
        //
      
      
        执行SQL语句
      
      
        public
      
      
        static
      
      
        void
      
      
         ExecSqlCommand()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetSqlStringCommond(
      
        "
      
      
        select * from t1_insert
      
      
        "
      
      
        );

        db.ExecuteNonQuery(command);

    }



    
      
      
        //
      
      
        执行存储过程
      
      
        public
      
      
        static
      
      
        void
      
      
         ExecStoredProcedure()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetStoredProcCommond(
      
        "
      
      
        t1_insert
      
      
        "
      
      
        );

        db.AddInParameter(command, 
      
      
        "
      
      
        @id
      
      
        "
      
      , DbType.String, 
      
        "
      
      
        heihei
      
      
        "
      
      
        );

        db.AddInParameter(command, 
      
      
        "
      
      
        @id
      
      
        "
      
      , DbType.String, 
      
        "
      
      
        heihei
      
      
        "
      
      
        );

        db.ExecuteNonQuery(command);

    }



    
      
      
        //
      
      
        返回DataTable
      
      
        public
      
      
        static
      
      
        void
      
      
         GetDataTable()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetSqlStringCommond(
      
        "
      
      
        t1_findall
      
      
        "
      
      
        );

        DataTable dt 
      
      =
      
         db.ExecuteDataTable(command);

    }



    
      
      
        //
      
      
        获取各种返回值
      
      
        public
      
      
        static
      
      
        void
      
      
         GetAllParameter()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetStoredProcCommond(
      
        "
      
      
        t2_insert
      
      
        "
      
      
        );



        db.AddInParameter(command, 
      
      
        "
      
      
        @timeticks
      
      
        "
      
      
        , DbType.Int64, DateTime.Now.Ticks);

        db.AddOutParameter(command, 
      
      
        "
      
      
        @outString
      
      
        "
      
      , DbType.String, 
      
        20
      
      
        );

        db.AddReturnParameter(command, 
      
      
        "
      
      
        @returnValue
      
      
        "
      
      
        , DbType.Int32);

        db.ExecuteNonQuery(command);



        
      
      
        string
      
       s = db.GetParameter(command, 
      
        "
      
      
        @outString
      
      
        "
      
      ).Value 
      
        as
      
      
        string
      
      ;
      
        //
      
      
        out parameter
      
      
        int
      
       r = Convert.ToInt32(db.GetParameter(command, 
      
        "
      
      
        @returnValue
      
      
        "
      
      ).Value);
      
        //
      
      
        return value
      
      
            }



    
      
      
        //
      
      
        DateReader操作
      
      
        public
      
      
        static
      
      
        void
      
      
         GetDataReader()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetStoredProcCommond(
      
        "
      
      
        t2_insert
      
      
        "
      
      
        );



        db.AddInParameter(command, 
      
      
        "
      
      
        @timeticks
      
      
        "
      
      
        , DbType.Int64, DateTime.Now.Ticks);

        db.AddOutParameter(command, 
      
      
        "
      
      
        @outString
      
      
        "
      
      , DbType.String, 
      
        20
      
      
        );

        db.AddReturnParameter(command, 
      
      
        "
      
      
        @returnValue
      
      
        "
      
      
        , DbType.Int32);



        
      
      
        using
      
       (DbDataReader reader =
      
         db.ExecuteReader(command))

        {

            
      
      
        //
      
      
        dt.Load(reader);
      
      
                }

        
      
      
        string
      
       s = db.GetParameter(command, 
      
        "
      
      
        @outString
      
      
        "
      
      ).Value 
      
        as
      
      
        string
      
      ;
      
        //
      
      
        out parameter
      
      
        int
      
       r = Convert.ToInt32(db.GetParameter(command, 
      
        "
      
      
        @returnValue
      
      
        "
      
      ).Value);
      
        //
      
      
        return value
      
      
            }

    

    
      
      
        //
      
      
        获取DataSet
      
      
        public
      
      
        static
      
      
        void
      
      
         GetDataSet()

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand command 
      
      = db.GetSqlStringCommond(
      
        "
      
      
        select * from t1
      
      
        "
      
      
        );

        DataSet ds 
      
      =
      
         db.ExecuteDataSet(command);

    }



    
      
      
        //
      
      
        事务的使用.(项目中需要将基本的数据库操作组合成一个完整的业务流时,代码级的事务是必不可少的哦)
      
      
        public
      
      
        void
      
      
         DoBusiness()

    {

        
      
      
        using
      
       (Trans t = 
      
        new
      
      
         Trans())

        {

            
      
      
        try
      
      
        

            {

                D1(t);

                
      
      
        throw
      
      
        new
      
       Exception();
      
        //
      
      
        如果有异常,会回滚滴
      
      
                        D2(t);

                t.Commit();

            }

            
      
      
        catch
      
      
        

            {

                t.RollBack();

            }

        }

    }



    
      
      
        public
      
      
        void
      
      
         D1(Trans t)

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand cmd 
      
      = db.GetStoredProcCommond(
      
        "
      
      
        t2_insert
      
      
        "
      
      
        );



        db.AddInParameter(cmd, 
      
      
        "
      
      
        @timeticks
      
      
        "
      
      
        , DbType.Int64, DateTime.Now.Ticks);

        db.AddOutParameter(cmd, 
      
      
        "
      
      
        @outString
      
      
        "
      
      , DbType.String, 
      
        20
      
      
        );

        db.AddReturnParameter(cmd, 
      
      
        "
      
      
        @returnValue
      
      
        "
      
      
        , DbType.Int32);

 

        
      
      
        if
      
       (t == 
      
        null
      
      
        ) 

            db.ExecuteNonQuery(cmd);

        
      
      
        else
      
      
         db.ExecuteNonQuery(cmd,t);

 

        
      
      
        string
      
       s = db.GetParameter(cmd, 
      
        "
      
      
        @outString
      
      
        "
      
      ).Value 
      
        as
      
      
        string
      
      ;
      
        //
      
      
        out parameter
      
      
        int
      
       r = Convert.ToInt32(db.GetParameter(cmd, 
      
        "
      
      
        @returnValue
      
      
        "
      
      ).Value);
      
        //
      
      
        return value
      
      
            }



    
      
      
        public
      
      
        void
      
      
         D2(Trans t)

    {

        DbHelper db 
      
      = 
      
        new
      
      
         DbHelper();

        DbCommand cmd 
      
      = db.GetSqlStringCommond(
      
        "
      
      
        insert t1 (id)values(‘..‘)
      
      
        "
      
      
        );       

        
      
      
        if
      
       (t == 
      
        null
      
      
        ) 

            db.ExecuteNonQuery(cmd);

        
      
      
        else
      
      
         db.ExecuteNonQuery(cmd, t);

    }
      
    

其中 DbCommand 位于 using System.Data.Common 命名空间

DbHelper数据库通用类使用方法


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论