Import SQL Server database using Excel)

Source: Internet
Author: User

 

 

// Filename: absolute path of the Excel file

Private dataset readexcel2 (string filename)
{
If (filename. Trim (). Length = 0)
{
Return NULL;
}

If (! System. Io. file. exists (filename ))
{
Return NULL;
}


String strconn = "provider = Microsoft. Jet. oledb.4.0 ;"
+ "Data Source =" + filename + ";"
+ "Extended properties = Excel 8.0 ;";

Oledbconnection objconn = new oledbconnection (strconn );

// Archivecategorys is the name of the sheet in the Excel file

// Here, we can refer to execel.exe in the project,

// Use the Excel. Application, Excel. Workbook, Excel. worksheet, and Excel. Range pairs to obtain the desired information.

String strsql = "select ID, PID, name, code, memo, locked, deleted from archivecategorys order by ID ";

Oledbcommand objcmd = new oledbcommand (strsql, objconn );
Oledbdataadapter adapter = new oledbdataadapter (objcmd );

Try
{
Objconn. open ();

Adapter. Fill (DS );
Return Ds;
}
Catch (exception exc)
{
MessageBox. Show (EXC. tostring ());
}
Finally
{
Objconn. Dispose ();
}

Return Ds;

}

// Traverse all tables in dataset and all rows in the table:

Foreach (datatable table in DS. Tables)
{
Foreach (datarow row in table. Rows)
{

}
}

// The dataset data is stored in the database or is not written in other ways.

// Refer to an article on csdn and forget the address. It is in English.

// Haha, this is the first article I posted online. please correct me.

 

Related Article

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.