本人是新手,以下代码不是很明白,所以还望大家多多指教!
代码有错!
protected void btnUpload_Click(object sender, EventArgs e)
{
    string filename = FileUpload1.PostedFile.FileName;
    /*string virtualName = "Temp/" + filename;
    string localFilename = Server.MapPath(virtualName);
    this.FileUpload1.SaveAs(localFilename);*/
    this.Label1.Text = filename;
    string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + filename + "';Extended Properties=Excel 8.0";
    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 ="";
        string CnnString = "Provider=PC-201103261848;Data Source=jieguo;Initial Catalog=pingjiaoMS;User Id=sa;Password=123";
        OleDbConnection conn = new OleDbConnection(CnnString);
        conn.Open();
        OleDbCommand myCmd = null;
        for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
        {
            strSql = "insert into Web_Processing (SourceID,Operator,SourceName) values ('";
            strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
            strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '";
            strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "')";
            this.Label1.Text = strSql;
            try
            {
                myCmd = new OleDbCommand(strSql, conn);
                myCmd.ExecuteNonQuery();
                Label1.Text = "<script language=javascript>alert('数据导入成功.');</script>";
            }
            catch
            {
                Label1.Text = "<script language=javascript>alert('数据导入失败.');</script>";
            }
        }
        conn.Close();
    }}