java access数据库实现增删改查_C#对Access进行增删改查的完整示例

这篇文章整理了C#对Access数据库的查询、添加记录、删除记录和更新数据等一系列的操作示例,有需要的可以参考学习。

首先是AccessHelper.cs,网上有下载,下面附送一份;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.OleDb;

using System.Data;

using System.Windows.Forms;

namespace yxdain

{

public 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;

///

/// 构造函数

///

public AccessHelper()

{

//conn_str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Environment.CurrentDirectory + "\\yxdain.accdb'";

conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Environment.CurrentDirectory + "\\yxdain.accdb'";

InitDB();

}

private void InitDB()

{

ole_connection =new OleDbConnection(conn_str);//创建实例

ole_command =new OleDbCommand();

}

///

/// 构造函数

///

///数据库路径

public AccessHelper(string db_path)

{

//conn_str ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+ db_path + "'";

conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + db_path + "'";

InitDB();

}

///

/// 转换数据格式

///

///数据源

/// 数据列表

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;

}

///

/// 创建并初始化数据列表

///

///列的个数

/// 数据列表

private DataTable BuildAndInitDataTable(int Field_Count)

{

DataTable dt_tmp =null;

DataColumn dc =null;

int i = 0;

if(Field_Cou

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值