2012-10-01 24 views
-1

即時通訊工作在WinForms C#中。損壞的數據庫和列表框問題

由於某些原因,當我想填充我的listBox它停止並說我的數據庫已損壞。 我已經添加了一條修理線,代碼隨後運行,但沒有任何事情發生。我的列表框不填充。

下面是使用:

public void button1_Click(object sender, EventArgs e) 
    { 
     SqlCeConnection cn = new SqlCeConnection(@"Data Source = Database1.mdf"); 
     cn.Open(); 
     SqlCeCommand cm = new SqlCeCommand("SELECT * FROM tblprojects ORDER BY Projekt_liste ASC", cn); 

     try 
     { 
      SqlCeDataReader dr = cm.ExecuteReader(); 

      while (dr.Read()) 
      { 
       ListBox project_list = Application.OpenForms["Form1"].Controls["tabControl1"].Controls["tabPage1"].Controls["Project_list"] as ListBox; 
       project_list.Items.Add(dr["Projekt_liste"].ToString()); 
      } 
      cn.Close(); 
      cn.Dispose(); 
     } 
     catch (Exception ex) 
     { 
     } 
    } 

    public void button2_Click(object sender, EventArgs e) 
    { 

     SqlCeConnection cn = new SqlCeConnection(); 

     SqlCeEngine engine = new SqlCeEngine("Data Source = Database1.mdf"); 

     if (false == engine.Verify()) 
     { 
      MessageBox.Show("Database is corrupted."); 
      engine.Repair(null, RepairOption.RecoverAllPossibleRows); 
     } 
    } 
+0

看看這個鏈接以及 http://www.mikeborozdin.com/post/Introducing-Microsoft-SQL-Server-Compact-Edition-%28Part-I%29.aspx – MethodMan

+0

如果你是隻填充ListBox的值從1行我建議將您的SELECT *更改爲「Select Projekt_liste FROM tblprojects ORDER BY Projekt_liste ASC」 – MethodMan

+0

謝謝。生病了,記住。即時通訊仍在致力於您發送給我的鏈接。 –

回答

1

例如,如果要加載的項目

1. make sure you have a ListBox on the winform 
2. name the ListBox 
3. Create a ListItem 
4 Add the ListItem to the ListBox 

while(dr.Read()) 
{ 
    ListViewItem obj=new ListViewItem(Convert.ToString(dr[0]),Convert.ToString(dr[1]); 
    //in object of ListViewItem give display member at first and give value member at second position 
    listView1.Items.Add(obj); // add object to the listbox 
} 

下面是您可以使用,以及展示如何填充一個列表框

一個不同的方式鏈接數爲Windows,且其他的將是,如果你正在使用或計劃使用ASP.NET

Populate a ListBox when using SQLDataReader

asp.net SqlDataReader example: how to use Read() method to populate ListBox

Populate ASP.NET ListBox using SqlDataReader

1

代碼IM從Microsoft Site

修復方法不能保證完整的數據恢復爲每個 數據庫。不管 應用程序選擇的修復選項如何,某些形式的數據損壞無法完全修復 。

這可能是您的文件損壞的情況之一。

此外請嘗試在維護呼叫上方直接調用以填充列表中的數據。

這可能有幫助。

+0

你能否修復你的鏈接,以便OP不必複製粘貼.. thx .. – MethodMan

+0

感謝DJ Kraze;剛剛做了 –

+0

。我移動維修電話後,我的數據庫沒有損壞enymore。 但我的列表尚未填充:( –