浏览 2448 次
锁定老帖子 主题:DBHelper的写法
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-08-26
using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; /********************************* * 类名:DBHelper * 功能描述:提供基本数据访问功能 * ******************************/ namespace HotelManager.DAL { public static class DBHelper { //数据库连接属性 private static SqlConnection connection; public static SqlConnection Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["HotelManageConnectionString"].ConnectionString; if (connection == null) { connection = new SqlConnection(connectionString); connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } /// <summary> /// 执行无参SQL语句 /// </summary> public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } /// <summary> /// 执行带参SQL语句 /// </summary> public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } /// <summary> /// 执行无参SQL语句,并返回执行记录数 /// </summary> public static int GetScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } /// <summary> /// 执行有参SQL语句,并返回执行记录数 /// </summary> public static int GetScalar(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } /// <summary> /// 执行无参SQL语句,并返回SqlDataReader /// </summary> public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; } /// <summary> /// 执行有参SQL语句,并返回SqlDataReader /// </summary> public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |