What is database SQL query to display the name of the table only in to datagrid?
-
Hello Everyone I have created a WPF project (C# language) and I also have a MS Access 2007 database. I'm trying to display the database table name in to datagrid, but i can't figur out the SQL query how to do it. The code below that I wrote it's not correct I think maybe someone can help me please and solve it...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;using (OleDbConnection myConnection = new OleDbConnection(ConString)) { CmdString = "SELECT FoodMenu FROM MSysObjects WHERE (FoodMenu Not Like 'MSys\*') AND (Type In (1,4,6)) ORDER BY FoodMenu"; OleDbCommand comm = new OleDbCommand(CmdString, myConnection); OleDbDataAdapter sda = new OleDbDataAdapter(comm); myConnection.Open(); DataTable dt = myConnection.GetSchema("Tables"); //Get list of user tables foreach (DataRow dataRow in dt.Rows) { DataGridMenuTables.Items.Add(dataRow\["FoodMenu"\].ToString().Trim()); //DataGridMenuTables.ItemsSource = dt.DefaultView; } //myConnection.Close(); } }
Kind regards Roni
-
Hello Everyone I have created a WPF project (C# language) and I also have a MS Access 2007 database. I'm trying to display the database table name in to datagrid, but i can't figur out the SQL query how to do it. The code below that I wrote it's not correct I think maybe someone can help me please and solve it...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;using (OleDbConnection myConnection = new OleDbConnection(ConString)) { CmdString = "SELECT FoodMenu FROM MSysObjects WHERE (FoodMenu Not Like 'MSys\*') AND (Type In (1,4,6)) ORDER BY FoodMenu"; OleDbCommand comm = new OleDbCommand(CmdString, myConnection); OleDbDataAdapter sda = new OleDbDataAdapter(comm); myConnection.Open(); DataTable dt = myConnection.GetSchema("Tables"); //Get list of user tables foreach (DataRow dataRow in dt.Rows) { DataGridMenuTables.Items.Add(dataRow\["FoodMenu"\].ToString().Trim()); //DataGridMenuTables.ItemsSource = dt.DefaultView; } //myConnection.Close(); } }
Kind regards Roni
LAPEC wrote:
DataTable dt = myConnection.GetSchema("Tables"); //Get list of user tables
That should work.
LAPEC wrote:
//DataGridMenuTables.ItemsSource = dt.DefaultView;
Do you mean DataSource?
LAPEC wrote:
dataRow["FoodMenu"].ToString()
It's already a string, don't use ToString, just cast it
((string) dataRow["FoodMenu"]).Trim()
-
Hello Everyone I have created a WPF project (C# language) and I also have a MS Access 2007 database. I'm trying to display the database table name in to datagrid, but i can't figur out the SQL query how to do it. The code below that I wrote it's not correct I think maybe someone can help me please and solve it...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;using (OleDbConnection myConnection = new OleDbConnection(ConString)) { CmdString = "SELECT FoodMenu FROM MSysObjects WHERE (FoodMenu Not Like 'MSys\*') AND (Type In (1,4,6)) ORDER BY FoodMenu"; OleDbCommand comm = new OleDbCommand(CmdString, myConnection); OleDbDataAdapter sda = new OleDbDataAdapter(comm); myConnection.Open(); DataTable dt = myConnection.GetSchema("Tables"); //Get list of user tables foreach (DataRow dataRow in dt.Rows) { DataGridMenuTables.Items.Add(dataRow\["FoodMenu"\].ToString().Trim()); //DataGridMenuTables.ItemsSource = dt.DefaultView; } //myConnection.Close(); } }
Kind regards Roni
The simplest way is to create a Stored Procedure and put below mentioned SQL Query to fetch the list of tables in database.. then Bind those records to a DataGrid in C#. SQL Query is: SELECT * FROM information_schema.tables
- Happy Coding - Vishal Vashishta
-
The simplest way is to create a Stored Procedure and put below mentioned SQL Query to fetch the list of tables in database.. then Bind those records to a DataGrid in C#. SQL Query is: SELECT * FROM information_schema.tables
- Happy Coding - Vishal Vashishta