问题描述
在C#中如何做数据导入或合并功能,请做过这方面功能的大侠们留下代码??????
解决方案
解决方案二:
publicvoidGetExcelToDB(stringtbName){//将EXCEL导入数据库,tbname为传入的数据库表名stringfilename="";stringsqlTxt="";OpenFileDialogopenFileDialog=newOpenFileDialog();openFileDialog.DefaultExt="xls";openFileDialog.Filter="xls|*.xls";openFileDialog.ShowDialog();filename=openFileDialog.FileName;if(filename=="")return;sqlTxt="insertinto"+tbName+"select*fromOPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel8.0;HDR=YES;IMEX=1;DATABASE="+filename+"','select*from[sheet1$]')";SqlTransactiontrans=null;//OPENROWSETDBdbGetExcelToDB=newDB();//DB包含cmd,conn等的一个类你可以自己改下代码try{dbGetExcelToDB.GetConnction();trans=dbGetExcelToDB.conn.BeginTransaction();dbGetExcelToDB.cmd.Connection=dbGetExcelToDB.conn;dbGetExcelToDB.cmd.Transaction=trans;dbGetExcelToDB.cmd.CommandText="deletefrom"+tbName;dbGetExcelToDB.cmd.ExecuteNonQuery();dbGetExcelToDB.cmd.CommandText=sqlTxt;introws=dbGetExcelToDB.cmd.ExecuteNonQuery();trans.Commit();MessageBox.Show("导入成功!共导入"+rows+"条记录!");}catch(SqlExceptionex){trans.Rollback();throwex;}finally{dbGetExcelToDB.ShutDownConn();}}