2012-03-23 56 views
0

我有一個打開文件對話框,將假設選擇照片並把它保存到數據庫 但問題是,當我訪問上課的時候打開文件對話框的對話框結果是正常 它說,no such table : PhotoFile當SavePhoto函數調用時使用以下參數:錯誤影像保存到數據庫

TODO(PJ):把值這裏

這是我到目前爲止已經試過

OpenFileDialog d = new OpenFileDialog(); 

     d.Filter = ("JPEG Imange (*.jpg|*.jpg|PNG Image (*.png)|All Files*.*"); 
     if ((d.ShowDialog()) == DialogResult.OK) 
     { 
      SavePhoto(txtID.text,d.fileName); 
     } 

下面是該函數的代碼

 try { 
     using (SQLite.SQLiteConnection SQLConnect = new SQLite.SQLiteConnection(g_constring)) { 
      byte[] photo = FileImageToByte(PhotoFile); 
      SQLConnect.Open(); 
      if (SQLConnect.State == ConnectionState.Open) { 
       SQLiteCommand SQLcommand = new SQLiteCommand(SQLConnect); 
       SQLcommand = SQLConnect.CreateCommand; 
       SQLcommand.CommandText = "DELETE FROM PhotoFile WHERE PhotoID = '" + PhotoId + "'"; 
       SQLcommand.ExecuteNonQuery(); 
       SQLcommand.Parameters.Clear(); 

       SQLcommand.CommandText = "INSERT INTO PhotoFile(PhotoID, Photo) VALUES(@EmployeeID, @Photo1)"; 

       SQLiteParameter SQLparmID = new SQLiteParameter("@EmployeeID", PhotoId); 
       SQLparmID.DbType = DbType.String; 
       SQLparmID.Value = PhotoId; 
       SQLcommand.Parameters.Add(SQLparmID); 

       SQLiteParameter SQLparm = new SQLiteParameter("@Photo1", photo); 
       SQLparm.DbType = DbType.Binary; 
       SQLparm.Value = photo; 
       SQLcommand.Parameters.Add(SQLparm); 

       SQLcommand.ExecuteNonQuery(); 

       bReturn = true; 
      } else { 
       bReturn = false; 
      } 
     } 
    } catch (System.Exception eX) { 
     MessageBox.Show(eX.Message.ToString(), "Error in database", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     bReturn = false; 
    } 
    return bReturn; 
} 

PhotoFile表在我的數據庫事實上是存在的我已經嘗試了Windows窗體和觸發功能,如果dialog result = ok,但是當我用openFileDialog它總是產生錯誤的規定以上。

+1

這與OpenFileDialog有關嗎?看起來像SavePhoto的某些特定參數導致錯誤,而OpenFileDialog本身沒有問題。 – 2012-03-23 16:15:29

+0

你確定問題不在於你的功能嗎?應用程序到底在哪裏破壞? – Lander 2012-03-23 16:16:31

+3

只是想知道爲什麼'SavePhoto()'方法實際上'DELETE FROM' sql查詢? – sll 2012-03-23 16:16:44

回答

5

這不是什麼做的打開文件對話框本身,這是你的SQL查詢失敗,並指出該表不存在(Photofile) - 所以我建議,這不和你要麼檢查你的表名,或者在必要時創建它。

除此之外,您的查詢存在疑慮:您的方法表示照片將保存爲,但您仍使用DELETE。另外,如果表確實存在/當您設法正確地對錶名進行排序時,我建議您不要使用字符串作爲標識符。沒有節省任何地方。

+0

或檢查連接字符串。 – 2012-03-23 16:21:05

+1

ConnectionString me too。 – 2012-03-23 16:21:45

+1

@NickGotch如果連接字符串不正確,OP應該得到一個連接錯誤,除非它們連接到錯誤的數據庫。 – 2012-03-23 16:21:57

1

您的AppMain.ConnectionString指向錯誤的數據庫,該數據庫不包含任何PhotoTable表。你可能想仔細檢查一下。

順便說一下,您的代碼會刪除與提供的PhotoID匹配的記錄,它不會將任何內容保存到數據庫中。