protected void Button1_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('資料匯入失敗.');</script>");
}
}
Response.Write( "<script language='javascript'>alert('資料匯入成功.');</script>");
conn.Close();
}
}