社区
C#
帖子详情
关于DBHelper的用法
vitsippa___
2014-11-09 07:08:17
求指点DBHelper怎么引用??
...全文
3200
7
打赏
收藏
关于DBHelper的用法
求指点DBHelper怎么引用??
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
wind_cloud2011
2014-11-12
打赏
举报
回复
你建立一个DbHelper.cs类文件,复制内容: using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections.Generic; namespace DbHelper { public class SqlDbHelper :IDisposable { protected SqlConnection conn; protected SqlCommand cmd; protected SqlDataReader reader; protected SqlDataAdapter adapter; protected string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; public static SqlDbHelper Instance = null; public string ConnectionString { get { return this.connectionString; } set { this.connectionString = value; } } static SqlDbHelper() { SqlDbHelper.Instance = new SqlDbHelper(); } /// <summary> /// 获取一个未打开连接的SqlConnection对象 /// </summary> /// <returns>SqlConnection对象</returns> public SqlConnection GetConnection() { if (conn != null) return this.conn; return this.conn = new SqlConnection(connectionString); } /// <summary> /// 使用连接字符串获取未打开连接SqlConnection对象 /// </summary> /// <param name="_connStr">连接字符串</param> /// <returns>SqlConnection对象</returns> public SqlConnection GetConnection(string _connStr) { if (this.conn != null) this.conn.ConnectionString = _connStr; else this.conn = new SqlConnection(_connStr); return this.conn; } /// <summary> /// 使用指定的Sql语句创建SqlCommand对象 /// </summary> /// <param name="sqlStr">Sql语句</param> /// <returns>SqlCommand对象</returns> private SqlCommand GetCommand(string sqlStr) { if (this.conn == null) this.conn = GetConnection(); if (this.cmd == null) this.cmd = this.GetCommand(sqlStr, CommandType.Text, null); else { this.cmd.CommandText = sqlStr; this.cmd.CommandType = CommandType.Text; this.cmd.Parameters.Clear(); } return this.cmd; } /// <summary> /// 使用指定的Sql语句,CommandType,SqlParameter数组创建SqlCommand对象 /// </summary> /// <param name="sqlStr">Sql语句</param> /// <param name="type">命令类型</param> /// <param name="paras">SqlParameter数组</param> /// <returns>SqlCommand对象</returns> public SqlCommand GetCommand(string sqlStr, CommandType type, SqlParameter[] paras) { if (conn == null) this.conn = this.GetConnection(); if (cmd == null) this.cmd = conn.CreateCommand(); this.cmd.CommandType = type; this.cmd.CommandText = sqlStr; this.cmd.Parameters.Clear(); if (paras != null) this.cmd.Parameters.AddRange(paras); return this.cmd; } /// <summary> /// 执行Sql语句返回受影响的行数 /// </summary> /// <param name="sqlStr">Sql语句</param> /// <returns>受影响的行数,失败则返回-1</returns> public int ExecuteNonQuery(string sqlStr) { int line = -1; try { line = this.ExecuteNonQuery(sqlStr,CommandType.Text,null); } catch (SqlException e) { throw e; } return line; } /// <summary> /// 使用指定的Sql语句,CommandType,SqlParameter数组执行Sql语句,返回受影响的行数 /// </summary> /// <param name="sqlStr">Sql语句</param> /// <param name="type">命令类型</param> /// <param name="paras">SqlParameter数组</param> /// <returns>受影响的行数</returns> public int ExecuteNonQuery(string sqlStr, CommandType type, SqlParameter[] paras) { int line = -1; CheckArgs(sqlStr); if (this.cmd == null) GetCommand(sqlStr, type, paras); this.cmd.Parameters.Clear(); this.cmd.CommandText = sqlStr; this.cmd.CommandType = type; if(paras != null) this.cmd.Parameters.AddRange(paras); try { OpenConn(); line = this.cmd.ExecuteNonQuery(); } catch (SqlException e) { throw e; } return line; } /// <summary> /// 使用指定Sql语句获取dataTable /// </summary> /// <param name="sqlStr">Sql语句</param> /// <returns>DataTable对象</returns> public DataTable GetDataTable(string sqlStr) { CheckArgs(sqlStr); if (this.conn == null) this.conn = GetConnection(); this.adapter = new SqlDataAdapter(sqlStr, this.conn); DataTable table = new DataTable(); try { adapter.Fill(table); } catch (SqlException e) { throw e; } finally { this.adapter.Dispose(); } return table; } /// <summary> /// 使用指定的Sql语句获取SqlDataReader /// </summary> /// <param name="sqlStr">sql语句</param> /// <returns>SqlDataReader对象</returns> public SqlDataReader GetSqlDataReader(string sqlStr) { CheckArgs(sqlStr); if (cmd == null) GetCommand(sqlStr); if(reader != null) reader.Dispose(); try { OpenConn(); this.reader = this.cmd.ExecuteReader(); } catch (SqlException e) { throw e; } return this.reader; } /// <summary> /// 使用事务执行多条Sql语句 /// </summary> /// <param name="sqlCommands">sql语句数组</param> /// <returns>全部成功则返回true否则返回false</returns> public bool ExecuteSqls(List<string> sqlCommands) { if (sqlCommands == null) throw new ArgumentNullException(); if (sqlCommands.Count == 0) throw new ArgumentOutOfRangeException(); if(this.cmd == null) GetCommand(null); SqlTransaction tran = null; try { OpenConn(); tran = this.conn.BeginTransaction(); this.cmd.Transaction = tran; foreach (string sql in sqlCommands) { if (ExecuteNonQuery(sql) == 0) { tran.Rollback(); return false; } } } catch { if (tran != null) tran.Rollback(); throw; } tran.Commit(); return true; } public virtual void Dispose() { if (this.reader != null) { reader.Dispose(); this.reader = null; } if (this.cmd != null) { this.cmd.Dispose(); this.cmd = null; } if (this.conn != null) { this.conn.Dispose(); conn = null; } } protected void OpenConn() { try { if (this.conn.State != ConnectionState.Open) conn.Open(); } catch (SqlException e) { throw e; } } /// <summary> /// 关闭连接 /// </summary> public void CloseConn() { if (this.conn != null && this.conn.State == ConnectionState.Open) this.conn.Close(); } /// <summary> /// 检查Sql语句是否合法 /// </summary> /// <param name="sqlStr">Sql语句</param> protected virtual void CheckArgs(string sqlStr) { if (sqlStr == null) throw new ArgumentNullException(); if (sqlStr.Length == 0) throw new ArgumentOutOfRangeException(); } } } 调用时: DbHelper.SqlDbHelper dh = new DbHelper.SqlDbHelper(); dh.GetConnection(); //建立连接 .....
smthgdin_020
2014-11-11
打赏
举报
回复
你先说你的DBHelper是在哪里。。。。。 本程序集还是其他程序集?还有什么命名空间下。
忘丿殇
2014-11-11
打赏
举报
回复
有源码先编译成dll,然后引用添加 无源码就直接用dll咯。。。
hhhh2010
2014-11-11
打赏
举报
回复
DBHelper是封装好的公共类。
有dll或CS源码两种模式:
dll的话,在VS项目-添加引用,找到dll就添加完成;
cs文件在VS项目,添加类可以添加进去。
添加之后,使用using 引用DBHelper的命名空间
然后就可以使用了
threenewbee
2014-11-09
打赏
举报
回复
如果是dll,项目-添加引用 或者 如果是cs源代码,项目-添加类
江南小鱼
2014-11-09
打赏
举报
回复
DBHelper就是封装好的数据库增删改查的公共类,什么叫怎么引用?
KJ_Wang
2014-11-09
打赏
举报
回复
看不出来在哪?怎么用?谁知道呀!
asp.net
Db
Helper
用法
本文介绍 ASP.NET
Db
Helper
类的基本
用法
,包括数据库连接、SQL 查询、存储过程调用、参数设置、数据集和数据表操作、事务处理等。详细示例覆盖了常见数据库操作需求。
DB
Helper
本文介绍了
DB
Helper
的基本
用法
及其在实际开发中的应用。重点讲解了如何通过
DB
Helper
提升数据库操作效率,并结合JavaScript进行前端数据处理。
DB
Helper
、Data
Helper
、ADO数据库操作类(转)
博客介绍了
DB
Helper
、Data
Helper
、ADO数据库操作类的
用法
,包括查询、分页、取第一行第一列值、新增、带条件新增、更新、事务等操作,还提及参数支持匿名函数、动态对象、JObject、自定义Model等类型。
C#
111,129
社区成员
642,540
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章