1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.SqlClient;
6using System.Configuration;
7namespace MyBookShop.DAL
8{
9 public static class DBHelper
10 {
11 public static SqlConnection connection;
12 public static SqlConnection Connection
13 {
14 get
15 {
16 if (connection == null)
17 {
18 //string connectionString = ConfigurationManager.ConnectionStrings["MyBookShop"].ConnectionString;
19 string strConn = @"Data Source=./SQLEXPRESS;Initial Catalog=MYBOOKSHOP;Integrated Security=True";
20 // string strConn = @"Data Source=MICROSOF-E36F55;integrated security=sspi;database=MyBookShop";
21 connection = new SqlConnection(strConn);
22 connection.Open();
23 }
24 else if (connection.State == ConnectionState.Closed)
25 {
26 connection.Open();
27 }
28 else if (connection.State == ConnectionState.Broken)
29 {
30 connection.Close();
31 connection.Open();
32 }
33 return connection;
34 }
35 }
36 /** <summary>
37 /// 执行增,删,改,的方法
38 /// </summary>
39 /// <param name="commandText">sql,proc</param>
40 /// <param name="commandType">CommandType</param>
41 /// <param name="para"></param>
42 /// <returns>int</returns>
43 public static int ExecuteCommand(string commandText, CommandType commandType,SqlParameter[] para)
44 {
45 SqlCommand cmd = new SqlCommand();
46 cmd.Connection = Connection;
47 cmd.CommandText = commandText;
48 cmd.CommandType = commandType;
49 try
50 {
51 if (para != null)
52 {
53 cmd.Parameters.AddRange(para);
54 }
55 return cmd.ExecuteNonQuery();
56 }
57
58 finally
59 {
60 connection.Close();
61 }
62 }
63 /** <summary>
64 /// 执行查询的方法
65 /// </summary>
66 /// <param name="commandText"></param>
67 /// <param name="commandType"></param>
68 /// <param name="para"></param>
69 /// <returns></returns>
70 public static DataTable GetDataTable(string commandText, CommandType commandType, SqlParameter[] para)
71 {
72 SqlCommand cmd = new SqlCommand();
73 cmd.Connection = Connection;
74 cmd.CommandText = commandText;
75 cmd.CommandType = commandType;
76 try
77 {
78 if (para != null)
79 {
80 cmd.Parameters.AddRange(para);
81 }
82 SqlDataAdapter da = new SqlDataAdapter(cmd);
83 DataTable temp = new DataTable();
84 da.Fill(temp);
85 return temp;
86 }
87 finally
88 {
89 connection.Close();
90 }
91 }
92 /** <summary>
93 /// 执行少量查询的方法
94 /// </summary>
95 /// <param name="commandText"></param>
96 /// <param name="commandType"></param>
97 /// <param name="para"></param>
98 /// <returns></returns>
99 public static SqlDataReader GetReader(string commandText, CommandType commandType, SqlParameter[] para)
100 {
101 SqlCommand cmd = new SqlCommand();
102 cmd.Connection = Connection;
103 cmd.CommandText = commandText;
104 cmd.CommandType = commandType;
105 if (para != null)
106 {
107 cmd.Parameters.AddRange(para);
108 }
109 return cmd.ExecuteReader();
110 }
111 /** <summary>
112 /// 执行聚合函数的方法
113 /// </summary>
114 /// <param name="commandText"></param>
115 /// <param name="commandType"></param>
116 /// <param name="para"></param>
117 /// <returns></returns>
118 public static object GetScalar(string commandText, CommandType commandType, SqlParameter[] para)
119 {
120 SqlCommand cmd = new SqlCommand();
121 cmd.Connection = Connection;
122 cmd.CommandText = commandText;
123 cmd.CommandType = commandType;
124 try
125 {
126 if (para != null)
127 {
128 cmd.Parameters.AddRange(para);
129 }
130 return cmd.ExecuteScalar();
131 }
132 finally
133 {
134 connection.Close();
135 }
136 }
137 }
138}
DBHelper参考
最新推荐文章于 2021-01-26 18:00:25 发布