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

  • 相关阅读:
    INFORMATION_SCHEMA.COLUMNS表的字段信息
    如何取得一个数据表的所有列名
    CASE 函数
    Js定制窗口
    获取当前数据库中的所有用户表
    Js让状态栏不显示链接地址
    RA病人关节残障与软骨破坏而非骨破坏相关
    抗阿达木单抗的抗体可能与阿达木单抗治疗过程中静脉和动脉血栓事件相关
    长期应用阿达木单抗时所产生的抗抗体会影响疗效
    多普勒超声预测抗TNFα治疗类风湿关节炎患者的有效性:一项前瞻性队列研究
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/10149283.html
Copyright © 2020-2023  润新知