Populating the value in listview in winform
I am working on a desktop application that populate the list of all tables containing primary key in a combobox on selecting the corresponding database from another combo box like this
///This function binds the names of all the tables without primary keys in a dropdown cmbResults.
public void GetNonPrimaryKeyTables()
{
//An instance of the connection string is created to manage the contents of the connection string.
var sqlConnection = new SqlConnectionStringBuilder();
//Declare the datasource,UserId,Password
sqlConnection.DataSource = "192.168.10.3";
sqlConnection.UserID = "gp";
sqlConnection.Password = "gp";
//Add the initial catalog to the connection string
sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue);
//Assign the ConnectionString value to a new variable
string connectionString = sqlConnection.ConnectionString;
//Create the connection object
SqlConnection sConnection = new SqlConnection(connectionString);
//To Open the connection.
sConnection.Open();
//Query to select table_names that doesn't have PRIMARY_KEY.
string selectNonPrimaryKeys = @"SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE
CONSTRAINT_TYPE <> 'PRIMARY KEY'
ORDER BY
TABLE_NAME";
//Create the command object
SqlCommand sCommand = new SqlCommand(selectNonPrimaryKeys, sConnection);
try
{
//Create the dataset
DataSet dsListOfNonPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
//Create the dataadapter object
SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectNonPrimaryKeys, sConnection);
//Provides the master mapping between the sourcr table and system.data.datatable
sDataAdapter.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
//Fill the dataset
sDataAdapter.Fill(dsListOfNonPrimaryKeys);
//Bind the result combobox with non primary key table names
DataViewManager dvmListOfNonPrimaryKeys = dsListOfNonPrimaryKeys.DefaultViewManager;
cmbResults.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
cmbResults.DisplayMember = "TABLE_NAME";
cmbResults.ValueMember = ("");
}
catch(Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
finally
{
//If connection is not closed then close the connection
if(sConnection.State != ConnectionState.Closed)
{
sConnection.Close();
}
}
}
But what I should do if I need to replace the combobox with a list view populating the same item when according to the selected database from another dropdown开发者_运维技巧.
Can youguys please help me?
Instead of a ListView
, try using a DataGridView
, replacing these lines
cmbResults.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
cmbResults.DisplayMember = "TABLE_NAME";
cmbResults.ValueMember = ("");
with this
dataGridView1.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
You can set properties on the DataGridView
to have it look more like a ListView
, for example:
dataGridView1.RowHeadersVisible = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
Edit
Also, looking at your query, if your goal is to get the tables that don't have primary keys, try this:
select t.TABLE_NAME
from INFORMATION_SCHEMA.TABLES t
left join INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
on t.TABLE_SCHEMA = c.TABLE_SCHEMA
and t.TABLE_NAME = c.TABLE_NAME
and c.CONSTRAINT_TYPE = 'PRIMARY KEY'
where t.TABLE_TYPE = 'BASE TABLE'
and c.CONSTRAINT_TYPE is null
The INFORMATION_SCHEMA.TABLE_CONSTRAINTS
view also includes rows for FOREIGN KEY
, CHECK
, and UNIQUE
constraints, so your query as it stands now will select the table names associated with any of these constraints.
精彩评论