2013-06-12 123 views
4

enter image description here填充與

我已經工作這一基本形式,而現在從SQL Server CE表中獲取價值組合框,並打了我最新的絆腳石。 目前有效的方法如下:在文本框中輸入一個ID,單擊Load按鈕,選擇.jpg,然後在頂部的圖片框中顯示圖像(本例中爲攝像機)。

現在的問題,當你點擊Save按鈕,它使一個呼叫到一個名爲updatedata();

我相信這是寫作的形象和ID到我的SQL Server CE數據庫(我已經做了幾個方法時間在bin \ debug文件夾中,並且db已經變大)。該方法還調用另一個方法Connection();

現在,連接方法的想法是根據任何項目已被保存到數據庫,所以基本上每次我添加一個新的圖像時,選擇列表應刷新並可供選擇,目前它沒有做任何事情因爲我使用的代碼最初是爲SQL Server的正確實例編寫的,而不是CE。我試圖重構它的一些,但我現在正在得到下面的錯誤。

enter image description here

這裏是我的連接方法的代碼:

private void Connection() 
{ 
      //connect to the database and table 
      //selecting all the columns 
      //adding the name column alone to the combobox 

     try 
     { 
      string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"; 

      SqlCeConnection conn = new SqlCeConnection(connstr); 
      conn.Open(); 
      empadap1 = new SqlDataAdapter(); 
      empadap1.SelectCommand = new SqlCommand("SELECT * FROM test_table" 
       , conn); 
      dset = new DataSet("dset"); 
      empadap1.Fill(dset); 
      DataTable dtable;  
      dtable = dset.Tables[0]; 
      comboBox1.Items.Clear(); 
      foreach (DataRow drow in dtable.Rows) 
      { 
       comboBox1.Items.Add(drow[0].ToString()); 
       comboBox1.SelectedIndex = 0; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message);  
     } 
} 

問題
誰能ammend我的連接()方法的代碼做我想做的(即更新的組合框在SQL Server CE數據庫中找到所有保存的ID)或者建議一些新的代碼。

旁註
一旦我的組合框填充我將試圖譯碼所述「檢索」基於所選擇的ID按鈕,然後,這將在下面的第一個的第二圖片框顯示所選擇的圖像,但是我認爲它如果我保留一個單獨的問題是最好的!

回答

1

嘗試SqlCeDataAdapterSqlCeCommand,而不是SqlDataAdapterSqlCommand

+0

嘗試這個。我得到: 無法隱式轉換類型「System.Data.SqlServerCe.SqlCeCommand」到「System.Data.SqlClient.SqlCommand」 \t 不能鍵入「System.Data.SqlServerCe.SqlCeDataAdapter」隱式轉換爲「System.Data .SqlClient.SqlDataAdapter' – JsonStatham

+0

Empadap在其他地方被聲明爲 – ErikEJ

0

我有同樣的問題(有關刷新)。我找到了一種方式,不好但工作。 只需

public ButtonSave_Click(...) 
{ 
    Connection(); 
    Application.Restart(); 
} 

這是足以讓我,但我不知道它會幫助你或不...

0

試試這個

private void Connection() 
{ 
      //connect to the database and table 
      //selecting all the columns 
      //adding the name column alone to the combobox 

     try 
     { 
      string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"; 

      SqlCeConnection conn = new SqlCeConnection(connstr); 
      conn.Open(); 
      SqlCeCommand selectCmd = conn.CreateCommand(); 
      selectCmd.CommandText = "SELECT * FROM test_table"; 
      SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd);    
      dset = new DataSet("dset"); 
      adp.Fill(dset); 
      DataTable dtable;  
      dtable = dset.Tables[0]; 
      if(comboBox1.Items.Count>0) 
      { 
       comboBox1.Items.Clear(); 
      } 
      foreach (DataRow drow in dtable.Rows) 
      { 
       comboBox1.Items.Add(drow[0].ToString());     
      } 
      comboBox1.SelectedIndex = 0; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message);  
     } 
     finally 
     { 
      conn.Open(); 
     } 
} 
+0

您能否將您的答案添加到我的Connection()代碼中,因爲我不知道它需要去哪裏以及需要替換的地方。還有什麼關於錯誤即時通訊= newSQLCommand? – JsonStatham

+0

嘗試上面的代碼 –

+0

你知道我的問題的一部分是,empadap1.SelectCommand =新的SqlCommand沒有編譯爲使用SQL-CE即時通訊和SqlCommand是SQL服務器?這仍然不會編譯。 – JsonStatham