public class DBHelper { //创建连接字符串 public static string str = "server=.;database=StuDB;Integrated Security=true"; public static SqlConnection conn = null; //增删改 //查询 public static SqlDataReader GetDataReader(string sql) { conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader reader = comm.ExecuteReader(); return comm.ExecuteReader(CommandBehavior.CloseConnection); } //增删改 public static Boolean QuNoerey(string sql) { conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); int num = comm.ExecuteNonQuery(); conn.Close();//关闭数据库链接 return num > 0; } //填充 查询 public static DataTable Query(string sql) { DataTable ds = new DataTable(); conn = new SqlConnection(str); SqlDataAdapter reader = new SqlDataAdapter(sql, conn); reader.Fill(ds); return ds; } //聚合函数 public static object scalar(string sql) { conn = new SqlConnection(str); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); object obj = comm.ExecuteScalar(); return obj; } }