sql server数据库封装DBHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Model; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace DAL { public class DBHelper { // static string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=four;Integrated Security=True"); //增删改 public int ExeNonQuery(string sql) { //打开数据库 conn.Open(); SqlCommand cmd = new SqlCommand(sql,conn); int i = cmd.ExecuteNonQuery(); conn.Close(); return i; } //聚合函数查询 public object ExeScalar(string sql) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); object i = cmd.ExecuteScalar(); conn.Close(); return i; } //多条数据 public DataTable GetTable(string sql) { SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } }