Protected void button#click (Object sender, eventargs E)
{
String sqlconn = system. Web. configuration. webconfigurationmanager. connectionstrings ["sqlconn"]. connectionstring;
String mystring = "provider = Microsoft. Jet. oledb.4.0; Data Source = E:/examples.xls; extended properties = 'excel 8.0; HDR = yes; IMEX = 1 '";
Oledbconnection cnnxls = new oledbconnection (mystring );
Oledbdataadapter myda = new oledbdataadapter ("select * from [sheet1 $]", cnnxls );
Dataset myds = new dataset ();
Myda. Fill (myds );
Cnnxls. Close ();
If (myds. Tables [0]. Rows. Count> 0)
{
String strsql = "";
Sqlconnection conn = new sqlconnection (sqlconn );
Conn. open ();
Sqlcommand mycmd = NULL;
For (INT I = 0; I <myds. Tables [0]. Rows. Count; I ++)
{
Strsql = "insert into Hitch (hname, num) values ('";
Strsql + = myds. Tables [0]. Rows [I]. itemarray [0]. tostring () + "','";
Strsql + = myds. Tables [0]. Rows [I]. itemarray [1]. tostring () + "')";
Try
{
Mycmd = new sqlcommand (strsql, Conn );
Mycmd. executenonquery ();
}
Catch
{
Response. Write ("<script language = 'javascript '> alert ('data import failed.'); </SCRIPT> ");
}
}
Response. Write ("<script language = 'javascript '> alert ('data imported successfully.'); </SCRIPT> ");
Conn. Close ();
}
}