• 大小: 3KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-22
  • 语言: C#
  • 标签: c#  DBHelper  

资源简介

c# DBHelper 的通用类 。连接数据库公用类

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace NJCS.Dal
{
    public class DBHelper
    {
        private static OleDbConnection connection;
        public static OleDbConnection Connection
        {
            get
            {
                string connectionString = @“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=web/App_Data/njcs.mdb“;
                if (connection == null)
                {
                    connection = new OleDbConnection(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;
            }
        }


        public static int ExecuteCommand(string safeSql)
        {
            OleDbCommand cmd = new OleDbCommand(safeSql Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static int ExecuteCommand(string sql params OleDbParameter[] values)
        {
            OleDbCommand cmd = new OleDbCommand(sql Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }

        public static int GetScalar(string safeSql)
        {
            OleDbCommand cmd = new OleDbCommand(safeSql Connection);
    

评论

共有 条评论