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;
}