首页 新闻 搜索 专区 学院

超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达

1
[已关闭问题] 关闭于 2012-01-03 10:50

我的代码是这样的,不知道为何 还是出现这样的错误!!

 

  1 using System.Configuration;
2 using System.Data;
3 using System.Data.SqlClient;
4 namespace DB
5 {
6
7 public class Db
8 {
9 static string _ConnectString = ConfigurationManager.AppSettings["connString"].ToString();
10
11 public static string ConnectString
12 {
13 get { return _ConnectString; }
14 set { _ConnectString = value; }
15 }
16
17 public static int runSql(string sql, params SqlParameter[] sps)
18 {
19 int num;
20 SqlConnection conn = new SqlConnection(_ConnectString);
21 try
22 {
23 conn.Open();
24 SqlCommand cmd = new SqlCommand(sql);
25
26 cmd.Connection = conn;
27 if (sps.Length > 0)
28 {
29 cmd.Parameters.AddRange(sps);
30 }
31 num= cmd.ExecuteNonQuery();
32 cmd.Dispose();
33 }
34 catch (System.Exception e)
35 {
36 System.Web.HttpContext.Current.Response.Write(e.Message);
37 throw e;
38 }
39 finally
40 {
41 conn.Close();
42 conn.Dispose();
43 }
44
45 return num;
46 }
47
48
49
50 public static int runProc(string procName, params SqlParameter[] sps)
51 {
52 int num;
53 SqlConnection conn = new SqlConnection(_ConnectString);
54 try
55 {
56 conn.Open();
57 SqlCommand cmd = new SqlCommand();
58
59 cmd.Connection = conn;
60 cmd.CommandText = procName;
61 cmd.CommandType = CommandType.StoredProcedure;
62
63 if (sps.Length > 0)
64 {
65 cmd.Parameters.AddRange(sps);
66 }
67 num = cmd.ExecuteNonQuery();
68 cmd.Dispose();
69 }
70 catch (System.Exception e)
71 {
72 throw e;
73 }
74 finally
75 {
76 conn.Close();
77 conn.Dispose();
78 }
79 return num;
80 }
81
82
83 public static object getVar(string sql, params SqlParameter[] sps)
84 {
85 object o;
86 SqlConnection conn = new SqlConnection(_ConnectString);
87 try
88 {
89 conn.Open();
90 SqlCommand cmd = new SqlCommand(sql);
91
92 cmd.Connection = conn;
93 if (sps.Length > 0)
94 {
95 cmd.Parameters.AddRange(sps);
96 }
97 o = cmd.ExecuteScalar();
98 cmd.Dispose();
99 }
100 catch (System.Exception e)
101 {
102
103 throw e;
104 }
105 finally
106 {
107 conn.Close();
108 conn.Dispose();
109 }
110 return o;
111 }
112
113 public static DataRow getLine(string sql,params SqlParameter[] sps)
114 {
115 DataRow dr = null;
116 DataTable dt = getData(sql, sps);
117 if (dt.Rows.Count > 0)
118 {
119 dr = dt.Rows[0];
120 }
121 return dr;
122 }
123
124 public static object lastId(string tableName)
125 {
126 return getVar("select IDENT_CURRENT('" + tableName + "')");
127 }
128
129 public static object lastId()
130 {
131 return getVar("select @@IDENTITY");
132 }
133
134 public static DataTable getData(string sql, params SqlParameter[] sps)
135 {
136 DataTable dt = new DataTable();
137 SqlConnection conn = new SqlConnection(_ConnectString);
138 try
139 {
140 conn.Open();
141 SqlCommand cmd = new SqlCommand(sql);
142
143 cmd.Connection = conn;
144 if (sps.Length > 0)
145 {
146 cmd.Parameters.AddRange(sps);
147 }
148
149 SqlDataAdapter sda = new SqlDataAdapter();
150
151 sda.SelectCommand = cmd;
152 sda.Fill(dt);
153 sda.Dispose();
154 cmd.Dispose();
155 }
156 catch (System.Exception e)
157 {
158 throw e;
159 }
160 finally
161 {
162 conn.Close();
163 conn.Dispose();
164 }
165 return dt;
166 }
167
168 public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] sps)
169 {
170 SqlConnection conn = new SqlConnection(_ConnectString);
171 try
172 {
173 conn.Open();
174 SqlCommand cmd = new SqlCommand(sql);
175 cmd.Connection = conn;
176 if (sps.Length > 0)
177 {
178 cmd.Parameters.AddRange(sps);
179 }
180 return cmd.ExecuteReader(CommandBehavior.CloseConnection);
181 }
182 catch (System.Exception e)
183 {
184 conn.Close();
185 conn.Dispose();
186 throw e;
187 }
188 }
189
190 }
191 }
fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2011-11-16 11:14
< >
分享
所有回答(2)
0

没有释放连接的操作出现你最后一个函数

public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] sps)

调用方在使用完SqlDataReader 后,应该Close,以及Close,SqlDataReader所持有的SqlConnection.

Launcher | 园豆:45045 (高人七级) | 2011-11-16 11:57
 1             SqlDataReader sdr;
2 string word=dt.Rows[i]["word"].ToString();
3 sdr = Db.ExecuteReader("select softid,softname from [SoftDown_SoftInfo] where softname like '%'+@kwd+'%' order by weekhits desc", new SqlParameter("@kwd", word));
4 while (sdr.Read())
5 {
6
7 gsb.Append(",");
8 gsb.Append(sdr.GetInt32(0));
9 Response.Write(sdr.GetString(1)+"<br />");
10
11 }
12
13 sdr.Close();
14 sdr.Dispose();

是关闭掉的啊???

支持(0) 反对(0) fun5 | 园豆:4 (初学一级) | 2011-11-16 16:16

查看sqlserver 进程 中 不一会就有 200 多个 进程 

支持(0) 反对(1) fun5 | 园豆:4 (初学一级) | 2011-11-16 16:22
0

ExecuteReader 这个方法调用以后会返回SqlDataReader  

你在调用方有没有dispose这个返回的SqlDataReader ?

chenping2008 | 园豆:9836 (大侠五级) | 2011-11-17 09:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册