SqlHelper.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Collections;
12
13 /// <summary>
14 /// 数据库的访问类()
15 /// author:daiwei
16 /// date:09/07/11
17 /// </summary>
18 public abstract class SqlHelper
19 {
20 public SqlHelper()
21 {
22 //
23 // TODO: 在此处添加构造函数逻辑
24 //
25 }
26
27 //从web配置文件中读取数据数据库连接字符串信息,静态只读。
28 public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
29
30 //用哈希表来存储缓存的参数信息,
31 //哈希表可以用来存储各种类型的参数信息
32 //线程同步包装
33 private static Hashtable tmpCache = Hashtable.Synchronized(new Hashtable());
34
35 /// <summary>
36 ///
37 /// </summary>
38 /// <param name="connectionString">数据库连接字符串</param>
39 /// <param name="cmdType">命令执行类型,包括存储过程,sql语句</param>
40 /// <param name="cmdText">SQL</param>
41 /// <param name="commandParameters">以数组的形式提供命令中所用的参数列表</param>
42 /// <returns>返回命令执行后所影响的行数</returns>
43 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
44 {
45 SqlCommand cmd = new SqlCommand();
46 using (SqlConnection conn = new SqlConnection(connectionString))
47 {
48 //通过PrePareCommand方法将参数逐个加到SqlCommand的参数集合中
49 PrePareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
50
51 int val = cmd.ExecuteNonQuery();
52
53 //清空SqlCommand中参数列表
54 cmd.Parameters.Clear();
55 return val;
56 }
57 }
58
59 /// <summary>
60 /// 执行命令,返回受影响的行数
61 /// </summary>
62 /// <param name="connection">数据库连接字符串</param>
63 /// <param name="cmdType">命令类型</param>
64 /// <param name="cmdText">SQL</param>
65 /// <param name="commandParameters">命令参数列表</param>
66 /// <returns>受影响函数</returns>
67 public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
68 {
69 SqlCommand cmd = new SqlCommand();
70 //cmd.Connection = connection;
71 //cmd.CommandText = cmdText;
72 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
73 int val = cmd.ExecuteNonQuery();
74 cmd.Parameters.Clear();
75 return val;
76
77 }
78
79 /// <summary>
80 /// 执行命令,返回受影响函数
81 /// </summary>
82 /// <param name="trans">sql事务</param>
83 /// <param name="cmdType">命令类型</param>
84 /// <param name="cmdText">SQL</param>
85 /// <param name="commandParameters">命令参数列表</param>
86 /// <returns>返回执行命令受影响的行数</returns>
87 public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
88 {
89 SqlCommand cmd = new SqlCommand();
90 PrePareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
91 int val = cmd.ExecuteNonQuery();
92 cmd.Parameters.Clear();
93 return val;
94 }
95
96 /// <summary>
97 /// 执行命令,返回结果集
98 /// </summary>
99 /// <param name="connectionString">数据库连接</param>
100 /// <param name="cmdType">命令类型</param>
101 /// <param name="cmdText">SQL</param>
102 /// <param name="commandParameters">命令参数列表参数的</param>
103 /// <returns>SqlDataReader结果集</returns>
104 public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
105 {
106 SqlCommand cmd = new SqlCommand();
107 SqlConnection conn = new SqlConnection(connectionString);
108
109 //使用try/catch处理
110 //如果发生异常,则SqlDataReader就不存在,
111 //CommandBehavior.CloseConnection就不会执行,异常将被捕获
112 //关闭数据库连接,
113 try
114 {
115 PrePareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
116 SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
117 cmd.Parameters.Clear();
118 return rdr;
119 }
120 catch
121 {
122 conn.Close();
123 throw;
124 }
125 }
126
127 /// <summary>
128 /// 执行命令,返回第一条记录的第一列
129 /// </summary>
130 /// <param name="connectionString">数据库连接字符串</param>
131 /// <param name="cmdType">命令类型</param>
132 /// <param name="cmdText">SQL</param>
133 /// <param name="commandParameters">命令参数列表</param>
134 /// <returns>返回一个Object类型的数据,可以通过ConvertTo(Type)方法转换类型</returns>
135 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
136 {
137 SqlCommand cmd = new SqlCommand();
138 using (SqlConnection connection = new SqlConnection(connectionString))
139 {
140 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
141 object val = cmd.ExecuteScalar();
142 cmd.Parameters.Clear();
143 return val;
144 }
145 }
146
147 /// <summary>
148 /// 执行命令,返回第一条记录的第一列
149 /// </summary>
150 /// <param name="connection">数据库连接字符串</param>
151 /// <param name="cmdType">命令类型</param>
152 /// <param name="cmdText">SQL</param>
153 /// <param name="commandParameters">命令参数列表</param>
154 /// <returns>返回一个Object类型的数据,可以通过Convert.To(Type)方法转换类型</returns>
155 public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
156 {
157 SqlCommand cmd = new SqlCommand();
158
159 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
160 object val = cmd.ExecuteScalar();
161 cmd.Parameters.Clear();
162 return val;
163 }
164
165 /// <summary>
166 /// 缓存参数列表
167 /// </summary>
168 /// <param name="cacheKey">缓存的键值</param>
169 /// <returns>缓存的参数列表</returns>
170 public static SqlParameter[] GetCachedParameters(string cacheKey)
171 {
172 SqlParameter[] cacheParms = (SqlParameter[])tmpCache[cacheKey];
173 if (cacheParms == null)
174 {
175 return null;
176 }
177
178 //新建一个参数的克隆列表
179 SqlParameter[] cloneParms = new SqlParameter[cacheParms.Length];
180
181 //通过循环为克隆参数列表赋值
182 for (int i = 0, j = cacheParms.Length; i < j; i++)
183 {
184 //使用clone方法复制参数列表中的参数
185 cloneParms[i] = (SqlParameter)((ICloneable)cacheParms[i]).Clone();
186 }
187
188 return cloneParms;
189 }
190
191 /// <summary>
192 /// 缓存参数数组
193 /// </summary>
194 /// <param name="cacheKey">参数缓存的键值</param>
195 /// <param name="commandParameters">被缓存的参数列表</param>
196 public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
197 {
198 tmpCache[cacheKey] = commandParameters;
199 }
200
201 /// <summary>
202 /// 为执行命令准备参数
203 /// </summary>
204 /// <param name="cmd">命令</param>
205 /// <param name="conn">数据库连接字符串</param>
206 /// <param name="trans">事务</param>
207 /// <param name="cmdType">命令类型</param>
208 /// <param name="cmdText">SQL</param>
209 /// <param name="cmdParms">返回带参数的命令</param>
210 private static void PrePareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
211 {
212 //判断数据库连接状态
213 if (conn.State != ConnectionState.Open)
214 {
215 //如果数据库连接未打开,则打开数据库连接
216 conn.Open();
217
218 }
219
220 cmd.Connection = conn;
221 cmd.CommandText = cmdText;
222
223 //是否需要事物处理
224 if (trans != null)
225 {
226 cmd.Transaction = trans;
227 }
228
229 //命令类型
230 cmd.CommandType = cmdType;
231
232 //添加参数
233 if (cmdParms != null)
234 {
235 foreach (SqlParameter param in cmdParms)
236 {
237 cmd.Parameters.Add(param);
238 }
239 }
240 }
241 }
242
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Collections;
12
13 /// <summary>
14 /// 数据库的访问类()
15 /// author:daiwei
16 /// date:09/07/11
17 /// </summary>
18 public abstract class SqlHelper
19 {
20 public SqlHelper()
21 {
22 //
23 // TODO: 在此处添加构造函数逻辑
24 //
25 }
26
27 //从web配置文件中读取数据数据库连接字符串信息,静态只读。
28 public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
29
30 //用哈希表来存储缓存的参数信息,
31 //哈希表可以用来存储各种类型的参数信息
32 //线程同步包装
33 private static Hashtable tmpCache = Hashtable.Synchronized(new Hashtable());
34
35 /// <summary>
36 ///
37 /// </summary>
38 /// <param name="connectionString">数据库连接字符串</param>
39 /// <param name="cmdType">命令执行类型,包括存储过程,sql语句</param>
40 /// <param name="cmdText">SQL</param>
41 /// <param name="commandParameters">以数组的形式提供命令中所用的参数列表</param>
42 /// <returns>返回命令执行后所影响的行数</returns>
43 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
44 {
45 SqlCommand cmd = new SqlCommand();
46 using (SqlConnection conn = new SqlConnection(connectionString))
47 {
48 //通过PrePareCommand方法将参数逐个加到SqlCommand的参数集合中
49 PrePareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
50
51 int val = cmd.ExecuteNonQuery();
52
53 //清空SqlCommand中参数列表
54 cmd.Parameters.Clear();
55 return val;
56 }
57 }
58
59 /// <summary>
60 /// 执行命令,返回受影响的行数
61 /// </summary>
62 /// <param name="connection">数据库连接字符串</param>
63 /// <param name="cmdType">命令类型</param>
64 /// <param name="cmdText">SQL</param>
65 /// <param name="commandParameters">命令参数列表</param>
66 /// <returns>受影响函数</returns>
67 public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
68 {
69 SqlCommand cmd = new SqlCommand();
70 //cmd.Connection = connection;
71 //cmd.CommandText = cmdText;
72 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
73 int val = cmd.ExecuteNonQuery();
74 cmd.Parameters.Clear();
75 return val;
76
77 }
78
79 /// <summary>
80 /// 执行命令,返回受影响函数
81 /// </summary>
82 /// <param name="trans">sql事务</param>
83 /// <param name="cmdType">命令类型</param>
84 /// <param name="cmdText">SQL</param>
85 /// <param name="commandParameters">命令参数列表</param>
86 /// <returns>返回执行命令受影响的行数</returns>
87 public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
88 {
89 SqlCommand cmd = new SqlCommand();
90 PrePareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
91 int val = cmd.ExecuteNonQuery();
92 cmd.Parameters.Clear();
93 return val;
94 }
95
96 /// <summary>
97 /// 执行命令,返回结果集
98 /// </summary>
99 /// <param name="connectionString">数据库连接</param>
100 /// <param name="cmdType">命令类型</param>
101 /// <param name="cmdText">SQL</param>
102 /// <param name="commandParameters">命令参数列表参数的</param>
103 /// <returns>SqlDataReader结果集</returns>
104 public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
105 {
106 SqlCommand cmd = new SqlCommand();
107 SqlConnection conn = new SqlConnection(connectionString);
108
109 //使用try/catch处理
110 //如果发生异常,则SqlDataReader就不存在,
111 //CommandBehavior.CloseConnection就不会执行,异常将被捕获
112 //关闭数据库连接,
113 try
114 {
115 PrePareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
116 SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
117 cmd.Parameters.Clear();
118 return rdr;
119 }
120 catch
121 {
122 conn.Close();
123 throw;
124 }
125 }
126
127 /// <summary>
128 /// 执行命令,返回第一条记录的第一列
129 /// </summary>
130 /// <param name="connectionString">数据库连接字符串</param>
131 /// <param name="cmdType">命令类型</param>
132 /// <param name="cmdText">SQL</param>
133 /// <param name="commandParameters">命令参数列表</param>
134 /// <returns>返回一个Object类型的数据,可以通过ConvertTo(Type)方法转换类型</returns>
135 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
136 {
137 SqlCommand cmd = new SqlCommand();
138 using (SqlConnection connection = new SqlConnection(connectionString))
139 {
140 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
141 object val = cmd.ExecuteScalar();
142 cmd.Parameters.Clear();
143 return val;
144 }
145 }
146
147 /// <summary>
148 /// 执行命令,返回第一条记录的第一列
149 /// </summary>
150 /// <param name="connection">数据库连接字符串</param>
151 /// <param name="cmdType">命令类型</param>
152 /// <param name="cmdText">SQL</param>
153 /// <param name="commandParameters">命令参数列表</param>
154 /// <returns>返回一个Object类型的数据,可以通过Convert.To(Type)方法转换类型</returns>
155 public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
156 {
157 SqlCommand cmd = new SqlCommand();
158
159 PrePareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
160 object val = cmd.ExecuteScalar();
161 cmd.Parameters.Clear();
162 return val;
163 }
164
165 /// <summary>
166 /// 缓存参数列表
167 /// </summary>
168 /// <param name="cacheKey">缓存的键值</param>
169 /// <returns>缓存的参数列表</returns>
170 public static SqlParameter[] GetCachedParameters(string cacheKey)
171 {
172 SqlParameter[] cacheParms = (SqlParameter[])tmpCache[cacheKey];
173 if (cacheParms == null)
174 {
175 return null;
176 }
177
178 //新建一个参数的克隆列表
179 SqlParameter[] cloneParms = new SqlParameter[cacheParms.Length];
180
181 //通过循环为克隆参数列表赋值
182 for (int i = 0, j = cacheParms.Length; i < j; i++)
183 {
184 //使用clone方法复制参数列表中的参数
185 cloneParms[i] = (SqlParameter)((ICloneable)cacheParms[i]).Clone();
186 }
187
188 return cloneParms;
189 }
190
191 /// <summary>
192 /// 缓存参数数组
193 /// </summary>
194 /// <param name="cacheKey">参数缓存的键值</param>
195 /// <param name="commandParameters">被缓存的参数列表</param>
196 public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
197 {
198 tmpCache[cacheKey] = commandParameters;
199 }
200
201 /// <summary>
202 /// 为执行命令准备参数
203 /// </summary>
204 /// <param name="cmd">命令</param>
205 /// <param name="conn">数据库连接字符串</param>
206 /// <param name="trans">事务</param>
207 /// <param name="cmdType">命令类型</param>
208 /// <param name="cmdText">SQL</param>
209 /// <param name="cmdParms">返回带参数的命令</param>
210 private static void PrePareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
211 {
212 //判断数据库连接状态
213 if (conn.State != ConnectionState.Open)
214 {
215 //如果数据库连接未打开,则打开数据库连接
216 conn.Open();
217
218 }
219
220 cmd.Connection = conn;
221 cmd.CommandText = cmdText;
222
223 //是否需要事物处理
224 if (trans != null)
225 {
226 cmd.Transaction = trans;
227 }
228
229 //命令类型
230 cmd.CommandType = cmdType;
231
232 //添加参数
233 if (cmdParms != null)
234 {
235 foreach (SqlParameter param in cmdParms)
236 {
237 cmd.Parameters.Add(param);
238 }
239 }
240 }
241 }
242
Default.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>SqlHelper示例</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <asp:HyperLink ID="hlk1" runat="server" NavigateUrl="~/ExcuteSQL.aspx" >简单sql语句</asp:HyperLink>
12 <br />
13 <asp:HyperLink ID="hlk2" runat="server" NavigateUrl="~/ExcuteSQLParm.aspx" >带参数的简单SQL语句</asp:HyperLink>
14 <br />
15 <asp:HyperLink ID="hlk3" runat="server" NavigateUrl="~/ExcuteProc.aspx" >简单存储过程</asp:HyperLink>
16 <br />
17 <asp:HyperLink ID="hlk4" runat="server" NavigateUrl="~/ExcuteProcParm.aspx" >带参数的存储过程</asp:HyperLink>
18 <br />
19 <asp:HyperLink ID="hlk5" runat="server" NavigateUrl="~/ExcuteScalar.aspx" >只返回一个字段</asp:HyperLink>
20 <br />
21 <asp:HyperLink ID="hlk6" runat="server" NavigateUrl="~/ExcuteReader.aspx" >返回一个数据集</asp:HyperLink>
22 <br />
23 </div>
24 <a href="http://www.cnblogs.com/daiweixm">写信给我</a>
25 </form>
26 </body>
27 </html>
28
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>SqlHelper示例</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <asp:HyperLink ID="hlk1" runat="server" NavigateUrl="~/ExcuteSQL.aspx" >简单sql语句</asp:HyperLink>
12 <br />
13 <asp:HyperLink ID="hlk2" runat="server" NavigateUrl="~/ExcuteSQLParm.aspx" >带参数的简单SQL语句</asp:HyperLink>
14 <br />
15 <asp:HyperLink ID="hlk3" runat="server" NavigateUrl="~/ExcuteProc.aspx" >简单存储过程</asp:HyperLink>
16 <br />
17 <asp:HyperLink ID="hlk4" runat="server" NavigateUrl="~/ExcuteProcParm.aspx" >带参数的存储过程</asp:HyperLink>
18 <br />
19 <asp:HyperLink ID="hlk5" runat="server" NavigateUrl="~/ExcuteScalar.aspx" >只返回一个字段</asp:HyperLink>
20 <br />
21 <asp:HyperLink ID="hlk6" runat="server" NavigateUrl="~/ExcuteReader.aspx" >返回一个数据集</asp:HyperLink>
22 <br />
23 </div>
24 <a href="http://www.cnblogs.com/daiweixm">写信给我</a>
25 </form>
26 </body>
27 </html>
28
ExecuteProc.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteProc.aspx.cs" Inherits="ExcuteProc" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>ExecuteProc</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 448px">
12 <tr>
13 <td style="height: 30px">
14 要执行的存储过程的名字(无参数类型)
15 </td>
16 </tr>
17 <tr>
18 <td>
19 <asp:TextBox ID="txtProcNoParms" runat="server" Width="167px" Text="UpdateTable"></asp:TextBox>
20 </td>
21 </tr>
22 <tr>
23 <td>
24 <asp:Button ID="btnProcNoParms" runat="server" Text="执行存储过程" OnClick="btnProcNoParms_Click" />
25 </td>
26 </tr>
27 </table>
28 </div>
29 </form>
30 </body>
31 </html>
32
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteProc.aspx.cs" Inherits="ExcuteProc" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>ExecuteProc</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 448px">
12 <tr>
13 <td style="height: 30px">
14 要执行的存储过程的名字(无参数类型)
15 </td>
16 </tr>
17 <tr>
18 <td>
19 <asp:TextBox ID="txtProcNoParms" runat="server" Width="167px" Text="UpdateTable"></asp:TextBox>
20 </td>
21 </tr>
22 <tr>
23 <td>
24 <asp:Button ID="btnProcNoParms" runat="server" Text="执行存储过程" OnClick="btnProcNoParms_Click" />
25 </td>
26 </tr>
27 </table>
28 </div>
29 </form>
30 </body>
31 </html>
32
ExecuteProc.aspx.cs(示例)
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class ExcuteProc : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnProcNoParms_Click(object sender, EventArgs e)
21 {
22 //获取要执行的命令
23 string sqlExec = txtProcNoParms.Text;
24
25 //定义对象资源的保存范围,一旦usiing范围结束,将释放对方所占的资源
26 using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
27 {
28 //打开连接
29 conn.Open();
30
31 //调用要执行的方法,因为没有参数,最后一项为null
32 SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure , sqlExec, null);
33
34 Response.Write("<font color='red'>操作成功。</Font>");
35 }
36 }
37 }
38
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class ExcuteProc : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnProcNoParms_Click(object sender, EventArgs e)
21 {
22 //获取要执行的命令
23 string sqlExec = txtProcNoParms.Text;
24
25 //定义对象资源的保存范围,一旦usiing范围结束,将释放对方所占的资源
26 using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
27 {
28 //打开连接
29 conn.Open();
30
31 //调用要执行的方法,因为没有参数,最后一项为null
32 SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure , sqlExec, null);
33
34 Response.Write("<font color='red'>操作成功。</Font>");
35 }
36 }
37 }
38