C# 对Access数据库操作的通用类
(转载自博主
Jerry很简单)
//Access数据库-C# 操作类 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace XXX
{
class AccessHelper
{
private string conn_str = null;
private OleDbConnection ole_connection = null;
private OleDbCommand ole_command = null;
private OleDbDataReader ole_reader = null;
private DataTable dt = null;
/// <summary>
/// 构造函数
/// </summary>
public AccessHelper()
{
conn_str =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\我的文档\Visual Studio 2008\Projects\AccessOperator\AccessOperator\bin\Debug\myDb.mdb'";
InitDB();
}
private void InitDB()
{
ole_connection =new OleDbConnection(conn_str);//创建实例
ole_command =new OleDbCommand();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="db_path">数据库路径</param>
public AccessHelper(string db_path)
{
conn_str ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+ db_path + "'";
InitDB();
}
/// <summary>
/// 转换数据格式
/// </summary>
/// <param name="reader">数据源</param>
/// <returns>数据列表</returns>
private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
{
DataTable dt_tmp =null;
DataRow dr =null;
int data_column_count = 0;
int i = 0;
data_column_count = reader.FieldCount;
dt_tmp = BuildAndInitDataTable(data_column_count);
if(dt_tmp == null)
{
return null;
}
while(reader.Read())
{
dr = dt_tmp.NewRow();
for(i = 0; i < data_column_count; ++i)
{
dr[i] = reader[i];
}
dt_tmp.Rows.Add(dr);
}
return dt_tmp;
}
/// <summary>
/// 创建并初始化数据列表
/// </summary>
/// <param name="Field_Count">列的个数</param>
/// <returns>数据列表</returns>
private DataTable BuildAndInitDataTable(int Field_Count)
{
DataTable dt_tmp =null;
DataColumn dc =null;
int i = 0;
if(Field_Count <= 0)
{
return null;
}
dt_tmp =new DataTable();
for(i = 0; i < Field_Count; ++i)
{
dc =new DataColumn(i.ToString());
dt_tmp.Columns.Add(dc);
}
return dt_tmp;
}
/// <summary>
/// 从数据库里面获取数据
/// </summary>
/// <param name="strSql">查询语句</param>
/// <returns>数据列表</returns>
public DataTable GetDataTableFromDB(string strSql)
{
if(conn_str == null)
{
return null;
}
try
{
ole_connection.Open();//打开连接
if(ole_connection.State == ConnectionState.Closed)
{
return null;
}
ole_command.CommandText = strSql;
ole_command.Connection = ole_connection;
ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);
dt = ConvertOleDbReaderToDataTable(ref ole_reader);
ole_reader.Close();
ole_reader.Dispose();
}
catch(System.Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
if(ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return dt;
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="strSql">sql语句</param>
/// <returns>返回结果</returns>
public int ExcuteSql(string strSql)
{
int nResult = 0;
try
{
ole_connection.Open();//打开数据库连接
if(ole_connection.State == ConnectionState.Closed)
{
return nResult;
}
ole_command.Connection = ole_connection;
ole_command.CommandText = strSql;
nResult = ole_command.ExecuteNonQuery();
}
catch(System.Exception e)
{
Console.WriteLine(e.ToString());
return nResult;
}
finally
{
if(ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return nResult;
}
//static void Main(string[] args)
//{
// AccessHelper Helper =new AccessHelper();
// DataTable dt = Helper.GetDataTableFromDB("select * from test");
// foreach(DataRow dr in dt.Rows)
// {
// Console.WriteLine(dr[0].ToString()+" "+dr[1].ToString());
// }
// Console.WriteLine(Helper.ExcuteSql("insert into test(test) values ('hello')"));
//}
}
}
C# 对Access数据库操作的通用类的更多相关文章
- 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. ...
- C# ACCESS数据库操作类
这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的调用可以参考MSSQL那个类的调用,差不多的,只是提取所有记录的数量的时候有多一个参数,这个需要注意一下! usi ...
- Microsoft Access数据库操作类(C#)
博文介绍的Microsoft Access数据库操作类是C#语言的,可实现对Microsoft Access数据库的增删改查询等操作.并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片 ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
- java web工程 数据库操作报驱动类找不到的错误
这几天在进行数据库的操作,写好数据库操作类后,用测试类测试成功通过,但是部署到tomcat后,从页面访问就会报异常. 最后终于发现是tomcat使用了连接池的数据连接方式. 解决方法是把jdbc ja ...
- C#对SQLite、Access数据库操作的封装,很好用的~
1.对SQLite的封装: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- asp.net mvc access数据库操作
连接access数据库其实也简单,只要按照mvc的模式来就可以,遵循c v约定就可以 既然渲染试图是强类型,那么取得的数据就转换成强类型,其他一切和asp.net操作一样 DB mydb = new ...
- C# 动软生成器对应的Access数据库操作类DbHelperOleDb
using System;using System.Collections;using System.Collections.Specialized;using System.Data;using S ...
随机推荐
- Linux:下载方式安装lrzsz
若机器服务使用yum源安装,可先下载好lrzsz文件后再上传安装 步骤一: 先下载lrzsz的tar包:wget https://ohse.de/uwe/releases/lrzsz-0.12.20. ...
- UITableView的创建及其一些常用方法
UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate. 一.UITableView的创建 1.代码方式: UITableV ...
- 【c++】虚基类
何要使用虚基类: 为何避免多层继承中出项多个公共基类所造成的歧义现象 虚基类用法 派生类继承基类时,加上一个virtual关键词则为虚拟基类继承. 在上图程序运行中,我们发现class bass的构造 ...
- Node入门(转)
原文链接:http://www.nodebeginner.org/index-zh-cn.html Node入门 作者: Manuel Kiessling翻译: goddyzhao & Gra ...
- Bitmap简单操作笔记
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- Python实现插件机制——自动import一个目录下的所有.py文件
假设有这样一个目录结构: /src main.py /plugins __init__.py a.py ...
- CheckedComboBoxEdit 重置初始化值的方法
CheckedComboBoxEdit ccbgb; ccbgb绑定方式ccbgb .Properties .DataSource =数据集; ccbgb.Properties.DisplayMemb ...
- VC++ 最小化到托盘、恢复
所谓的“托盘”,在Windows系统界面中,指的就是下面任务条右侧,有系统时间等等的标志的那一部分.在程序最小化或挂起时,但有不希望占据任务栏的时候,就可以把程序放到托盘区. 一.托盘编程相关函数 ...
- 《IT蓝豹》完整阅读软件客户端app
完整阅读软件客户端app,本项目里面还有扫描功能,搜索本地书籍,不过扫码功能通过c++层实现的. 本项目来自:https://github.com/JayFang1993/ScanBook Captu ...
- 关于初学者Could not find action or result :No result defined for action com.lyw.action.LoginAction and result success
解决如下: 将:<package name="struts2" extends="struts-default" > <action ...