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參考
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.