(转载自博主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数据库操作的通用类的更多相关文章

  1. C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

    原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...

  2. C#---数据库访问通用类、Access数据库操作类、mysql类 .

    //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...

  3. C# ACCESS数据库操作类

    这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的调用可以参考MSSQL那个类的调用,差不多的,只是提取所有记录的数量的时候有多一个参数,这个需要注意一下! usi ...

  4. Microsoft Access数据库操作类(C#)

    博文介绍的Microsoft Access数据库操作类是C#语言的,可实现对Microsoft Access数据库的增删改查询等操作.并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片 ...

  5. 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 ...

  6. java web工程 数据库操作报驱动类找不到的错误

    这几天在进行数据库的操作,写好数据库操作类后,用测试类测试成功通过,但是部署到tomcat后,从页面访问就会报异常. 最后终于发现是tomcat使用了连接池的数据连接方式. 解决方法是把jdbc ja ...

  7. C#对SQLite、Access数据库操作的封装,很好用的~

    1.对SQLite的封装: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. asp.net mvc access数据库操作

    连接access数据库其实也简单,只要按照mvc的模式来就可以,遵循c v约定就可以 既然渲染试图是强类型,那么取得的数据就转换成强类型,其他一切和asp.net操作一样 DB mydb = new ...

  9. C# 动软生成器对应的Access数据库操作类DbHelperOleDb

    using System;using System.Collections;using System.Collections.Specialized;using System.Data;using S ...

随机推荐

  1. mac boot2docker certs not valid with 1.7

    摘自:https://github.com/boot2docker/boot2docker/issues/824 An error occurred trying to connect: Get ht ...

  2. Excel 取得一定范围内最大的有值的行号

    dim iRow iRow = Range("A1000").End(xlUp).Row

  3. 使用onclick跳转到其他页面/跳转到指定url

    ☆如果是本页显示可以直接用location,方法如下: ①onclick="javascript:window.location.href='URL'" ②onclick=&quo ...

  4. oracle的over函数应用(转载)

    摘自: http://www.poluoluo.com/jzxy/201004/81921.html 百度文库也记载了Oracle中over()分析函数的用法 在泡坛子的时候中无意中发现了这个函数,才 ...

  5. Struts2框架之-Struts2的标签

    Struts2包含哪些标签? 解答: A: <s:a href=”"></s:a>—–超链接,类似于html里的<a></a> <s:a ...

  6. c语言自定义BOOL函数

    C语言中没有BOOL类型变量,它是C++独有的,由于使用BOOL类型可以使代码更具有可读性,很多编程者都在C中自己定义了类似的应用,一般方法有两种: 第一种:采用宏定义方式 typedef int B ...

  7. SqlLocalDB使用笔记

    一.介绍 SqlLocalDB是VS安装时附带的数据库软件,相当于精简版的SQL Express. 二.使用 VS版本为2015,默认安装位置为:C:\Program Files\Microsoft ...

  8. ActiveX: 如何用.inf和.ocx文件生成cab文件

    ActiveX: 如何用.inf和.ocx文件生成cab文件  

  9. 用Linux自带的Logrotate来管理日志

    Logrotate是由cron控制,cron在规定的时间执行 " logrotate  /etc/logrotate.conf "命令.将对象日志进行转储,删除,压缩等操作... ...

  10. ios UINaviBar 去除分割线

    //For a custom shadow image to be shown, custom background image must also be set with the setBackgr ...