zoukankan      html  css  js  c++  java
  • getcon跟数据库建立连接的方法

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Xml.Linq;
    using System.Collections.Generic;
    using System.Data.SqlClient;

    namespace DAL
    {
        public class GetCon
        {
            private static SqlConnection conn;
            private static SqlDataAdapter da;
            private static SqlCommand com;
            private static SqlDataReader dr;
            private static string strCon;
            public GetCon()
            {

            }
            public static SqlConnection GetConnection()
            {

                strCon =ConfigurationSettings.AppSettings["zhukeConnection"].ToString();
                conn = new SqlConnection(strCon);
                return conn;
            }
            //获得SqlDataReader
            public static SqlDataReader GetReader(string sql)
            {
                SqlConnection con = GetConnection();
                com = new SqlCommand(sql, con);
                con.Open();
                dr = com.ExecuteReader();
                return dr;
            }
            //获得DataSet
            public static DataSet GetDataSet(string sql)
            {
                SqlConnection con = GetConnection();
                da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            //执行的方法
            private static void closeConnection()
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Dispose();
                com.Dispose();
            }
            /// <summary>
            /// 执行一条sql语句
            /// </summary>
            /// <param name="sqlStr">sql语句</param>
            public static void ExecuteSql(string sqlStr)
            {
                try
                {
                    SqlConnection con=GetCon.GetConnection();
                    SqlCommand com = new SqlCommand(sqlStr, con);
                    con.Open();
                    com.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    GetCon.GetClose();
                }
            }
            //关闭相关信息
            public static void GetClose()
            {
                try
                {
                    if (dr != null)
                    {
                        dr.Close();
                    }
                    if (com != null)
                    {
                        com.Dispose();
                    }
                    if (da != null)
                    {
                        da.Dispose();
                    }
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    System.Console.Write(ex.ToString());
                }
            }

        }
    }

  • 相关阅读:
    js中checkbox的全选和反选的实现
    【VS开发】VS2013多字节工程问题uilding an MFC project for a non-Unicode character set is deprecated
    【VS开发】VS2013多字节工程问题uilding an MFC project for a non-Unicode character set is deprecated
    【机器学习】【神经网络与深度学习】不均匀正负样本分布下的机器学习 《讨论集》
    【机器学习】【神经网络与深度学习】不均匀正负样本分布下的机器学习 《讨论集》
    【VS开发】【C++语言】reshuffle的容器实现算法random_shuffle()的使用
    【VS开发】【C++语言】reshuffle的容器实现算法random_shuffle()的使用
    【VS开发】cmd dos 批处理重命名文件
    【VS开发】cmd dos 批处理重命名文件
    【Python开发】【神经网络与深度学习】如何利用Python写简单网络爬虫
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1536412.html
Copyright © 2011-2022 走看看