I have a DataGridView
that loads table names. Get tables is called in the constructor. I have 9 tables stored in the .sdf database. When it loads I have all tables listed at least 81 times. How can I show one of each table name? I'm using SqlCe 3.5 and C# 2010 Express. Here is my code :
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);
}
}
}
}
}
You are mixing and combining two things. To retrieve data, you can use a DataAdapter
OR a DataReader
, not each of them. Here you only need the DataAdapter
to fill the table.
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);
}
}
}
}