技术开发 频道

Visual C# 动态操作 SQL Server 数据库实例教程:通用数据访问类(Sq


【IT168技术文档】

  本文介绍的通用数据库访问类,是本人的个人网站实际使用的一个基类,它是一组通用的访问数据库的代码集,在本人网站对数据库的访问绝大部分都使用这一个类。其主要功能有:

  1.判断数据库是否存在?

  2.判断数据库表是否存在?

  3.判断数据库存储过程是否存在?

  4.判断视图是否存在?

  5.自动创建数据库 

  6.自动创建数据库表、存储过程

  7.不带参数的 SQL 语句ExecuteNonQuery的执行方法

  8.执行一条不返回结果的SqlCommand。通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。

  9.执行一条不返回结果的SqlCommand。通过一个已经存在的数据库事件处理

  10执行一条不返回结果的SqlCommand,通过一个已经存在的数据库事物处理

  11.执行一条返回结果集的SqlCommand命令,通过专用的连接字符串。

  12.执行一条返回第一条记录第一列的SqlCommand命令,通过专用的连接字符串。

  13.执行一条返回第一条记录第一列的SqlCommand命令,通过已经存在的数据库连接。

  其代码清单如下:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Collections; using System.Data.SqlClient; /// /// 数据库的通用访问代码 /// 此类为抽象类,不允许实例化,在应用时直接调用即可 /// public abstract class SqlHelper { //获取数据库连接字符串,其属于静态变量且只读,项目中所有文档可以直接使用,但不能修改 public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["MysqldataConnectionString"].ConnectionString; // 哈希表用来存储缓存的参数信息,哈希表可以存储任意类型的参数。 private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// ///判断数据库是否存在 /// 通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。 /// /// /// 使用示例: /// bool databaseExist = SqlHelper.CheckExistsDatebase(dataBaseNameStr); /// /// /// public static bool CheckExistsDatebase(string dataBaseName) { string connstring = SqlHelper.ConnectionStringLocalTransaction; String dataBaseNameStr = "select count(1) From master.dbo.sysdatabases where name='" + dataBaseName + "'"; using (SqlConnection con = new SqlConnection(connstring)) { con.Open(); SqlCommand cmd = new SqlCommand(dataBaseNameStr, con); int result = Convert.ToInt32(cmd.ExecuteScalar()); if (result == 0) { return false; } else { return true; } } } /// 返回页头 ///判断数据库表是否存在 /// 通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。 /// /// /// 使用示例: /// bool databaseExist = SqlHelper.CheckExistsDatebase(dataBaseNameStr); /// /// /// public static bool CheckExistsTable(string dataBaseNameStr, string tablename) { string connstring = "server=“服务器名”;database='" + dataBaseNameStr + "'" + ";Trusted_Connection=SSPI"; String tableNameStr = "select count(1) from sysobjects where name = '" + tablename + "'"; using (SqlConnection con = new SqlConnection(connstring)) { con.Open(); SqlCommand cmd = new SqlCommand(tableNameStr, con); int result = Convert.ToInt32(cmd.ExecuteScalar()); if (result == 0) { return false; } else { return true; } } } /// 返回页头 ///判断数据库存储过程是否存在 /// 通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。 /// /// /// 使用示例: /// bool databaseExist = SqlHelper.CheckExistsDatebase(dataBaseNameStr); /// /// /// /// public static bool CheckExistsProc(string dataBaseNameStr, string procName) { string connstring = "server=“服务器名”;database='" + dataBaseNameStr + "'" + ";Trusted_Connection=SSPI"; //String procNameStr = "select count(1) from sysobjects where name = '" + procName + "'"; String procNameStr = "select count(1) from sysobjects where name = '" + procName + "'" + " AND type = 'P'"; using (SqlConnection con = new SqlConnection(connstring)) { con.Open(); SqlCommand cmd = new SqlCommand(procNameStr, con); int result = Convert.ToInt32(cmd.ExecuteScalar()); if (result == 0) { return false; //不存在 } else { con.Close(); return true; //存在 } } }
0
相关文章