Je travaille sur une application de bureau qui Renseigner la liste de toutes les tables contenant la clé primaire dans une zone de liste déroulante sur la sélection de la base de données correspondante dans une autre zone de liste déroulante comme celui-ciPeupler la valeur listview dans WinForm
///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();
}
}
}
Mais ce Je devrais faire si j'ai besoin de remplacer la liste déroulante avec une vue de liste peuplant le même élément lorsque selon la base de données sélectionnée d'une autre liste déroulante.
Pouvez-vous m'aider s'il vous plaît?