• DBHelper.CS


    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.Common;
    using System.Collections.Generic;
    
    //引用后,直接修改与项目相同的命名空间就可直接使用:DXApplication5
    namespace DXApplication5
    {
    
        class DBHelper
        {
            
            //数据库链接字符串
            public static string ConnString = "Data Source = 127.0.0.1;Initial Catalog = PulseSterilizer;User Id = sa;Password =sa;";
    
            //建立数据库链接
            public static SqlConnection conn = null;
            private static void InitConnection()
            {
                if (conn == null)
                    conn = new SqlConnection(ConnString);
                if (conn.State == ConnectionState.Broken)
                {
                    conn.Close();
                    conn.Open();
                }
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
    
            }
    
            //获取DataReader
            public static SqlDataReader GetDataReader(string str)
            {
                InitConnection();
                SqlCommand cmd = new SqlCommand(str, conn);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
    
            //查询,获取DataSet
            public static DataSet GetDataSet(string sqlStr)
            {
                InitConnection();
                DataSet ds = new DataSet();
                SqlDataAdapter dap = new SqlDataAdapter(sqlStr, conn);
                dap.Fill(ds);
                conn.Close();
                return ds;
            }
    
            //查询,获取DataTable
            public static DataTable GetDataTable(string sqlStr)
            {
                return GetDataSet(sqlStr).Tables[0];
            }
    
            //增删改
            public static int ExecuteNonQuery(string str)
            {
                InitConnection();
                SqlCommand cmd = new SqlCommand(str, conn);
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                return result;
            }
    
            //执行集合函数
            public static object ExecuteScalar(string str)
            {
                InitConnection();
                SqlCommand cmd = new SqlCommand(str, conn);
                object result = cmd.ExecuteScalar();
                conn.Close();
                return result;
            }
        }
    
    }
  • 相关阅读:
    引入其他字体库 和 字体样式设置
    onmousemove鼠标截取
    最大、最小值
    ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法
    时间戳
    JS性能优化 -- 函数节流
    css兼容问题
    上传文件(ajax结合form表单)
    文件下载
    input上传指定类型的文件
  • 原文地址:https://www.cnblogs.com/Bung/p/14278751.html
Copyright © 2020-2023  润新知