2010-11-27 61 views
0

我對填充包含從另一個組合框中選擇相應的數據庫,這樣在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(); 
       } 
      } 
     } 

但是在組合框中的主鍵的所有表的列表中選擇桌面應用程序的工作在列表視圖填充值我應該這樣做,如果我需要根據從另一個下拉列表中選擇的數據庫,將列表視圖中的組合框替換爲填充相同項目的組合框。

你能幫我嗎?

回答

3

取而代之的是ListView,請嘗試使用DataGridView,更換這些線路

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

與此

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

您可以設置在DataGridView性能有它看起來更像是一個ListView,爲例如:

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

編輯

此外,看着你的查詢,如果你的目標是讓那些沒有主鍵的表,試試這個:

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 

INFORMATION_SCHEMA.TABLE_CONSTRAINTS視圖還包括FOREIGN KEY行, CHECKUNIQUE約束,因此您現在的查詢將選擇與任何這些約束關聯的表名稱。