首页 新闻 赞助 找找看

我写了一个静态数据连接类 但是不知道 怎么处理其中的对象

0
悬赏园豆:30 [已解决问题] 解决于 2011-06-02 08:59
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.Data.SqlClient;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12
13 /// <summary>
14 ///database 的摘要说明
15 /// </summary>
16 public static class sqlbase
17 {
18
19 static SqlConnection thiscon2;//处理函数使用的链接
20 static string constr1;
21
22 public static string constr2 = ConfigurationManager.ConnectionStrings["data_smfConnectionString"].ToString();
23 static SqlCommand com,com1,com2,com3 ;
24 static SqlDataAdapter adpater;
25 static DataSet ds;
26 static String rtstr;
27 /// <summary>
28 /// 数据连接字符串
29 /// </summary>
30 /// <param name="_sqlstr"></param>
31 static sqlbase()
32 {
33 sqlconstr1();
34
35 }
36 /// <summary>
37 /// 使用默认1链接
38 /// </summary>
39 public static void sqlconstr1()
40 {
41 constr1 = ConfigurationManager.ConnectionStrings["playerConnectionString"].ToString();
42 }
43 /// <summary>
44 /// 使用默认2链接
45 /// </summary>
46 public static void sqlconstr2()
47 {
48 constr1 = constr2;
49 }
50
51 public static void getcon()
52 {
53
54 thiscon2= new SqlConnection(constr1);
55
56 }
57 public static SqlCommand getcom()
58 {
59
60 com= new SqlCommand();
61 return com;
62
63 }
64 #region p_sqldatabase 成员
65
66 /// <summary>
67 /// 通过SQL语句返回特定数据
68 /// </summary>
69 /// <param name="str"></param>
70 /// <returns></returns>
71 public static DataSet gettable(String str)
72 {
73 getcon();
74 com1 = getcom();
75 com1.Connection = thiscon2;
76 com1.CommandText = str;
77 adpater = new SqlDataAdapter(com);
78 ds = new DataSet();
79 adpater.Fill(ds);
80 closecom(ref com3);
81 return ds;
82 }
83
84 /// <summary>
85 /// 通过存储过程返回特定数据
86 /// </summary>
87 /// <param name="ay"></param>
88 /// <param name="StoredProcedurename"></param>
89 /// <returns></returns>
90 public static DataSet gettable(SqlParameter[] pter, String StoredProcedurename)
91 {
92 getcon();
93 com2 = getcom();
94 com2.Connection = thiscon2;
95 com2.Parameters.Clear();
96 com2.CommandType = CommandType.StoredProcedure;
97 com2.CommandText = StoredProcedurename;
98 com2.Parameters.AddRange(pter);
99 adpater = new SqlDataAdapter(com);
100 ds = new DataSet();
101 adpater.Fill(ds);
102 closecom(ref com3);
103 return ds;
104 }
105
106 public static int todo(string str)
107 {
108
109 throw new NotImplementedException();
110 }
111
112 public static int todo(SqlParameter[] pter, string StoredProcedurename)
113 {
114 throw new NotImplementedException();
115 }
116
117 public static string getone(string str)
118 {
119 throw new NotImplementedException();
120 }
121
122 public static string getone(SqlParameter[] pter, string StoredProcedurename)
123 {
124 try
125 {
126 getcon();
127 com3 = getcom();
128 com3.Connection = thiscon2;
129 com3.Connection.Open();
130 com3.CommandType = CommandType.StoredProcedure;
131 com3.Parameters.Clear();
132 com3.Parameters.AddRange(pter);
133 com3.CommandText = StoredProcedurename;
134 rtstr = com.ExecuteScalar().ToString();
135 closecom(ref com3);
136
137 return rtstr;
138 }
139 catch (Exception e)
140 {
141 closecom(ref com3);
142 errormark.strong(e);
143 return "-1";
144 }
145
146
147 }
148
149 public static void closecom(ref SqlCommand comclose)
150 {
151
152 if (comclose != null)
153 {
154 comclose.Connection.Close();
155 comclose.Connection.Dispose();
156 comclose.Dispose();
157
158 }
159
160 }
161 /// <summary>
162 /// 设置链接字符串
163 /// </summary>
164 public static string setsqlcon
165 {
166 set
167 {
168 constr1 = value; sqlconstr1();
169
170 }
171
172 }
173
174 #endregion
175
176
177 public static void gettable()
178 {
179 throw new NotImplementedException();
180 }
181 }
问题补充:

很想知道 其中的静态对象 会不会在多用户调用的时候发生冲突

隋凯的主页 隋凯 | 初学一级 | 园豆:65
提问于:2011-05-27 15:41
< >
分享
最佳答案
0

关键在于SqlConnection,这个不能静态的,不然在多操作的情况下就会有问题,更别说多用户。

你想写静态的话,可以这样

public static SqlConnection getcon()
{

return new SqlConnection(constr1);

}

然后在执行命令的函数里调用

SqlConnection cn=getcon();

这样就没问题了。(最好加上using块)

收获园豆:25
ailove | 菜鸟二级 |园豆:382 | 2011-05-27 17:07
你说的问题我也发现了
但是不知道 2用用户同时调用一个静态函数会不会引发冲突
隋凯 | 园豆:65 (初学一级) | 2011-06-01 16:58
函数不管是不是静态的都一样的,不一样的是静态变量。
ailove | 园豆:382 (菜鸟二级) | 2011-06-01 20:14
其他回答(1)
0

不会,用户之间是不会影响的,重载方法会根据传入的参数调用对应的方法。

zhaojl | 园豆:205 (菜鸟二级) | 2011-05-27 16:05
我写的那是静态的你没看到?
支持(0) 反对(0) 隋凯 | 园豆:65 (初学一级) | 2011-05-27 16:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册