Read Excel records and import SQL database

Source: Internet
Author: User

To prepare, you need to import Excel data into the database in the near period.

Reference namespaces:

using System.Configuration; using System.Data; using System.Data.OleDb; using System.Data.SqlClient;

You can write a method that reads a method of an Excel document and returns a DataSet dataset:

 private  DataSet importexceltodataset ( String  virtualpath, string   sqlquerystatement) { string  excelconnectionstring = DB.        Excelconnectionstring (Server.MapPath (virtualpath)); OleDbConnection dc  = new   OleDbConnection        (excelconnectionstring); OleDbDataAdapter da  = new          OleDbDataAdapter (Sqlquerystatement, DC);        DataSet ds  = new   DataSet (); Da.        Fill (DS);     return   DS; }
Source Code


To import a database, in a database, you need to create a table to store the data imported by Excel:



Next, you can make the SqlBulkCopy method replicate the database:

Try        {            stringCS = configurationmanager.connectionstrings["insussqlconnectionstring"].            ConnectionString; using(SqlConnection sqlconn =NewSqlConnection (CS)) {                stringSqlquerystatement ="SELECT [material],[plnt],[level],[item],[component],[object description] from [sheet1$]"; stringVirtualPath ="~/app_data/book1.xlsx"; DataSet DS=Importexceltodataset (virtualpath, sqlquerystatement); DataTable DT= ds. tables[0];                Sqlconn.open (); using(SqlBulkCopy SQLBC =NewSqlBulkCopy (sqlconn)) {SQLBC. DestinationTableName="BOM"; SQLBC. Columnmappings.add ("Material","Material"); SQLBC. Columnmappings.add ("plnt","plnt"); SQLBC. Columnmappings.add (" Level"," Level"); SQLBC. Columnmappings.add ("Item","Item"); SQLBC. Columnmappings.add ("Component","Component"); SQLBC. Columnmappings.add ("Object Description","Object Description"); SQLBC.                    WriteToServer (DT); Response.Write ("Data Import Successful! "); }            }        }        Catch(Exception ex) {Throwex; }
Source Code


Demonstrate:

Read Excel records and import SQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.