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
原创粉丝点击