如何高效的将excel导入sqlserver?

 大部分人都知道用oledb来读取数据到dataset,但是读取之后怎么处理dataset就千奇百怪了。很多人通过循环来拼接sql,这样做不但容易出错而且效率低下,System.Data.SqlClient.SqlBulkCopy 对于新手来说还是比较陌生的,这个就是传说中效率极高的bcp,6万多数据从excel导入到sql只需要4.5秒。 

[c-sharp] view plaincopy
 
  1. using System;  
  2. using System.Data;  
  3. using System.Windows.Forms;  
  4. using System.Data.OleDb;  
  5. namespace WindowsApplication2  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void button1_Click(object sender, EventArgs e)  
  15.         {  
  16.             //测试,将excel中的sheet1导入到sqlserver中  
  17.             string connString = "server=localhost;uid=sa;pwd=sqlgis;database=master";  
  18.             System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();  
  19.             if (fd.ShowDialog() == DialogResult.OK)  
  20.             {  
  21.                 TransferData(fd.FileName, "sheet1", connString);  
  22.             }  
  23.         }  
  24.   
  25.         public void TransferData(string excelFile, string sheetName, string connectionString)  
  26.         {  
  27.             DataSet ds = new DataSet();  
  28.             try  
  29.             {  
  30.                 //获取全部数据  
  31.                 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";  
  32.                 OleDbConnection conn = new OleDbConnection(strConn);  
  33.                 conn.Open();  
  34.                 string strExcel = "";  
  35.                 OleDbDataAdapter myCommand = null;  
  36.                 strExcel = string.Format("select * from [{0}$]", sheetName);  
  37.                 myCommand = new OleDbDataAdapter(strExcel, strConn);  
  38.                 myCommand.Fill(ds, sheetName);  
  39.   
  40.                 //如果目标表不存在则创建  
  41.                 string strSql = string.Format("if object_id('{0}') is null create table {0}(", sheetName);  
  42.                 foreach (System.Data.DataColumn c in ds.Tables[0].Columns)  
  43.                 {  
  44.                     strSql += string.Format("[{0}] varchar(255),", c.ColumnName);  
  45.                 }  
  46.                 strSql = strSql.Trim(',') + ")";  
  47.   
  48.                 using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))  
  49.                 {  
  50.                     sqlconn.Open();  
  51.                     System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();  
  52.                     command.CommandText = strSql;  
  53.                     command.ExecuteNonQuery();  
  54.                     sqlconn.Close();  
  55.                 }  
  56.                 //用bcp导入数据  
  57.                 using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))  
  58.                 {  
  59.                     bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);  
  60.                     bcp.BatchSize = 100;//每次传输的行数  
  61.                     bcp.NotifyAfter = 100;//进度提示的行数  
  62.                     bcp.DestinationTableName = sheetName;//目标表  
  63.                     bcp.WriteToServer(ds.Tables[0]);  
  64.                 }  
  65.             }  
  66.             catch (Exception ex)  
  67.             {  
  68.                 System.Windows.Forms.MessageBox.Show(ex.Message);  
  69.             }  
  70.   
  71.         }  
  72.   
  73.         //进度显示  
  74.         void bcp_SqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)  
  75.         {  
  76.             this.Text = e.RowsCopied.ToString();  
  77.             this.Update();  
  78.         }  
  79.   
  80.   
  81.     }  
  82. }   

上面的TransferData基本可以直接使用,如果要考虑周全的话,可以用oledb来获取excel的表结构,并且加入ColumnMappings来设置对照字段,这样效果就完全可以做到和sqlserver的dts相同的效果了。

原文地址:https://www.cnblogs.com/ranzige/p/3954076.html