using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using MyShoolModels; namespace MyShoolDAL { public class DBhelper { string conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa"; SqlConnection sc; public SqlConnection Sc//连接 { get { if(sc==null) { sc = new SqlConnection(conn); } return sc; } } public void OpenSc() //打开 { if(sc.State==System.Data.ConnectionState.Broken || sc.State==System.Data.ConnectionState.Closed) { sc.Close(); sc.Open(); } } public void CloseSc()//关闭 { if (sc.State == System.Data.ConnectionState.Broken || sc.State == System.Data.ConnectionState.Open) { sc.Close(); } } public int ExecuteQue(string sql,SqlParameter[] paras) { SqlCommand cmd = new SqlCommand(sql,Sc); cmd.Parameters.AddRange(paras); int resul = cmd.ExecuteNonQuery(); return resul; } } }