01 | using System; |
02 | using System.Collections.Generic; |
03 | using System.Linq; |
04 | using System.Text; |
05 | using System.Threading.Tasks; |
06 | using System.Data; |
07 | using System.Data.SqlClient; |
08 |
09 |
10 | namespace DAL |
11 | { |
12 | public class DBHelper |
13 | { |
14 | //创建链接字符串 |
15 | public static string sqlconn = "Data Source=.;Initial Catalog=StudentDB;Integrated Security=True" ; |
16 | //创建链接对象 |
17 | public static SqlConnection conn = new SqlConnection(sqlconn); |
18 |
19 | //增删改方法 |
20 | public static bool ExeQuteNonQuery( string sql) |
21 | { |
22 | //打开链接对象 |
23 | conn.Open(); |
24 | //创建命令对象 |
25 | SqlCommand cmd = new SqlCommand(sql,conn); |
26 | //执行命令对象 |
27 | int result = cmd.ExecuteNonQuery(); |
28 | conn.Close(); |
29 | if (result > 0) |
30 | { |
31 | return true ; |
32 | } |
33 | else { |
34 | return false ; |
35 | } |
36 | } |
37 | //查询方法 |
38 | public DataTable ExeQuteTable( string sql) |
39 | { |
40 | //创建数据适配器 |
41 | SqlDataAdapter da = new SqlDataAdapter(sql, conn); |
42 | DataTable table= new DataTable (); |
43 | da.Fill(table); |
44 | return table; |
45 | } |
46 |
47 | } |
48 | } |