ASP.NET C# 调用存储过程 SQL SERVER 事务 数据库操作类

这篇博客介绍了一个用于ASP.NET C#的数据库操作类,该类支持SQL SERVER的存储过程调用和事务管理。通过示例展示了如何开始事务、提交、回滚以及如何设置存储过程参数,提供了执行SQL语句和存储过程的方法。

我们现在写一个SQL SERVER的数据库简单的操作类。

包括事务,存储过程调用。

类文件如下:

 

 

上面这个类包括常用的操作数据库的方法,下面我们来写几个test用例

 public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [TestMethod]

       //这里演示怎么调用事务,用绑定变量。
        public void TestMethod1()
        {
            DBLib lib = new DBLib();              
            try
            {
                lib.BeginTransaction();//要使用事务,这里必须要先BeginTransaction(),下面执行的方法都要调用带Transaction的方法。
                String sql = "INSERT INTO testtable VALUES(@tid,@text)";
                SqlTypeBean[] bean = new SqlTypeBean[2];
                bean[0] = new SqlTypeBean(false, "@tid", "18", 4, SqlDbType.Int);
                bean[1] = new SqlTypeBean(false, "@text", "good", 50, SqlDbType.VarChar);
                lib.ExecTransactionSql(sql, bean);
                System.Console.WriteLine("ok!");
                sql = "INSERT INTO testtable VALUES(@tid,@text)";

                bean[0] = new SqlTypeBean(false, "@tid", "17", 4, SqlDbType.Int);
                bean[1] = new SqlTypeBean(false, "@text", "good", 50, SqlDbType.VarChar);
                lib.ExecTransactionSql(sql, bean);
                System.Console.WriteLine("ok!");

                lib.CommitTransaction();
            }
            catch (Exception e)
            {
                lib.RollbackTransaction();
                throw e;
            }
        }
        [TestMethod]

       //这里普通的调用
        public void TestMethod2()
        {
            DBLib lib = new DBLib();
            String sql = "INSERT INTO testtable VALUES(@tid,@text)";
            SqlTypeBean[] bean = new SqlTypeBean[2];
            bean[0] = new SqlTypeBean(false, "@tid", "7", 4, SqlDbType.Int);
            bean[1] = new SqlTypeBean(false, "@text", "good", 50, SqlDbType.VarChar);
            lib.ExecSql(sql, bean);
        }

        [TestMethod]

       //这里是带返回值的存储
        public void TestMethod3()
        {
            DBLib lib = new DBLib();
            SqlTypeBean[] bean = new SqlTypeBean[2];
            bean[0] = new SqlTypeBean(true, "@COUNT", "7", 4, SqlDbType.Int);
            bean[1] = new SqlTypeBean(false, "@TEXT", "good", 50, SqlDbType.VarChar);
            SqlCommand cmd = lib.SetParams(bean,"GETCOUNT");
            cmd.ExecuteNonQuery();
            String result = cmd.Parameters["@COUNT"].Value.ToString();
            String result1 = cmd.Parameters["@COUNT"].Value.ToString();
            cmd.Connection.Close();
        }
    }
 

存储存储过程变量的bean

using System;
using System.Collections.Generic;
using System.Text;

namespace SpLib.db
{
    //本类用于存放变量类型
    public class SqlTypeBean
    {
        //这里设定变量是输入变量还是输出变量。默认是输入变量
        private bool IsOutPut = false;      
        //这里存放字段变量的名称
        private String Name;
        //这里存放字段变量的值
        private String Value;
        //这里存放字段的长度
        private int ClumLength;
        //这里存放字段的类型
        private object ClumType;
   

        public SqlTypeBean(bool IsOutPut, String Name, String Value, int ClumLength, object ClumType)
        {
            this.IsOutPut = IsOutPut;
            this.Name = Name;
            this.Value = Value;
            this.ClumLength = ClumLength;
            this.ClumType = ClumType;
           
        }

        public SqlTypeBean( String Name, String Value, int ClumLength, object ClumType)
        {
            this.IsOutPut = false;
            this.Name = Name;
            this.Value = Value;
            this.ClumLength = ClumLength;
            this.ClumType = ClumType;

        }       

        public bool GetIsOutPut()
        {
            return IsOutPut;
        }

        public String GetName()
        {
            return Name;
        }

        public object GetClumType()
        {
            return ClumType;
        }

        public String GetValueString()
        {
            return Value;
        }

        public long GetValueLong()
        {
            return long.Parse(Value);
        }

        public bool GetValueBool()
        {
            return bool.Parse(Value);
        }

        public int GetClumLength()
        {
            return ClumLength;
        }

    }
}

原创文章,转载请标明出处http://blog.csdn.net/keyboardsun

作者 keyboardsun

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值