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());
                }
            }

        }
    }

  • 相关阅读:
    正则表达式随笔
    linux 命令大全
    oracle merge into
    存储过程else if
    存储过程 loop
    存储过程 函数
    存储过程使用集合来存储查询
    存储过程使用游标和索引
    存储过程使用%rowtype定义游标类型的变量提取emp数据
    ORACLE 存储过程 like 样例
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1536412.html
Copyright © 2011-2022 走看看