2014-10-11 76 views
1

我在Visual Studio 2010中工作,我試圖通過網頁將文檔上載到訪問數據庫。當我運行我的代碼時,我沒有收到任何錯誤,但沒有寫入數據庫。這是我的點擊代碼,以顯示我認爲它應該做的事情。使用c#將文檔上傳到Access數據庫

protected void btnUpload_Click(object sender, EventArgs e) 
    { 
     if (FileUpload1.HasFile) 
     { 
      string fileExtension = Path.GetExtension(FileUpload1.FileName); 

      if (fileExtension.ToLower() != ".doc" || fileExtension.ToLower() != ".docx" || fileExtension.ToLower() != ".pdf") 
      { 
       lblInfo.Text = "Only .doc, .docx, or .pdf files are allowed."; 
       lblInfo.ForeColor = System.Drawing.Color.Red; 
      } 
      else 
      { 
       int fileSize = FileUpload1.PostedFile.ContentLength; 
       if (fileSize > 2097152) 
       { 
        lblInfo.Text = "Maximum file size of 2 MB exceeded."; 
        lblInfo.ForeColor = System.Drawing.Color.Red; 
       } 
       else 
       { 
        OleDbCommand update = new OleDbCommand("Update STAFF SET Resume = @Resume WHERE [email protected]", DBConnection); 
        update.Parameters.Add("@Resume", OleDbType.LongVarBinary).Value = FileUpload1.FileContent; 
        update.Parameters.Add("@StaffID", OleDbType.Integer).Value = txtStaffID.Text; 
        lblInfo.Text = "File Uploaded"; 
        lblInfo.ForeColor = System.Drawing.Color.Green; 
       } 
      } 
     } 
     else 
     { 
      lblInfo.Text = "Please select a file to upload"; 
      lblInfo.ForeColor = System.Drawing.Color.Red; 
     } 

    } 

如果您可以提供任何意見或建議,將是偉大的。謝謝。我還會顯示整個代碼,以防萬一它是數據庫連接的問題。

public partial class Staff : System.Web.UI.Page 
{ 
    OleDbConnection DBConnection = new OleDbConnection(); 
    OleDbDataAdapter DataAdapter; 
    DataTable LocalDataTable = new DataTable(); 

    private void ConnectToDatabase() 
    { 
     DBConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CIS470_TPS_System\CIS470_TPS_System\CIS470_TPS_System\App_Data\TpsSystem_DB.mdb"; 
     DBConnection.Open(); 
     DataAdapter = new OleDbDataAdapter("Select * From STAFF", DBConnection); 
     DataAdapter.Fill(LocalDataTable); 
    } 

    private void Page_Load(object sender, EventArgs e) 
    { 
     ConnectToDatabase(); 
    } 

    protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 

    } 

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string requestId = GridView1.SelectedRow.Cells[1].Text; 
     txtSelectedStaff.Text = requestId; //this control holds the selected value 
    } 

    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e) 
    { 

    } 
+0

此次榮獲」 t工作,因爲你剛剛使用OleDbCommand,但沒有執行它。嘗試在「lblInfo.Text =」File Uploaded「;」之前使用update.ExecuteNonQuery()行,然後檢查。 ;) – 2014-10-11 06:02:01

+0

@HiteshMistry感謝您的建議,但之後,我按下上傳時出現錯誤。它說InvalidCastException由用戶代碼處理。無法將參數值從HttpInputStream轉換爲字節[]。 – 2014-10-11 06:29:41

+0

嘿道格拉斯,只需寫FileUpload1.FileBytes而不是FileUpload1.FileContent,試着讓我知道它是否解決了這個問題。我無法做到這一點,因爲我沒有安裝在我的機器上。 :| – 2014-10-11 09:18:57

回答

0

正如評論的問題的建議,我們可以使用FileUpload控件的.FileBytes財產提供查詢參數的值,如本(簡體)例如:

protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    using (var con = new OleDbConnection()) 
    { 
     con.ConnectionString = 
       @"Provider=Microsoft.ACE.OLEDB.12.0;" + 
       @"Data Source=C:\__tmp\staffDb.accdb;"; 
     con.Open(); 
     using (var cmd = new OleDbCommand()) 
     { 
      cmd.Connection = con; 
      cmd.CommandText = 
        "UPDATE STAFF SET Resume=? " + 
        "WHERE StaffID=?"; 
      cmd.Parameters.AddWithValue("?", FileUpload1.FileBytes); 
      cmd.Parameters.AddWithValue("?", 1); 
      cmd.ExecuteNonQuery(); 
     } 
     con.Close(); 
    } 
} 
相關問題