基于excel导入数据到ms sql server

时间:2021-10-20 06:38:03

 

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\rakesh.kumar\Documents\employee.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/