.net连接access操作类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using System.IO; /// <summary>
///MyClasses 的摘要说明
/// </summary>
namespace newClasses
{ //自定义数据库连接类
public class superConn{ public OleDbConnection cnn;
private OleDbCommand cmd;
private OleDbDataReader datar; public superConn(string mdbFileName) {
string _path = "~\\App_Data\\" + mdbFileName;
string str_conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ System.Web.HttpContext.Current.Server.MapPath(_path);
//MapPath(_path);
cnn = new OleDbConnection(str_conn);
} // superConn.open()
public void open() {
cnn.Open();
} public void close() {
cnn.Close();
} public OleDbDataReader GetDataReader(string _sql) {
cmd = new OleDbCommand(_sql, cnn);
datar = cmd.ExecuteReader();
return datar;
} //返回DDL控件,表名列表
public DropDownList GetDDLforTableList() {
DropDownList _ddl = new DropDownList(); cnn.Open(); DataTable dt = cnn.GetSchema("Tables", null); foreach (DataRow dr in dt.Select("TABLE_TYPE='TABLE'"))
{
string s = dr["TABLE_NAME"].ToString();
_ddl.Items.Add(new ListItem(s)); } cnn.Close(); return _ddl;
} //返回table,详细表内容
public Table GetTable(string _tableName) {
Table _tbl = new Table();
cnn.Open(); cmd = new OleDbCommand("SELECT * FROM "+_tableName, cnn);
datar = cmd.ExecuteReader(); int i_fcount = datar.FieldCount; TableHeaderRow thr = new TableHeaderRow();
for (int i = ; i < i_fcount; i++)
{
TableHeaderCell thd = new TableHeaderCell();
thd.Text = datar.GetName(i);
thr.Cells.Add(thd); }
_tbl.Rows.Add(thr); while (datar.Read())
{
TableRow tr = new TableRow();
for (int i = ; i < i_fcount; i++)
{
TableCell td = new TableCell();
td.Text = datar[i].ToString();
tr.Cells.Add(td);
} _tbl.Rows.Add(tr);
} cnn.Close();
return _tbl;
} } //文件管理类
public class superFile { public string _fileName;
public string _fileTitle;
public string _fileDir="~/filesDownload"; //
public superFile() {
_fileName = _fileTitle = "";
} //
public superFile(int _id) {
_fileName = _fileTitle = "";
superConn sconn = new superConn("fileMNG.mdb");
sconn.open();
string _sql = "SELECT * FROM T_FILES WHERE f_id=" + _id;
OleDbDataReader dr = sconn.GetDataReader(_sql);
if (dr.Read())
{
_fileTitle = dr["f_title"].ToString();
_fileName = dr["f_name"].ToString();
}
sconn.close(); } //下载
public void fileDownload() {
string _path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath(_fileDir),_fileName); if (System.IO.File.Exists(_path))
{ HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + _fileTitle);
HttpContext.Current.Response.ContentType = "application/unknow";
HttpContext.Current.Response.TransmitFile(_path);
HttpContext.Current.Response.End(); } } } //读取文章的类
public class superArtReader{ public string ArtTitle;
public string ArtContent;
private string ArtID; /// <summary>
/// 构造一个空文章对象
/// </summary>
public superArtReader() {
ArtTitle = "";
ArtContent = "";
} /// <summary>
/// 通过ID从数据库获取对应ID的文章
/// </summary>
/// <param name="artID"></param>
public superArtReader(string artID) {
ArtID = artID;
ArtTitle = "";
ArtContent = ""; superConn scnn = new superConn("DATA.mdb");
scnn.open();
string _sql = "select * from T_article WHERE art_id=" + artID;
OleDbDataReader dr = scnn.GetDataReader(_sql);
if (dr.Read())
{
ArtTitle = dr["art_ttl"].ToString();
ArtContent=dr["art_content"].ToString();
}
scnn.close(); } } //新闻类:superNews静态类
//superNewsItem动态类 //superNewsItem动态类
public class superNewsItem {
private int news_id;
private int news_ncid;
private string nc_ttl; public string news_ttl;
public string news_guide;
public DateTime news_date;
public int news_imgid;
private string img_url;
public string news_content;
public int news_order;
public Boolean news_enable;
public Boolean news_home; /// <summary>
/// 构造空新闻条目
/// </summary>
public superNewsItem(){ } /// <summary>
/// 根据新闻的ID,从数据库获取新闻和相关信息;
/// </summary>
/// <param name="newsid"></param>
public superNewsItem(int newsid) {
superConn scnn = new superConn("DATA.mdb");
scnn.open();
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;
OleDbDataReader dr = scnn.GetDataReader(_sql);
if (dr.Read()) {
news_id = int.Parse(dr["news_id"].ToString());
news_ncid = int.Parse(dr["news_ncid"].ToString());
nc_ttl = dr["nc_ttl"].ToString();
news_ttl = dr["news_ttl"].ToString();
news_guide = dr["news_guide"].ToString();
news_date = (DateTime)dr["news_date"];
news_imgid = int.Parse(dr["news_imgid"].ToString());
img_url = dr["img_url"].ToString();
news_content = dr["news_content"].ToString();
news_order = int.Parse(dr["news_order"] is DBNull?"":dr["new_order"].ToString());
news_enable = (Boolean)dr["news_enable"];
news_home = (Boolean)dr["news_home"];
}
scnn.close();
} //更细致:将新闻类别设定为一个类,有属性ID,ClassName, 返回一个新闻类别的类,更符合调用逻辑
/// <summary>
/// 获取类别名称
/// </summary>
/// <returns>string型,类别名称</returns>
public string getClassName() {
return nc_ttl;
} public int getClassID() {
return news_ncid;
} /// <summary>
/// 以类别ID来设置新闻所属类别
/// </summary>
/// <param name="classID"></param>
public void setClass(int classID) {
superConn scnn = new superConn("DATA.mdb");
scnn.open();
string _sql = "SELECT * FROM List_newsClass WHERE nc_id=" + classID;
OleDbDataReader dr = scnn.GetDataReader(_sql);
if (dr.Read()) {
news_ncid = int.Parse(dr["nc_id"].ToString());
nc_ttl = dr["nc_ttl"].ToString();
}
scnn.close();
} } //图片管理类
//1. 静态类,图片上传类,只提供对应的方法
public static class superImgUploader { /// <summary>
/// 返回待上传文件的数量
/// </summary>
/// <returns></returns>
public static int imgsCount() {
int _count = ;
foreach (superImg _img in (superImg[])HttpContext.Current.Session["UploadImgs"]) {
if (_img != null) { _count++; }
}
return _count;
} /// <summary>
/// 添加一个图片对象到列表中
/// </summary>
/// <param name="ObjectImg">图片对象superImg</param>
/// <returns>返回添加结果</returns>
public static Boolean addImg(superImg ObjectImg) {
Boolean _added = false;
for (int i = ; i < ; i++) {
if (((superImg[])HttpContext.Current.Session["UploadImgs"])[i] == null) {
((superImg[])HttpContext.Current.Session["UploadImgs"])[i] = ObjectImg;
_added = true;
break;
}
}
return _added;
} /// <summary>
/// 从列表中移除指定图片对象
/// </summary>
/// <param name="imgIndex">0-9 图片列表编号索引</param>
public static void removeImg(int imgIndex) {
((superImg[])HttpContext.Current.Session["UploadImgs"])[imgIndex] = null;
} /// <summary>
/// 清除所有上传列表
/// </summary>
public static void clear() {
HttpContext.Current.Session["UploadImgs"] = new superImg[];
} /// <summary>
/// 上传列表中的所有文件
/// </summary>
public static void uploadAll() {
foreach (superImg si in (superImg[])HttpContext.Current.Session["UploadImgs"])
{
if (si != null)
{
if (si.readToUpload() == true)
{
string targPath = HttpContext.Current.Server.MapPath("~/test/" + si.targFname);
si.pstImgFile.SaveAs(targPath); if (File.Exists(targPath))
{
si.uploaded();
} }
}
}
clear();
} public static void setCID(int i, int value) {
((superImg[])HttpContext.Current.Session["UploadImgs"])[i].setCID(value);
} } //2.图片对象类
public class superImg { public HttpPostedFile pstImgFile;
public string Title;
public string orgFname;
public string targFname; private int img_id; //
public int img_cid; //需要默认值
private string img_cname; // private Boolean uploadDone; //默认FALSH /// <summary>
/// 构建一个空superImg对象
/// </summary>
public superImg() { } /// <summary>
/// 通过postedFile构建一个superImg对象
/// </summary>
/// <param name="pstFile"></param>
public superImg(HttpPostedFile _pstFile) {
pstImgFile = _pstFile;
Title = "新上传文件";
orgFname = _pstFile.FileName;
targFname = DateTime.Now.ToFileTime().ToString() + Path.GetExtension(orgFname);
img_cid = ;
uploadDone = false;
} /// <summary>
/// 通过数据库img_id构建对应的superImg对象
/// </summary>
/// <param name="imgID">数据表T_imgMng.img_id的值</param>
public superImg(int imgID) { } /// <summary>
/// 检测属性,并返回对象是否具备上传条件
/// </summary>
/// <returns></returns>
public Boolean readToUpload() {
Boolean _ready = true;
if (pstImgFile == null) { _ready = false; }
if (Title == null || Title == "") { _ready = false; }
if (orgFname == "" || orgFname == null) { _ready = false; }
if (targFname == "" || targFname == null) { _ready = false; }
if (uploadDone == true) { _ready = false; } return _ready;
} public void uploaded() {
uploadDone = true;
} public void setCID(int cidValue) {
img_cid = cidValue;
} }
}
.net连接access操作类的更多相关文章
- Microsoft Access数据库操作类(C#)
博文介绍的Microsoft Access数据库操作类是C#语言的,可实现对Microsoft Access数据库的增删改查询等操作.并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片 ...
- access数据库的连接字符串以及数据库操作类
<!--access数据库连接方式--> <add name="QYTangConnectionString" connectionString="Pr ...
- Oracle数据库操作类及连接方法
创建DataBaseAccess引用库下面包括DBConnectionString.cs,DBType.cs ,SysName.cs 三个类 DBConnectionString: using Sys ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .
//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...
- 一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER
最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库.读数据表.执行SQL操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢. ADO ...
- System.Data.OleDb操作access数据库类,【bubuko.com】
access数据库在应用了System.Data.OleDb后操作会很方便,这是一个常用的数据库操作类,其中两个方法,一个是返回datatable的,一个是执行sql语句返回影响记录的(一般是inse ...
- 【PHPsocket编程专题(实战篇②)】兼容 Curl/Socket/Stream 的 HTTP 操作类[转]
<?php /************************************************************ * 描述:HTTP操作类 * 作者:heiyeluren ...
- php连接Access数据库的三种方法
http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2009/1115/3524.html 虽然不是一个类但先放这儿吧 最近想把一个asp的网站改成 ...
随机推荐
- java 打印出99乘法口诀表
public class Mutiplay { /** *实现99乘法表 * @param args */ public static void main(String[] args) { Syste ...
- 修改tomcat的logo
每页的<head> 里添加 <link rel="icon" href="favicon.gif" /> 图片名称必须是favi ...
- android pbap client 蓝牙
一. 简介: 此功能具体使用的是bluetoothV2.1之后的Phone Book Access Profile功能,简称PBAP .目前MTK Android中只实现了server端的功能,并没 ...
- [团队项目]后续安排 Github
6.后续安排 第16周 周二晚7点之前将本代码上传到GITHUB. 周三上课时运行你们的系统给我观赏一下. 根据博客,运行演示,github代码情况评定第二个冲刺的分数. 至此,软件工程学期平时分截止 ...
- word中设置前几页为罗马数字,后几页设置为阿拉伯数字
假如第1-5页摘要部分页脚要是罗马数字,第6页开始是正文部分是阿拉伯数字,起始页为1. WORD2003 1.将光标定位在第5页末尾处,在菜单栏中依次点击“插入——分隔符——(分节符类型)下一页”.按 ...
- STM32/GD32上内存堆栈溢出探测研究
无数次遭受堆栈溢出折磨,随着系统变得复杂,故障点越来越难以查找!主要溢出情况如下:1,一般RAM最后两块空间是堆Heap和栈Stack,堆从下往上用,栈从上往下用,任意一个用完,都会进入对方的空间2, ...
- 零零碎碎写的shell脚本(三):一键自动归档压缩脚本
#!/bin/bash # author by sysk read -p "There files: " FILE1 FILE2 FILE3 read -p "Desti ...
- Xcode的Architectures和Valid Architectures的区别,
登录 | 注册 ys410900345的专栏 目录视图摘要视图订阅 学院APP首次下载,可得50C币! 欢迎来帮助开源“进步” 当讲师?爱学习?投票攒课吧 CSDN 2015博 ...
- visual studio运行时库MT、MTd、MD、MDd的研究(转载)
转载:http://blog.csdn.net/ybxuwei/article/details/9095067 转载:http://blog.sina.com.cn/s/blog_624485f701 ...
- SVN Unable to connect to a repository at URL问题解决
图1 之前用的好好的,不知道为什么今天就不行了,根据网上给的方法TortoiseSVN -> Settings -> Saved Data,点击个个"Clear"按钮, ...