How to import data from an excel table to an SQL server database

Source: Internet
Author: User
C; \ 11.xls is the content in an excel table.
Type_Name \ Type_Order \ Type_Check
Martial arts \ 0 \ 1

There is a database shop in SQL server2000, and a table Goods_Type has a field.
Type_Id/Type_Name/Type_text/Type_Order/Type_Check/Type_time
In this example, Type_Id is an auto-incrementing int type, and Type_Time is the getdate () of the insertion time ()
I want to create a Web form and use a button to implement the import function,
I used a button control and inserted the code I wrote in the event:
Private void Button6_Click (object sender, System. EventArgs e)
{
Int InsertOk = Base. Insert ();
If (InsertOk = 1)
{

JScript. Alert ("successful, welcome! ");

}
Else
{
JScript. Alert ("failed. Please come back later! ");
Return;
}
}
Base. Insert ();
In a Base. cs File
Public int Insert ()
{
Int flag = 0;
Cmd. commandText = string. format ("SELECT * into Goods_Type from OpenDataSource ('Microsoft. jet. OLEDB.4.0 ', 'Data Source = C: \ 11.xls; Extended Properties = Excel 8.0 ')... goods_Type $ )");
Try
{
Conn. Open ();
Cmd. ExecuteNonQuery ();
Flag = 1;
}
Catch (Exception ex)
{
// Throw (ex );
}
Finally
{
Cmd. Dispose ();
Conn. Close ();
}

Return flag;
}

If throw (ex) is not commented out, a syntax error will be prompted during running, near throw (ex ).
I comment it out and it will pop up.
Failed. Please try again later

Protected DataTable GetDataTableFromExcel (string FileName)
{
// Create a connection string
String strConn = "Provider = Microsoft. Jet. OLEDB.4.0;" +
"Data Source =" + Server. MapPath (FileName) + ";" +
"Extended Properties = Excel 8.0 ;";
OleDbConnection objConn = new OleDbConnection (strConn );
OleDbCommand objCmd = new OleDbCommand ("SELECT * FROM [Sheet1 $]", objConn );
OleDbDataAdapter objDA = new OleDbDataAdapter (objCmd );
DataTable objDt = new DataTable ();
ObjDA. Fill (objDt );
Return objDt;
}

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.