数据库连接

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Web.Services
{
    class DBHelper
    {
        /// <summary>
        /// 执行增删改查语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static bool Update(string sql)
        {
            var con = new SqlConnection("server=.;database=Practice;uid=sa;pwd=sa");
            con.Open();
            var cmd = con.CreateCommand();
            cmd.CommandText = sql;
            var result = cmd.ExecuteNonQuery();
            con.Close();
            return result > 0;
        }
        /// <summary>
        /// 返回结果表中的第一行的第一个单元格数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object SelectForScalar(string sql)
        {
            var con = new SqlConnection("server=.;database=Practice;uid=sa;pwd=sa");
            con.Open();
            var cmd = con.CreateCommand();
            cmd.CommandText = sql;
            var result = cmd.ExecuteScalar();
            con.Close();
            return result;
        }
        /// <summary>
        /// 返回所有结果
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static SqlDataReader SelectForReader(string sql)
        {
            var con = new SqlConnection("server=.;database=Practice;uid=sa;pwd=sa");
            con.Open();
            var cmd = con.CreateCommand();
            cmd.CommandText = sql;
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
    } 
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web.Services
{
    public class DBHelperNew
    {
        private static SqlConnection connection;

        //public static SqlConnection Connection
        //{
        //    get
        //    {
        //        //类似单例模式
        //        if (connection == null) connection = new SqlConnection
        //             ("server=.;database=Practice;uid=sa;pwd=sa");
        //        return connection;
        //    }
        //}

        public static SqlConnection Connection
        {
            get
            {
                if (connection == null) connection = new SqlConnection(
                       ConfigurationManager.ConnectionStrings["con"].ConnectionString
                       );
                if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                }
                connection.Open();
                return connection;
            }
        }

        public static SqlCommand Command
        {
            get { return Connection.CreateCommand(); }
        }

        public static bool Update(string sql)
        {
            var cmd = Command;
            cmd.CommandText = sql;
            try
            {
                var result = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                return result > 0;
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw;
            }
        }

        public static object SelectForScalar(string sql)
        {
            var cmd = Command;
            cmd.CommandText = sql;
            try
            {
                var result = cmd.ExecuteScalar();
                cmd.Connection.Close();
                return result;
            }
            catch (Exception e)
            {
                cmd.Connection.Close();
                throw;
            }
        }

        public static SqlDataReader SelectForReader(string sql)
        {
            var cmd = Command;
            cmd.CommandText = sql;
            try
            {
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                cmd.Connection.Close();
                throw;
            }
        }

    }
}
View Code

 

posted @ 2019-07-30 15:25  Team_Leading  阅读(127)  评论(0编辑  收藏  举报