独上高楼网站
  •    你所在位置:首页 数据库SQLSQL开发〉Visual C# 动态操作 SQL Server 数据库实例教程:通用数据访问类(SqlHelper)
  • Visual C# 动态操作 SQL Server 数据库实例教程:通用数据访问类(SqlHelper)
  • 作者:王宏喜  文章来源:独上高楼  发布日期:2008-05-21  浏览次数:1369
  • 打印这篇文章
  • Visual C# 动态操作 SQL Server 数据库实例教程:通用数据访问类(SqlHelper)

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

    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;
    //存在
    }
    }
    }
    /// 返回页头
    ///判断视图是否存在
    ///

    ///
    /// 使用示例:
    /// bool databaseExist = SqlHelper.CheckExistsDatebase(dataBaseNameStr);
    ///

    /// 数据库名
    /// 视图名
    ///
    public static bool CheckExistsView(string dataBaseNameStr, string tablename, string ViewName)
    {
    string connstring = "server=“服务器名”;database='" + dataBaseNameStr + "'" + ";Trusted_Connection=SSPI";
    String viewNameStr = "select count(1) from sysobjects where name = '" + ViewName + "'" + " AND type = 'V'";
    using (SqlConnection con = new SqlConnection(connstring))
    {
    con.Open();
    SqlCommand cmd = new SqlCommand(viewNameStr, con);
    int result = Convert.ToInt32(cmd.ExecuteScalar());
    if (result == 0)
    {
    return false;
    //不存在
    }
    else
    {
    con.Close();
    return true;
    //存在
    }
    }
    }
    /// 返回页头
    ///调用ExecuteNonQuery()执行 创建数据库
    /// 通过指定专用的连接字符串,执行一个不需要返回值的SqlCommand命令。
    ///

    ///
    /// 使用示例:
    /// SqlHelper.CreateSqlDatabase(connstring, "master", CreateStr);
    ///

    /// 数据库连接字符
    /// 数据库名
    /// 创建数据库的Command命令
    ///
    public static bool CreateSqlDatabase(string connstring, string DatabaseName, string CreateStr)
    {
    //
    using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstring))
    {
    SqlCommand Command = new System.Data.SqlClient.SqlCommand(CreateStr, conn);
    conn.Open();
    Command.Connection.ChangeDatabase(DatabaseName);
    try
    {
    Command.ExecuteNonQuery();
    }
    catch (System.Exception ex)
    {
    Command.Connection.Close();
    throw ex;
    }
    finally
    {
    Command.Connection.Close();
    }
    }
    ret
  • 打印这篇文章
  • 与本文主题相关的文章
  • 返回首页