2010-11-26 66 views
1

我有這樣的功能的組合框結合從選定數據庫中的主鍵:爲什麼函數給出了不必要的價值

//An instance of the connection string is created to manage the contents of the connection string. 

var sqlConnection = new SqlConnectionStringBuilder(); 
sqlConnection.DataSource = "192.168.10.3"; 
sqlConnection.UserID = "gp"; 
sqlConnection.Password = "gp"; 
sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue); 
string connectionString = sqlConnection.ConnectionString; 

SqlConnection sConnection = new SqlConnection(connectionString); 

//To Open the connection. 
sConnection.Open(); 

//Query to select the table_names that have PRIMARY_KEYS. 
string selectPrimaryKeys = @"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(selectPrimaryKeys, sConnection); 

try 
{ 
    //Create the dataset 
    DataSet dsListOfPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); 

    //Create the dataadapter object 
    SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeys, 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(dsListOfPrimaryKeys); 

    //Bind the result combobox with primary key tables 
    DataViewManager dvmListOfPrimaryKeys = dsListOfPrimaryKeys.DefaultViewManager; 
    cmbResults.DataSource = dsListOfPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"]; 
    cmbResults.DisplayMember = "TABLE_NAME"; 
    cmbResults.ValueMember = "TABLE_NAME"; 
} 
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(); 
} 

但它給人一種事與願違的結果一樣dtproperties.Is什麼毛病查詢。

+3

什麼是預期輸出,什麼是當前的輸出?我們需要更多細節。您是否嘗試過使用類似SQL Management Studio的操作直接對數據庫運行查詢? – TheCloudlessSky 2010-11-26 13:16:13

+0

是在查詢中有問題....它在sql管理工作室中顯示相同的結果。 – Srivastava 2010-11-26 13:26:24

回答

1

dtproperties是使用SQL Server來存儲圖信息的表。在某些版本的SQL Server中,它被標記爲用戶表(而不是系統表),因此將通過查找用戶表的查詢返回。

也許只是像這樣的東西篩選出來:

string selectPrimaryKeys = @"SELECT 
             TABLE_NAME 
            FROM 
             INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
            WHERE 
             CONSTRAINT_TYPE = 'PRIMARY KEY' 
             AND TABLE_NAME <> 'dtproperties' 
           ORDER BY 
             TABLE_NAME"; 
0

儘管主鍵在技術上是表而不是數據庫的一部分,但假設您的查詢是正確的,請嘗試使用這種方法來處理您的C#代碼。它有一些內存和性能改進,以及它也會捕獲連接異常。

public void GetPrimaryKeyTable() { 
    //An instance of the connection string is created to manage the contents of the connection string. 

    var sqlConnection = new SqlConnectionStringBuilder(); 
    sqlConnection.DataSource = "192.168.10.3"; 
    sqlConnection.UserID = "gp"; 
    sqlConnection.Password = "gp"; 
    sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue); 

    // Automatically close the connection 
    using(SqlConnection sConnection = new SqlConnection(sqlConnection.ConnectionString)) { 
     try { 
      sConnection.Open(); 

      //Query to select the table_names that have PRIMARY_KEYS. 
      string selectPrimaryKeys = @"SELECT TABLE_NAME 
             FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
             WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
             ORDER BY TABLE_NAME"; 

      //Create the command object 
      using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection)) { 
       // Bind the combobox without destroying the data reader object after binding (no using statement) 
       cmbResults.DisplayMember = "TABLE_NAME"; 
       cmbResults.ValueMember = "TABLE_NAME"; 
       cmbResults.DataSource sReader = sCommand.ExecuteReader(); 
       cmbResults.DataBind(); 
      } 
     } 
     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 { 
      // Read somewhere that the using statement takes care of this for you 
      // but just in case 
      if(sConnection.State != ConnectionState.Closed) { 
       sConnection.Close(); 
      } 
     } 
    } 
} 

至於您的查詢,在SQL Server 2008 R2,它返回具有主鍵(列表中的每個表是一個主鍵的表),表的列表。你使用的是什麼版本的SQL Server?

編輯:如果您想查詢只返回用戶表,並篩選出類似的系統表的東西,嘗試此查詢:

SELECT tc.TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc 
INNER JOIN sysobjects so 
     ON tc.TABLE_NAME = so.name 
WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY' 
     AND so.xtype = 'U' 
ORDER BY TABLE_NAME; 
相關問題