c# vs2010 连接access数据库
来源:互联网 发布:手机域名是什么 编辑:程序博客网 时间:2024/05/29 13:59
最近要处理一个大数据量的access,想用java连接,总是各种错误,最后使用C#连接成功。
代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Data.OleDb;using System.Data;using System.IO;using System.Reflection; namespace Project2{ class Program { static void Main(string[] args) { OleDbConnection conn = null; try { string strcon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Users\\yagao\\Desktop\\data.accdb;"; conn = new OleDbConnection(strcon); conn.Open(); //string pattern = "\\d+"; //string a = null; //string b = null; //string c = null; //string d = null; //string e = null; //string f = null; //string g = null; string h = null; string l = null; //int i = 1; //int j = 1; //int k = 1; int m = 1; //**提取邮编前两位放入pc字段** /*string sql = "select zip from dbo_zzdw201511_zzz where zip is not null and pc is null"; OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); //创建适配对象 DataTable dt = new DataTable(); //新建表对象 da.Fill(dt); //用适配对象填充表对象 foreach (DataRow row in dt.Rows) { a = row["zip"].ToString(); foreach (Match match in Regex.Matches(a, pattern)) { b = match.Value; } c = b.Substring(0, 2); sql = "update dbo_zzdw201511_zzz set pc='" + c + "' where zip='" + a + "'"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); Console.WriteLine("{0} {1} {2} {3} ", i, a, b, c); i++; } //**利用pc进行匹配,更新表内容** String sql = "select * from 省_pc"; OleDbDataAdapter da1 = new OleDbDataAdapter(sql, conn); //创建适配对象 DataTable dt1 = new DataTable(); //新建表对象 da1.Fill(dt1); //用适配对象填充表对象 foreach (DataRow row1 in dt1.Rows) { d = row1["pc"].ToString(); e = row1["省"].ToString(); //更新原表内容 sql = "update dbo_zzdw201511_zzz set city='" + e + "' where pc='" + d + "'"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); Console.WriteLine("{0} {1} {2}", j, d, e); j++; } //**将没有邮编数据的部分利用单位中拼音字符串匹配进行补充** String sql = "select * from 省_拼音"; OleDbDataAdapter da2 = new OleDbDataAdapter(sql, conn); //创建适配对象 DataTable dt2 = new DataTable(); //新建表对象 da2.Fill(dt2); //用适配对象填充表对象 foreach (DataRow row2 in dt2.Rows) { f = row2["拼音"].ToString(); g = row2["省"].ToString(); //更新原表内容 sql = "update dbo_zzdw201511_zzz set city='" + g + "' where DW like '%" + f + "%' and city is null"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); Console.WriteLine("{0} {1} {2}", k, f, g); k++; }*/ //**创建excel文件** /*string path; //文件路径 MSExcel.Application excelApp; //Excel应用程序 MSExcel.WorkBook excelDoc; //Excel文档 path = @"C:\\Users\\yagao\\Desktop\\数据组织\\excel\\data.xlsx"; excelApp = new MSExcel.ApplicationClass(); if(File.Exists(path)) { File.Delete(path); } Object nothing = Missing.Value; excelDoc = excelApp.Workbooks.Add(nothing); Object format = MSExcel.XlFileFormat.xlWorkbookDefault; excelDoc.SaveAs(path,nothing,nothing,nothing,nothing,nothing, MSExcel.XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing,nothing); excelDoc.Close(nothing,nothing,nothing); excelApp.Quit(); */ //**查找各省doi到excel** /*String sql = "select * from 省_英文"; OleDbDataAdapter da3 = new OleDbDataAdapter(sql, conn); //创建适配对象 DataTable dt3 = new DataTable(); //新建表对象 da3.Fill(dt3); //用适配对象填充表对象 foreach (DataRow row3 in dt3.Rows) { h = row3["省"].ToString(); l = row3["英文"].ToString(); sql = "insert into [Excel 12.0 Xml;Database=C:\\Users\\yagao\\Desktop\\数据组织\\excel\\data.xlsx]." + l + " select doi from dbo_zzdw201511_zzz where city='" + h + "' and doi<>''"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); Console.WriteLine("{0} {1} {2}", m, h ,l); m++; }*/ //**查找各省doi到txt** String sql = "select * from 省_英文"; OleDbDataAdapter da3 = new OleDbDataAdapter(sql, conn); //创建适配对象 DataTable dt3 = new DataTable(); //新建表对象 da3.Fill(dt3); //用适配对象填充表对象 foreach (DataRow row3 in dt3.Rows) { h = row3["省"].ToString(); l = row3["英文"].ToString(); sql = "select doi into [text;Database=C:\\Users\\yagao\\Desktop\\数据组织\\txt]." + l + ".txt from dbo_zzdw201511_zzz where city='" + h + "' and doi<>''"; OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); Console.WriteLine("{0} {1} {2}", m, h, l); m++; } } finally { conn.Close(); } } }}
参考文章来源:
http://www.cnblogs.com/net064/p/5473886.html
vs2010中C#连接access数据库的配置以及操作代码
http://www.jb51.net/article/45197.htm
介绍了C# Access数据库增删查改的简单方法
0 0
- c# vs2010 连接access数据库
- vs2010 C#链接 ACCESS数据库
- C#连接Access数据库
- C#连接access数据库
- C#连接ACCESS数据库
- C#连接Access数据库
- c#连接access数据库
- C#连接Access数据库
- c# 连接ACCESS数据库
- C#连接access数据库
- VS2010连接Oracle数据库 C#
- c#中连接Access数据库
- C#连接Access数据库字符串
- C#连接Access数据库(详解)
- vs2010 C#连接ACCESS 小寨地铁出口选择助手
- VS2010下MFC通过ADO连接Access数据库
- vs2010连接access数据库及数据表基本操作(二)
- 如何在VS2010中连接MySQL和Access数据库
- 解决跨域问题
- 观察者模式实例
- 设置ntpdate服务开机启动校验时间
- erlang dialyzer使用
- asynctask基本使用
- c# vs2010 连接access数据库
- android Rxjava+Retrofit
- 时钟频率对程序效率影响测试(Platform: ARM ZYNQ 7000 ZP20 Board)
- PHP2.4+mysql5.7安装后的一个问题
- GNU C - 一个别致的HelloWorld程序 引申到: __attribute__((constructor)|(destructor)(PRIORITY))
- 【webservice】Axis2 客户端调用 设置超时时间
- SQL总结(快速参考)
- Nginx的下载、安装、启动|重启|关闭-虚拟机下操作
- 重写onKeyDown方法,监控返回键、菜单键和Home键