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

namespace LibraryMangementSystem.Dao
{
    class DaoUtil
    {
        SqlConnection sc;
        public SqlConnection Connect()
        {
            String str = "Data Source=127.0.0.1;Initial Catalog=Library;User ID=sa;Password=123456;";
            sc = new SqlConnection(str);
            sc.Open();//打开数据库
            return sc;
        }
        public SqlCommand Command(String sql, SqlParameter[] sqlParameters)
        {
            SqlCommand cmd = new SqlCommand(sql, Connect());
            cmd.Parameters.AddRange(sqlParameters);
            return cmd;
        }
        //更新操作
        public int Execute(String sql, SqlParameter[] sqlParameters)
        {
            return Command(sql, sqlParameters).ExecuteNonQuery();
        }
        //无条件的读取操作
        public SqlDataReader Reader(String sql)
        {
             
            return new SqlCommand(sql, Connect()).ExecuteReader();
        }
        //读取操作
        public SqlDataReader Reader(String sql, SqlParameter[] sqlParameters)
        {

            return Command(sql, sqlParameters).ExecuteReader();
        }
        public void DaoClose()
        {
            sc.Close();//关闭数据库连接

        }
    }
}
