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

994

被折叠的 条评论
为什么被折叠?



