文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>DbHelper数据库通用类使用方法

DbHelper数据库通用类使用方法

时间:2010-10-13  来源:Ryan_j

代码
    //执行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源码

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载