自己写的ASP.NET数据基本数据库操作类作者:LiJun027

  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.OleDb;
 11 /**/ /// <summary>
 12/// DB 的摘要说明
 13/// </summary>

 14 public   class  DB
 15 {
 16    public DB()
 17    {
 18        //
 19        // TODO: 在此处添加构造函数逻辑
 20        //
 21    }

 22    public static OleDbConnection Getconn()
 23    {
 24        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionString"].ToString()));//web.config文件配置
 25        if (conn.State.Equals(ConnectionState.Closed))
 26        {
 27
 28            conn.Open();
 29
 30        }

 31        return conn;
 32    
 33    }

 34    //=================================================
 35    //功能描述:关闭数据库
 36    //时间:2007.11.10
 37    //=================================================
 38    private static void closeConnection()
 39    {
 40        OleDbConnection conn = DB.Getconn();
 41        OleDbCommand cmd = new OleDbCommand();
 42        if (conn.State == ConnectionState.Open)
 43        {
 44            conn.Close();
 45            conn.Dispose();
 46            cmd.Dispose();
 47        }

 48    }

 49    //=================================================
 50    //功能描述:执行SQL语句
 51    //输入参数:sql,查询的SQL语句
 52    //时间:2007.11.10
 53    //=================================================
 54    public static  void execnonsql(string sql)
 55    {
 56        try
 57        {
 58            OleDbConnection conn = DB.Getconn();
 59            OleDbCommand com = new OleDbCommand(sql, conn);
 60            com.ExecuteNonQuery();
 61        }

 62        catch (Exception e)
 63        {
 64            throw new Exception(e.Message);
 65        }

 66        finally
 67        {
 68            closeConnection(); 
 69        }

 70
 71    }

 72    //=================================================
 73    //功能描述:获取DATASET
 74    //输入参数:sql,查询的SQL语句
 75    //返回值:DataSet
 76    //时间:2007.11.10
 77    //=================================================
 78    public static DataSet getdataset(string sql)
 79    {
 80        try
 81        {
 82            OleDbConnection conn = DB.Getconn();
 83            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
 84            DataSet ds = new DataSet();
 85            adp.Fill(ds, "ds");
 86            return ds;
 87        }

 88        catch (Exception e)
 89        {
 90            throw new Exception(e.Message);
 91        
 92        }

 93        finally
 94        {
 95         closeConnection();
 96        }

 97    }

 98    //=================================================
 99    //功能描述:获取某个字段数据
100    //输入参数:sql,查询的SQL语句
101    //返回值:hang
102    //时间:2007.11.10
103    //=================================================
104    public static string FindString(string sql)
105    {
106        try
107        {
108            OleDbConnection conn = DB.Getconn();
109            OleDbCommand com = new OleDbCommand(sql, conn);
110            string hang = Convert.ToString(com.ExecuteScalar());
111            return hang;
112        }

113        catch (Exception e)
114        {
115            throw new Exception(e.Message);
116
117        }

118        finally
119        {
120            closeConnection();
121        }

122
123    }

124    //=================================================
125    //功能描述:对DATAGRIG进行数据绑定,无排序
126    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
127    //返回值:无
128    //时间:2007.11.10
129    //=================================================
130    public static void binddatagrid(string sql, DataGrid dg)
131    {
132
133        try
134        {
135            DataSet ds = getdataset(sql);
136            dg.DataSource = ds.Tables[0].DefaultView;
137            dg.DataBind();
138        }

139        catch (Exception e)
140        {
141            throw new Exception(e.Message);
142        
143        }

144        finally
145        {
146         closeConnection();
147        }

148    }

149    //=================================================
150    //功能描述:对DropDownList进行数据绑定,无排序
151    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
152    //返回值:无
153    //时间:2007.11.10
154    //=================================================
155    public static void bindDropDownList(string sql, DropDownList dl, string class_name, string id)
156    {
157
158        try
159        {
160            DataSet ds = getdataset(sql);
161            dl.DataSource = ds.Tables[0].DefaultView;
162            dl.DataTextField = class_name;
163            dl.DataValueField = id;
164            dl.DataBind();
165        }

166        catch (Exception e)
167        {
168            throw new Exception(e.Message);
169
170        }

171        finally
172        {
173            closeConnection();
174        }

175    }

176    //=================================================
177    //功能描述:对RadioButtonList进行数据绑定,无排序
178    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
179    //返回值:无
180    //时间:2007.11.10
181    //=================================================
182    public static void bindRadioButtonList(string sql, RadioButtonList rl, string class_name, string id)
183    {
184
185        try
186        {
187            DataSet ds = getdataset(sql);
188            rl.DataSource = ds.Tables[0].DefaultView;
189            rl.DataTextField = class_name;
190            rl.DataValueField = id;
191            rl.SelectedIndex = 0;
192            rl.DataBind();
193        }

194        catch (Exception e)
195        {
196            throw new Exception(e.Message);
197
198        }

199        finally
200        {
201            closeConnection();
202        }

203    }

204    //=================================================
205    //功能描述:对GridView进行数据绑定,无排序
206    //输入参数:sql,查询的SQL语句;dg,需要绑定的DATAGRID控件
207    //返回值:无
208    //时间:2007.11.10
209    //=================================================
210    public static void bindGridView(string sql, GridView dg)
211    {
212        try
213        {
214            OleDbConnection conn = DB.Getconn();
215            DataSet ds = getdataset(sql);
216            dg.DataSource = ds.Tables[0].DefaultView;
217            dg.DataBind();
218        }

219        catch (Exception e)
220        {
221            throw new Exception(e.Message);
222
223        }

224        finally
225        {
226            closeConnection();
227        }

228    }

229    //=================================================
230    //功能描述:对datalist进行数据绑定,无排序
231    //输入参数:sql,查询的SQL语句;dl,需要绑定的datalist控件
232    //返回值:无
233    //时间:2007.11.10
234    //=================================================
235    public static void binddatalist(string sql, DataList dl)
236    {
237        try
238        {
239            OleDbConnection conn = DB.Getconn();
240            DataSet ds = getdataset(sql);
241            dl.DataSource = ds.Tables[0].DefaultView;
242            dl.DataBind();
243        }

244        catch (Exception e)
245        {
246            throw new Exception(e.Message);
247
248        }

249        finally
250        {
251            closeConnection();
252        }

253    }

254    //=================================================
255    //功能描述:对repeater进行数据绑定,无排序
256    //输入参数:sql,查询的SQL语句;dl,需要绑定的repeater控件
257    //返回值:无
258    //时间:2007.11.10
259    //=================================================
260    public static void bindrepeater(string sql, Repeater rp)
261    {
262        try
263        {
264            OleDbConnection conn = DB.Getconn();
265            DataSet ds = getdataset(sql);
266            rp.DataSource = ds.Tables[0].DefaultView;
267            rp.DataBind();
268        }

269        catch (Exception e)
270        {
271            throw new Exception(e.Message);
272
273        }

274        finally
275        {
276            closeConnection();
277        }

278    }

279    //=================================================
280    //功能描述:对listbox进行数据绑定
281    //输入参数:sql,查询的SQL语句;listb,需要绑定的listbox控件
282    //返回值:无
283    //时间:2007.11.10
284    //=================================================
285    public static void bindlistbox(string sql, ListBox listb, string class_name, string id)
286    {
287        try
288        {
289            OleDbConnection conn = DB.Getconn();
290            DataSet ds = getdataset(sql);
291            listb.DataSource = ds.Tables[0].DefaultView;
292            listb.DataTextField = class_name;
293            listb.DataValueField = id;
294            listb.DataBind();
295        }

296        catch (Exception e)
297        {
298            throw new Exception(e.Message);
299
300        }

301        finally
302        {
303            closeConnection();
304        }

305    }

306}

307

转载于:https://www.cnblogs.com/LiJun027/archive/2007/12/14/994678.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值