Good evening,
i'm at the beginning of an IT apprenticeship and i've started my own little project. I've got following problem:
I've created in WPF a standard Login Window for getting SQL Connection Datas. This Window is working fine and i only can switch to the next window when the entered Login-Datas are correct. My problem is the second window. In the second one i have one ListBox for listing every Database from the connected Server. Next to this ListBox i've a ListView. In this ListView i want to see the tables from the selected Database.
Code for listing Databases:
SqlConnection GetConnection = new SqlConnection("Server=admin-pc;user Id=sa;Password=123;");
try
{
this.libDatabase.Items.Clear();
DataTable databases = new DataTable("Databases");
using (IDbConnection connection = GetConnection)
{
IDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM sys.Databases";
connection.Open();
databases.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
}
this.libDatabase.Items.Clear();
foreach (DataRow row in databases.Rows)
this.libDatabase.Items.Add(row[0].ToString());
}
catch (SqlException)
{
this.libDatabase.Items.Clear();
this.libDatabase.Items.Add("Connection error");
}
catch (Exception ex)
{
MessageBox.Show("Error while loading available databases");
}
finally
{
GetConnection.Close();
}
libDatabase is the name of the ListBox.
This Code is working good and every Database is shown in my ListBox. But no i've a totally Blackout. How do i get the Tables from the selected Database into the ListView? I've tried the same way like i did with the Databases but with a different "SELECT"-Statement ==> "select * from {0}", listbox.SelectedItem Here are different Codes i've tried but i think that i do something completely wrong.
First Version:
List<ListViewItem> gettables = new List<ListViewItem>();
if (libDatabase.SelectedItem != null)
{
SqlConnection con = new SqlConnection("Server=admin-pc;user Id=sa;Password=123;");
string strSQL = string.Format("select * from {0}", libDatabase.SelectedItem);
SqlCommand cmd = new SqlCommand(strSQL, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
}
reader.Close();
con.Close();
}
Second Version:
SqlConnection GetConnection = new SqlConnection("Server=admin-pc;user Id=sa;Password=123;");
try
{
this.libTables.Items.Clear();
DataTable tables = new DataTable("Tables");
using (IDbConnection connection = GetConnection)
{
IDbCommand command = connection.CreateCommand();
if (libDatabase.SelectedItem != null)
{
command.CommandText = string.Format("SELECT * FROM {0}", libDatabase.SelectedItem);
connection.Open();
tables.Load(command.ExecuteReader());
}
}
this.libTables.Items.Clear();
foreach (DataRow row in tables.Rows)
this.libTables.Items.Add(row[0].ToString());
}
catch (SqlException)
{
this.libTables.Items.Clear();
this.libTables.Items.Add("Connection error. Check server");
}
catch (Exception ex)
{
MessageBox.Show("Error while loading available tables" + ex);
}
finally
{
GetConnection.Close();
}
libTables is the name of the ListView.
Maybe i did it completely wrong or there's just one code block false.
Thank you for supporting.
Dave S.