2014-06-22 73 views
1

我有一個加載表名稱的DataGridView。獲取表在構造函數中被調用。我有9個表存儲在.sdf數據庫中。當它加載時,我將所有表格至少列出81次。我如何顯示每個表名之一?我正在使用SqlCe 3.5C#2010 Express。這裏是我的代碼:查看DataGridView中的所有SQL表格

DataTable dt = new DataTable(); 
private void getTables(object sender, EventArgs e) 
{ 
    dataGridView1.DataSource = dt; 
    string strConnect = @"Data Source=|DataDirectory|\LWADataBase.sdf"; 
    using (SqlCeConnection con = new SqlCeConnection(strConnect)) 
    { 
     con.Open(); 
     using (SqlCeCommand com = new SqlCeCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con)) 
     { 
      using (SqlCeDataReader reader = com.ExecuteReader()) 
      { 
       SqlCeDataAdapter da = new SqlCeDataAdapter(); 
       da.SelectCommand = com; 

       while (reader.Read()) 
       { 
        da.Fill(dt); 

       } 
      } 
     } 
    } 
} 

回答

-1

你正在混合和結合兩件事。要檢索數據,您可以使用DataAdapter a DataReader,而不是它們中的每一個。在這裏你只需要DataAdapter填表。

private void getTables(object sender, EventArgs e) 
{ 
    dataGridView1.DataSource = dt; 
    string strConnect = "Data Source=|DataDirectory|\\LWADataBase.sdf"; 
    using (SqlCeConnection con = new SqlCeConnection(strConnect)) { 
     con.Open(); 
     using (SqlCeCommand com = new SqlCeCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con)) { 
      using (SqlCeDataAdapter da = new SqlCeDataAdapter()) { 
       da.SelectCommand = com; 
       da.Fill(dt); 
      } 
     } 
    } 
}