簡體   English   中英

使用從SQL Server CE表中獲取的值填充組合框

[英]Populate a combobox with value taken from SQL Server CE table

在此輸入圖像描述

我已經在這個基本形式上工作了一段時間,並且遇到了我最新的絆腳石。 到目前為止的工作原理如下:在文本框中輸入ID,單擊“加載”按鈕,選擇.jpg然后在頂部的圖片框中顯示圖像(在本例中為攝像機)。

現在針對該問題,當您單擊“ Save按鈕時,它會調用名為updatedata();的方法updatedata();

我相信這是將圖像和ID寫入我的SQL Server CE數據庫(我已經在bin \\ debug文件夾中完成了幾次,並且db的大小已經增大。)該方法還調用另一個名為Connection();方法Connection();

現在,連接方法的想法是根據已保存到數據庫的任何項目填充ID組合框,所以基本上每次添加新圖像時,選項列表都應該刷新並可供選擇,目前它不是做任何事情,因為我使用的代碼最初是為適當的SQL Server實例編寫的,而不是CE。 我試圖重構其中的一些但是我現在得到以下錯誤。

在此輸入圖像描述

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

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);    
         }   
}


任何人都可以修改我的Connection()方法代碼來做我想做的事情(即用SQL Server CE數據庫中找到的所有已保存ID更新組合框)或建議一些新代碼。

邊注
一旦我的組合框填充,我將嘗試根據所選的ID對“檢索”按鈕進行編碼,然后在第一個圖片框下方的第二個圖片框中顯示所選圖像,但我認為最好還是保留一個單獨的問題!

嘗試使用SqlCeDataAdapterSqlCeCommand而不是SqlDataAdapterSqlCommand

我有同樣的問題(關於刷新)。 我找到了一種方法,不是很好但是有效。 只是

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

這對我來說已經足夠了,但我不知道它會幫助你嗎?

試試這個

    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();
         }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM