DbHelper数据库通用类使用方法
代码 //执行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 命名空间
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。