2010-11-27 10 views
0

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?

Répondre

3

Au lieu d'un ListView, essayez d'utiliser un DataGridView, le remplacement de ces lignes

cmbResults.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"]; 
      cmbResults.DisplayMember = "TABLE_NAME"; 
      cmbResults.ValueMember = (""); 

avec ce

dataGridView1.DataSource = dsListOfNonPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"]; 

Vous pouvez définir les propriétés sur le DataGridView pour lui donner l'aspect plus comme un ListView, pour exemple:

dataGridView1.RowHeadersVisible = false; 
dataGridView1.AllowUserToAddRows = false; 
dataGridView1.AllowUserToDeleteRows = false; 

Modifier

Aussi, regarder votre requête, si votre objectif est d'obtenir les tables qui ne sont pas les clés primaires, essayez ceci:

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 

La vue INFORMATION_SCHEMA.TABLE_CONSTRAINTS comprend également des lignes pour FOREIGN KEY, , et UNIQUE contraintes, de sorte que votre requête telle qu'elle est maintenant va sélectionner les noms de table associés à l'une de ces contraintes.