码迷,mamicode.com
首页 > 数据库 > 详细

.net连接access操作类

时间:2016-01-15 16:18:30      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Data;
  7 using System.Data.OleDb;
  8 using System.Web.UI.WebControls;
  9 using System.IO;
 10 
 11 /// <summary>
 12 ///MyClasses 的摘要说明
 13 /// </summary>
 14 namespace newClasses
 15 {
 16     
 17     //自定义数据库连接类
 18     public class superConn{
 19 
 20         public OleDbConnection cnn;
 21         private OleDbCommand cmd;
 22         private OleDbDataReader datar;
 23 
 24         public superConn(string mdbFileName) {
 25             string _path = "~\\App_Data\\" + mdbFileName;
 26             string str_conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
 27                 + System.Web.HttpContext.Current.Server.MapPath(_path);
 28                 //MapPath(_path); 
 29             cnn = new OleDbConnection(str_conn);
 30         }
 31 
 32         // superConn.open()
 33         public void open() {
 34             cnn.Open();
 35         }
 36 
 37         public void close() {
 38             cnn.Close();
 39         }
 40 
 41         public OleDbDataReader GetDataReader(string _sql) {
 42             cmd = new OleDbCommand(_sql, cnn);
 43             datar = cmd.ExecuteReader();
 44             return datar;
 45         }
 46 
 47         //返回DDL控件,表名列表
 48         public DropDownList GetDDLforTableList() {
 49             DropDownList _ddl = new DropDownList();
 50 
 51             cnn.Open();
 52 
 53             DataTable dt = cnn.GetSchema("Tables", null);
 54 
 55             foreach (DataRow dr in dt.Select("TABLE_TYPE=‘TABLE‘"))
 56             {
 57                 string s = dr["TABLE_NAME"].ToString();
 58                 _ddl.Items.Add(new ListItem(s));
 59 
 60             }
 61 
 62             cnn.Close();
 63 
 64             return _ddl;
 65         }
 66 
 67         //返回table,详细表内容
 68         public Table GetTable(string _tableName) {
 69             Table _tbl = new Table();
 70             cnn.Open();
 71                         
 72             cmd = new OleDbCommand("SELECT * FROM "+_tableName, cnn);
 73             datar = cmd.ExecuteReader();
 74 
 75 
 76             int i_fcount = datar.FieldCount;
 77 
 78             TableHeaderRow thr = new TableHeaderRow();
 79             for (int i = 0; i < i_fcount; i++)
 80             {
 81                 TableHeaderCell thd = new TableHeaderCell();
 82                 thd.Text = datar.GetName(i);
 83                 thr.Cells.Add(thd);
 84 
 85             }
 86             _tbl.Rows.Add(thr);
 87 
 88 
 89 
 90             while (datar.Read())
 91             {
 92                 TableRow tr = new TableRow();
 93                 for (int i = 0; i < i_fcount; i++)
 94                 {
 95                     TableCell td = new TableCell();
 96                     td.Text = datar[i].ToString();
 97                     tr.Cells.Add(td);
 98                 }
 99 
100                 _tbl.Rows.Add(tr);
101             }
102 
103 
104             cnn.Close();
105             return _tbl;
106         }
107 
108     }
109 
110 
111 
112 
113 
114     //文件管理类
115     public class superFile {
116 
117         public string _fileName;
118         public string _fileTitle;
119         public string _fileDir="~/filesDownload";
120 
121         //1
122         public superFile() {
123             _fileName = _fileTitle = "";
124         }
125 
126         //2
127         public superFile(int _id) {
128             _fileName = _fileTitle = "";
129             superConn sconn = new superConn("fileMNG.mdb");
130             sconn.open();
131             string _sql = "SELECT * FROM T_FILES WHERE f_id=" + _id;
132             OleDbDataReader dr = sconn.GetDataReader(_sql);
133             if (dr.Read())
134             {
135                 _fileTitle = dr["f_title"].ToString();
136                 _fileName =  dr["f_name"].ToString();
137             }
138             sconn.close();
139         
140         }
141 
142         //下载
143         public void fileDownload() {
144             string _path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath(_fileDir),_fileName);
145            
146                 if (System.IO.File.Exists(_path))
147                 {
148 
149                     HttpContext.Current.Response.Clear();
150                     HttpContext.Current.Response.Buffer = true;
151 
152                     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + _fileTitle);
153                     HttpContext.Current.Response.ContentType = "application/unknow";
154                     HttpContext.Current.Response.TransmitFile(_path);
155                     HttpContext.Current.Response.End();
156 
157                 }
158 
159         }
160 
161     
162     }
163 
164   
165 
166     //读取文章的类
167     public class superArtReader{
168 
169         public string ArtTitle;
170         public string ArtContent;
171         private string ArtID;
172 
173         /// <summary>
174         /// 构造一个空文章对象
175         /// </summary>
176         public superArtReader() {
177             ArtTitle = "";
178             ArtContent = "";
179         }
180 
181 
182 
183         /// <summary>
184         /// 通过ID从数据库获取对应ID的文章
185         /// </summary>
186         /// <param name="artID"></param>
187         public superArtReader(string artID) {
188             ArtID = artID;
189             ArtTitle = "";
190             ArtContent = "";
191 
192             superConn scnn = new superConn("DATA.mdb");
193             scnn.open();
194             string _sql = "select * from T_article WHERE art_id=" + artID;
195             OleDbDataReader dr = scnn.GetDataReader(_sql);
196             if (dr.Read())
197             {
198                 ArtTitle = dr["art_ttl"].ToString();
199                 ArtContent=dr["art_content"].ToString();
200             }
201             scnn.close();
202            
203         }
204 
205 
206 
207     }
208 
209 
210 
211 
212     //新闻类:superNews静态类
213     //superNewsItem动态类
214 
215     //superNewsItem动态类
216     public class superNewsItem {
217         private int news_id;
218         private int news_ncid;
219         private string nc_ttl;
220 
221         public string news_ttl;
222         public string news_guide;
223         public DateTime news_date;
224         public int news_imgid;
225         private string img_url;
226         public string news_content;
227         public int news_order;
228         public Boolean news_enable;
229         public Boolean news_home;
230 
231         /// <summary>
232         /// 构造空新闻条目
233         /// </summary>
234         public superNewsItem(){
235                       
236 
237         }
238 
239         /// <summary>
240         /// 根据新闻的ID,从数据库获取新闻和相关信息;
241         /// </summary>
242         /// <param name="newsid"></param>
243         public superNewsItem(int newsid) {
244             superConn scnn = new superConn("DATA.mdb");
245             scnn.open();
246             string _sql = "SELECT T_NEWS.*, T_imgMng.img_url, List_newsClass.nc_ttl FROM List_newsClass INNER JOIN (T_imgMng INNER JOIN T_NEWS ON T_imgMng.img_id = T_NEWS.news_imgid) ON List_newsClass.nc_id = T_NEWS.news_ncid WHERE news_id=" + newsid;
247             OleDbDataReader dr = scnn.GetDataReader(_sql);
248             if (dr.Read()) {
249                 news_id = int.Parse(dr["news_id"].ToString());   
250                 news_ncid = int.Parse(dr["news_ncid"].ToString());
251                 nc_ttl = dr["nc_ttl"].ToString();
252                 news_ttl = dr["news_ttl"].ToString();
253                 news_guide = dr["news_guide"].ToString();
254                 news_date = (DateTime)dr["news_date"];
255                 news_imgid = int.Parse(dr["news_imgid"].ToString());
256                 img_url = dr["img_url"].ToString();
257                 news_content = dr["news_content"].ToString();
258                 news_order = int.Parse(dr["news_order"] is DBNull?"0":dr["new_order"].ToString());
259                 news_enable = (Boolean)dr["news_enable"];
260                 news_home = (Boolean)dr["news_home"];
261             }
262             scnn.close();
263         }
264 
265 
266         //更细致:将新闻类别设定为一个类,有属性ID,ClassName, 返回一个新闻类别的类,更符合调用逻辑
267         /// <summary>
268         /// 获取类别名称
269         /// </summary>
270         /// <returns>string型,类别名称</returns>
271         public string getClassName() {
272             return nc_ttl;
273         }
274 
275         public int getClassID() {
276             return news_ncid;
277         }
278 
279         /// <summary>
280         /// 以类别ID来设置新闻所属类别
281         /// </summary>
282         /// <param name="classID"></param>
283         public void setClass(int classID) {
284             superConn scnn = new superConn("DATA.mdb");
285             scnn.open();
286             string _sql = "SELECT * FROM List_newsClass WHERE nc_id=" + classID;
287             OleDbDataReader dr = scnn.GetDataReader(_sql);
288             if (dr.Read()) {
289                 news_ncid = int.Parse(dr["nc_id"].ToString());
290                 nc_ttl = dr["nc_ttl"].ToString();
291             }
292             scnn.close();
293         }
294 
295     
296     }
297 
298 
299 
300 
301 
302     //图片管理类
303     //1. 静态类,图片上传类,只提供对应的方法
304     public static class superImgUploader {
305 
306 
307 
308         /// <summary>
309         /// 返回待上传文件的数量
310         /// </summary>
311         /// <returns></returns>
312         public static int imgsCount() {
313             int _count = 0;
314             foreach (superImg _img in (superImg[])HttpContext.Current.Session["UploadImgs"]) {
315                 if (_img != null) { _count++; }
316             }
317             return _count;
318         }
319 
320         /// <summary>
321         /// 添加一个图片对象到列表中
322         /// </summary>
323         /// <param name="ObjectImg">图片对象superImg</param>
324         /// <returns>返回添加结果</returns>
325         public static Boolean addImg(superImg ObjectImg) {
326             Boolean _added = false;
327             for (int i = 0; i < 10; i++) {
328                 if (((superImg[])HttpContext.Current.Session["UploadImgs"])[i] == null) {
329                     ((superImg[])HttpContext.Current.Session["UploadImgs"])[i] = ObjectImg;
330                     _added = true;
331                     break;
332                 }
333             }
334             return _added;
335         }
336 
337 
338         /// <summary>
339         /// 从列表中移除指定图片对象
340         /// </summary>
341         /// <param name="imgIndex">0-9 图片列表编号索引</param>
342         public static void removeImg(int imgIndex) {
343             ((superImg[])HttpContext.Current.Session["UploadImgs"])[imgIndex] = null;
344         }
345 
346 
347         /// <summary>
348         /// 清除所有上传列表
349         /// </summary>
350         public static void clear() {
351             HttpContext.Current.Session["UploadImgs"] = new superImg[10];
352         }
353 
354 
355         /// <summary>
356         /// 上传列表中的所有文件
357         /// </summary>
358         public static void uploadAll() {
359             foreach (superImg si in (superImg[])HttpContext.Current.Session["UploadImgs"])
360             {
361                 if (si != null)
362                 {
363                     if (si.readToUpload() == true)
364                     {
365                         string targPath = HttpContext.Current.Server.MapPath("~/test/" + si.targFname);
366                         si.pstImgFile.SaveAs(targPath);
367 
368                         if (File.Exists(targPath))
369                         {
370                             si.uploaded();
371                         }
372 
373                     }
374                 }
375             }
376             clear();
377         }
378 
379 
380 
381         public static void setCID(int i, int value) {
382             ((superImg[])HttpContext.Current.Session["UploadImgs"])[i].setCID(value);
383         }
384 
385     }
386 
387 
388 
389 
390 
391 
392 
393     //2.图片对象类
394     public class superImg {
395 
396         public HttpPostedFile pstImgFile;
397         public string Title;
398         public string orgFname;
399         public string targFname;
400 
401         private int img_id;             //
402         public int img_cid;             //需要默认值
403         private string img_cname;       //
404 
405         private Boolean uploadDone;     //默认FALSH
406         
407 
408 
409         
410 
411         /// <summary>
412         /// 构建一个空superImg对象
413         /// </summary>
414         public superImg() { 
415             
416         }
417 
418 
419         /// <summary>
420         /// 通过postedFile构建一个superImg对象
421         /// </summary>
422         /// <param name="pstFile"></param>
423         public superImg(HttpPostedFile _pstFile) {
424             pstImgFile = _pstFile;
425             Title = "新上传文件";
426             orgFname = _pstFile.FileName;
427             targFname = DateTime.Now.ToFileTime().ToString() + Path.GetExtension(orgFname);
428             img_cid = 1;
429             uploadDone = false;
430         }
431 
432 
433         /// <summary>
434         /// 通过数据库img_id构建对应的superImg对象
435         /// </summary>
436         /// <param name="imgID">数据表T_imgMng.img_id的值</param>
437         public superImg(int imgID) { 
438         
439         }
440 
441 
442         /// <summary>
443         /// 检测属性,并返回对象是否具备上传条件
444         /// </summary>
445         /// <returns></returns>
446         public Boolean readToUpload() {
447             Boolean _ready = true;
448             if (pstImgFile == null) { _ready = false; }
449             if (Title == null || Title == "") { _ready = false; }
450             if (orgFname == "" || orgFname == null) { _ready = false; }
451             if (targFname == "" || targFname == null) { _ready = false; }
452             if (uploadDone == true) { _ready = false; }
453             
454             return _ready;
455         }
456 
457 
458         public void uploaded() {
459             uploadDone = true;
460         }
461 
462 
463         public void setCID(int cidValue) {
464             img_cid = cidValue;
465         }
466 
467 
468         
469 
470     }
471 }

 

.net连接access操作类

标签:

原文地址:http://www.cnblogs.com/hyyweb/p/5133195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!