zoukankan      html  css  js  c++  java
  • 基于excel导入数据到ms sql server

    OLE DB (Object Linking and Embedding, Database, sometimes written as OLEDB or OLE-DB) an API designed by Microsoft providing a set of interfaces that allows accessing data from a variety of sources that do not necessarily implement SQL.

    using System;
    using System.Data.OleDb;
    using System.Data.SqlClient;
     
    namespace ExcelMigration
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Excel File Path Location
                string excelfilepath = @"C:Users
    akesh.kumarDocumentsemployee.xls";
     
                // SQL Server TableName
                string TableName = "Sample";
     
                // Make Sure Your Sheet Name And Columns Names Are Correct, here sheet name is sheet4
                string exceldataquery = "select FirstName,LastName,Department,Gender from [Sheet1$]";
                try
                {
                    // Excel Connection String and SQL Server Connection String
                    string excelconnectionstring = @"provider=microsoft.jet.oledb.4.0;
                          data source=" + excelfilepath + 
                          ";extended properties=" + ""excel 4.0;hdr=yes;"";
                    string sqlconnectionstring = @"server=(localdb)ProjectsV13; 
                        database = TestDB; connection reset = false";
     
                    //Execute A Query To Drase Any Previous Data From Employee Table
                    string deletesqlquery = "delete from " + TableName;
                    SqlConnection sqlconn = new SqlConnection(sqlconnectionstring);
                    SqlCommand sqlcmd = new SqlCommand(deletesqlquery, sqlconn);
     
                    sqlconn.Open();
                    sqlcmd.ExecuteNonQuery();
                    sqlconn.Close();
     
                    // Build A Connection To Excel Data Source And Execute The Command
                    OleDbConnection oledbconn = new OleDbConnection(excelconnectionstring);
                    OleDbCommand oledbcmd = new OleDbCommand(exceldataquery, oledbconn);
                    oledbconn.Open();
                    OleDbDataReader dr = oledbcmd.ExecuteReader();
     
                    // Connect To SQL Server DB And Perform a Bulk Copy Operation
                    SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
     
                    // Provide Excel To Table Column Mapping If Any Difference In Name
                    bulkcopy.ColumnMappings.Add("FirstName", "FirstName");
                    bulkcopy.ColumnMappings.Add("LastName", "LastName");
                    bulkcopy.ColumnMappings.Add("Department", "Department");
                    bulkcopy.ColumnMappings.Add("Gender", "Gender");
     
                    // Provide The Table Name For Bulk Copy
                    bulkcopy.DestinationTableName = TableName;
     
                    while (dr.Read())
                    {
                        bulkcopy.WriteToServer(dr);
                    }
     
                    oledbconn.Close();
                }
                catch (Exception ex)
                {
                    //handle exception
                }
            }
        }
    }

    http://www.codemog.com/how-to-import-data-from-excel-sheet-into-a-sql-server-database-using-csharp/

  • 相关阅读:
    ajax中的application/x-www-form-urlencoded中的使用
    XMLHttpRequest的POST中文表单问题解决方案
    Address already in use: JVM_Bind<null>:80
    javascript-XMLHttpRequest
    The web application [/codeMarket] registered the JBDC driver[.........] but failed to unregister it when the web application was stopped. To prevent
    cookie的一些细节
    js操作cookie
    javascript与服务器3
    javascript与服务器1
    javascript与服务器2
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/10149283.html
Copyright © 2011-2022 走看看