九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Import MS Excel data to SQL Server table using C#

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ù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
請問一個SQL Server數(shù)據(jù)庫中最多可以存放多少個Table? MS-SQL Serv...
倒出數(shù)據(jù)庫用戶表結(jié)構(gòu)和一樣本數(shù)據(jù)到EXCEL中,供參考! MS-SQL Server / ...
MS SQL Server 備份與恢復(fù)詳解
在 Delphi 中用 FireDAC 連接 Excel
(C#)excel數(shù)據(jù)導(dǎo)入SqlServer中
Access、MSSQL、MYSQL數(shù)據(jù)庫之間有什么區(qū)別
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服