简单数据库连接---1

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

    }
}
DBHelperNew
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Web.Models;

namespace Web.Services
{
    public class UserInfo
    {
        //登录
        public bool SignIn(Models.UserInfo userInfo)
        {
            string sql=string.Format("select Count(1) from UserInfo where UserName='{0}' and UserPwd='{1}'", userInfo.UserName, userInfo.UserPwd);
           // var dr = DBHelper.SelectForScalar(sql);
            var dr = DBHelperNew.SelectForScalar(sql);
            if ((int)dr == 1)
            {
                return true;
            }
            return false;
        }
        //删除
        public bool DeleteUserInfo(Models.UserInfo userInfo)
        {
           // return DBHelper.Update(string.Format("delete from UserInfo where UserName='{0}'", userInfo.UserName));
            return DBHelperNew.Update(string.Format("delete from UserInfo where UserName='{0}'", userInfo.UserName));
        }
        //修改
        public bool UpdateUserInfo(Models.UserInfo userInfo)
        {
           // return DBHelper.Update(string.Format("update UserInfo set UserName='{0}' where UserName='{1}'", userInfo.UserName, userInfo.UserName));
            return DBHelperNew.Update(string.Format("update UserInfo set UserName='{0}' where UserName='{1}'", userInfo.UserName, userInfo.UserName));
        }
        //新增
        public bool AddUserInfo(Models.UserInfo userInfo)
        {
           // return DBHelper.Update(string.Format("insert into  UserInfo values('{0}','{1}')", userInfo.UserName, userInfo.UserPwd));
            return DBHelperNew.Update(string.Format("insert into  UserInfo values('{0}','{1}')", userInfo.UserName, userInfo.UserPwd));
        }
        //查看全部

        public List<Models.UserInfo> UserInfoAll()
        {
            var userInfo = new List<Models.UserInfo>();
            string sql = string.Format(@"select UserName,UserPwd
                                        from UserInfo");
           // var dr = DBHelper.SelectForReader(sql);
            var dr = DBHelperNew.SelectForReader(sql);
            while (dr.Read())
            {
                userInfo.Add(new Models.UserInfo()
                {
                    UserName=dr.GetString(0),
                    UserPwd=dr.GetString(1)
                });
            }
            dr.Close();
            return userInfo;
        }
        //修改后查看所有
        public List<Models.UserInfo> UserInfoAllAndUpdate(string Name)
        {
            var userInfo = new List<Models.UserInfo>();
            string sql = string.Format(@" UPDATE AllInfo SET UserName='{0}'  where UserName='{1}'
                                        select UserName,UserPwd
                                        from UserInfo",Name,Name);
            //var dr = DBHelper.SelectForReader(sql);
            var dr = DBHelperNew.SelectForReader(sql);
            while (dr.Read())
            {
                userInfo.Add(new Models.UserInfo()
                {
                    UserName = dr.GetString(0),
                    UserPwd = dr.GetString(1)
                });
            }
            dr.Close();
            return userInfo;
        }
    }
}
逻辑层

 

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