每篇随便都得有个背景吧,这次做一个项目时,突然碰到这个样一个问题,需要将本地sqlserver中的 数据 导出 到access后,再传输access 数据 库,所以就在想怎样实现这样的操作。后面经过在网上查找了一些资料结合以前的知识,搞了这样一个东西出来; 1 /// su
每篇随便都得有个背景吧,这次做一个项目时,突然碰到这个样一个问题,需要将本地sqlserver中的数据导出到access后,再传输access数据库,所以就在想怎样实现这样的操作。后面经过在网上查找了一些资料结合以前的知识,搞了这样一个东西出来;
1 /// 2 /// 从sqlserver中导出数据到access 3 /// state=0 jzjl 导出菜品信息表 4 /// state=1 lbxf_jz 导出收银信息表 5 /// 6 /// 7 /// 8 /// 9 /// 10 static void backupa(string tablename, int state, string connection = data source=.;initial catalog=mpcy;integrated security=true) 11 { 12 string path = system.appdomain.currentdomain.setupinformation.applicationbase; 13 string file = mpcy.model; 14 string root = path + file; 15 16 console.writeline(文件路径: + root); 17 //备份access数据库 18 if (state == 0) { tablename = jzjl; chkandcrt(root); } else { tablename = lbxf_jz; } 19 20 root = bakpath + \\mpcy.dat; 21 string accesssqlconn = @provider=microsoft.ace.oledb.12.0;jet oledb:database password=xhjxjf168;data source= + root + ;; 22 //sqlhelper.connectionstring = string.format(data source={0};initial catalog={1};user id={2};password={3}, 127.0.0.1, mpcytemp, sa, 12345); 23 //connection = string.format(data source=.;initial catalog={0};integrated security=true, mpcytemp); 24 sqlhelper.connectionstring = connection; 25 //从sqlserver中读出数据到datatable 26 oledbconnection conn = new oledbconnection(accesssqlconn); 27 oledbcommand cmd = conn.createcommand(); 28 try 29 { 30 datatable user = sqlhelper.executetable(commandtype.text, string.format(select * from {0}, tablename), null); 31 int i = 0; 32 if (user.rows.count > 0) 33 { 34 using (conn) 35 { 36 using (cmd) 37 { 38 conn.open(); 39 40 //每次导入前,先清空数据库表 41 string sql = string.format(delete * from {0}, tablename); cmd.commandtext = sql; cmd.executenonquery(); 42 43 oledbdataadapter adp = new oledbdataadapter(); adp.selectcommand = new oledbcommand(string.format(select * from {0}, tablename), conn); oledbcommandbuilder cb = new oledbcommandbuilder(adp); dataset data = new dataset(); 44 //加载access中的数据表,并通过追加的方式放入dataset中 45 adp.fill(data); 46 for (int j = 0; j ) 47 { 48 //for (int l = 0; l 49 //{ 50 // if (l > user.columns.count - 1) 51 // break; 52 datarow dr = data.tables[0].newrow(); 53 for (int k = 0; k ) 54 { 55 type typ = user.rows[j][k].gettype(); 56 if (typ == typeof(double) || typ == typeof(int) || typ == typeof(decimal) || typ == typeof(float)) 57 { if (string.isnullorempty(user.rows[j][k].tostring())) { dr[k] = 0; } else { dr[k] = user.rows[j][k].tostring(); } } 58 else 59 { if (string.isnullorempty(user.rows[j][k].tostring())) { dr[k] = ; } else { dr[k] = user.rows[j][k].tostring(); } } 60 } 61 //dr[userid] = user.rows[j][l].tostring(); 62 //dr[username] = user.rows[j][l + 1].tostring(); 63 //网dataset中添加数据 64 data.tables[0].rows.add(dr); 65 //} 66 } 67 data.tables[0].tablename = tablename; 68 i = adp.update(data.tables[0]); //通过update方法insert或update数据 69 console.writeline(i.tostring()); 70 } 71 } 72 } 73 } 74 //判断异常类型 75 catch (sqlexception er) 76 { 77 errortime++; 78 if (errortime > 3) 79 { filelog.logger.write(er); filelog.logger.write(错误次数太多,退出程序!); console.writeline(错误次数太多,退出程序!); } 80 else 81 { 82 if (errortime % 2 == 0) 83 { 84 filelog.logger.write(尝试使用mssql身份验证登陆数据库!); filelog.logger.write(er); console.writeline(sqlserver打开失败,尝试使用mssql方式登录数据库。); 85 string sqlcon = string.format(data source={0};initial catalog={1};user id={2};password={3}, confighelper.dbhost, mpcy, confighelper.dbname, confighelper.dbpwd); 86 //string sqlcon = string.format(data source={0};initial catalog={1};user id={2};password={3}, 127.0.0.1, mpcytemp, sa, 12345); 87 backupa(, state, sqlcon); 88 } 89 else 90 { filelog.logger.write(尝试使用windows身份验证登陆数据库!); filelog.logger.write(er); console.writeline(sqlserver打开失败,尝试使用windows方式登录数据库。); backupa(, state); } 91 } 92 } 93 catch (exception e) 94 { 95 filelog.logger.write(数据导出失败!); filelog.logger.write(e); console.writeline(数据导出失败!); 96 //throw (new exception(数据导出失败,请联系it部!)); 97 } 98 finally 99 {100 conn.close();101 }102 if (state == 0)103 {104 string sqlcon = string.format(data source={0};initial catalog={1};user id={2};password={3}, confighelper.dbhost, mpcytemp, confighelper.dbname, confighelper.dbpwd);105 //string sqlcon = string.format(data source={0};initial catalog={1};user id={2};password={3}, 127.0.0.1, mpcytemp, sa, 12345);106 backupa(, 1, sqlcon);107 }108 }109 110 /// 111 /// 检查并备份112 /// 113 /// 114 /// 115 static void chkandcrt(string root)116 {117 try { if (!directory.exists(bakpath)) { directory.createdirectory(bakpath); } if (!file.exists(bakpath + \\mpcy.dat)) { file.copy(root, bakpath + \\mpcy.dat); } }118 catch (exception e)119 { filelog.logger.write(e); filelog.logger.write(文件检查失败!); console.writeline(文件信息检查失败!); }120 }
代码不多,核心代码就这几句:
oledbdataadapter adp = new oledbdataadapter(); adp.selectcommand = new oledbcommand(string.format(select * from {0}, tablename), conn); oledbcommandbuilder cb = new oledbcommandbuilder(adp); dataset data = new dataset(); 44 //加载access中的数据表,并通过追加的方式放入dataset中 45 adp.fill(data); 46 for (int j = 0; j ) 47 { 48 //for (int l = 0; l 49 //{ 50 // if (l > user.columns.count - 1) 51 // break; 52 datarow dr = data.tables[0].newrow(); 53 for (int k = 0; k ) 54 { 55 type typ = user.rows[j][k].gettype(); 56 if (typ == typeof(double) || typ == typeof(int) || typ == typeof(decimal) || typ == typeof(float)) 57 { if (string.isnullorempty(user.rows[j][k].tostring())) { dr[k] = 0; } else { dr[k] = user.rows[j][k].tostring(); } } 58 else 59 { if (string.isnullorempty(user.rows[j][k].tostring())) { dr[k] = ; } else { dr[k] = user.rows[j][k].tostring(); } } 60 } 61 //dr[userid] = user.rows[j][l].tostring(); 62 //dr[username] = user.rows[j][l + 1].tostring(); 63 //网dataset中添加数据 64 data.tables[0].rows.add(dr); 65 //} 66 } 67 data.tables[0].tablename = tablename; 68 i = adp.update(data.tables[0]); //通过update方法insert或update数据 69 console.writeline(i.tostring());
这中间是这样的:先通过一个dataadapter读取了access数据库的结构,然后再通过从sqlserver中读取到数据的datatable将数据导入到这个被dataadapter填充了结构的dataset的表中。最后,通过这个dataadapter来更新整个表,当然,表的结构要一致!!
另外,这里有一个oledbcommandbuilder,根据网上解释:自动生成用于协调对 dataset的更改与关联数据库的单表命令。也就是说,在我们这个例子中,我们的表结构是没有改变,但是后面新添加了数据,于是这个oledbcommandbuilder就会自动将表单命令生成为insert语句,于是就执行了插入命令,但是如果在我们初始化的dataset中本来就存在数据,然后我们进行了对原始数据的改变,后面就会将命令变成update。
这样,就将sqlserver中的数据填充到了access数据库中了。
参考:http://bbs.bccn.net/thread-292655-1-1.html
http://blog.csdn.net/a3676212/article/details/2776027
http://www.cnblogs.com/rhythmk/archive/2010/07/19/1780874.html