基于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\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/

转载于:https://www.cnblogs.com/chenqingwei/p/10149283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值