一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Import MS Excel data to SQL Server table using C#

 woodsway 2020-07-28

Introduction

If you already have data in MS Excel file, and want to migrate your MS Excel data to SQL Server table, follow the below steps:

Step 1: Let’s take an example to import data to SQL Server table. I am going to import student information data from an MS Excel sheet to the tStudent SQL table:

Step 2: Now design a tStudent table in SQL Server

Hide   Copy Code
CREATE TABLE
(
STUDENT VARCHAR(64),
ROLLNO VARCHAR(16),
COURSE VARCHAR(32),
)

Your MS Excel sheet and SQL table are ready, now it’s time to write C# code to import the Excel sheet into the tStudent table.

Step 3: Add these two namespaces in your class file:

Hide   Copy Code
USING SYSTEM.DATA.OLEDB;
USING SYSTEM.DATA.SQLCLIENT;

Step 4: Add below method in your class file, you can call this method from any other class and pass the Excel file path:

Hide   Shrink   Copy Code
public void importdatafromexcel(string excelfilepath)
{
    //declare variables - edit these based on your particular situation
    string ssqltable = "tdatamigrationtable";
    // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have
    different
    string myexceldataquery = "select student,rollno,course from [sheet1$]";
    try
    {
        //create our connection strings
        string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath +
        ";extended properties=" + "\"excel 8.0;hdr=yes;\"";
        string ssqlconnectionstring = "server=mydatabaseservername;user
        id=dbuserid;password=dbuserpassword;database=databasename;connection reset=false";
        //execute a query to erase any previous data from our destination table
        string sclearsql = "delete from " + ssqltable;
        sqlconnection sqlconn = new sqlconnection(ssqlconnectionstring);
        sqlcommand sqlcmd = new sqlcommand(sclearsql, sqlconn);
        sqlconn.open();
        sqlcmd.executenonquery();
        sqlconn.close();
        //series of commands to bulk copy data from the excel file into our sql table
        oledbconnection oledbconn = new oledbconnection(sexcelconnectionstring);
        oledbcommand oledbcmd = new oledbcommand(myexceldataquery, oledbconn);
        oledbconn.open();
        oledbdatareader dr = oledbcmd.executereader();
        sqlbulkcopy bulkcopy = new sqlbulkcopy(ssqlconnectionstring);
        bulkcopy.destinationtablename = ssqltable;
        while (dr.read())
        {
            bulkcopy.writetoserver(dr);
        }
     
        oledbconn.close();
    }
    catch (exception ex)
    {
        //handle exception
    }
}

In the above function you have to pass the MS Excel file path as a parameter. If you want to import your data by providing the client access to select the Excel file and import, then you might have to use the ASP.NET File control and upload the Excel file on the server in some temp folder, then use the file path of the uploaded Excel file and pass the path in the above function. Once data import is complete then you can delete the temporary file.

The above method first deletes the existing data from the destination table, then imports the Excel data into the same table.

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    亚洲一区二区三区国产| 尤物久久91欧美人禽亚洲| 精品亚洲av一区二区三区| 国产传媒欧美日韩成人精品| 日韩性生活片免费观看| 欧美日韩综合在线精品| 国产av一区二区三区麻豆| 日韩成人中文字幕在线一区| 国产成人高清精品尤物| 亚洲天堂精品1024| 午夜精品久久久免费视频| 日本免费一级黄色录像| 蜜桃av人妻精品一区二区三区| 日韩国产亚洲欧美另类 | 视频一区日韩经典中文字幕| 午夜视频免费观看成人| 亚洲色图欧美另类人妻| 高清国产日韩欧美熟女| 国产精品一区二区视频成人| 午夜视频成人在线免费| 午夜亚洲少妇福利诱惑| 国产免费一区二区不卡| 激情三级在线观看视频| 亚洲国产一区精品一区二区三区色 | 日韩精品视频高清在线观看| 高清一区二区三区不卡免费| 精品欧美国产一二三区| 国产成人免费激情视频| 国产福利一区二区久久| 有坂深雪中文字幕亚洲中文| 精品久久久一区二区三| 加勒比日本欧美在线观看| 激情综合网俺也狠狠地| 国产午夜免费在线视频| 国产欧美高清精品一区| 人妻露脸一区二区三区| 国产精品午夜一区二区三区| 亚洲一区二区精品福利| 日韩综合国产欧美一区| 日韩在线欧美一区二区| 一区二区三区日韩经典|