2012-03-09 15 views
1

C-Sharp(C#)を使用してMySQLデータベースのテーブルの主キーを取得しようとしていますが、問題が発生しています。OdbcConntion(System.Data.Odbc)を使用してC#でMySqlテーブルPrimaryKeyを取得

提供されたさまざまなMetaDataコレクションと対応する列を調べましたが、いずれもプライマリキーを提供していませんでした。 "Tables"と "Indexes"コレクションは最も有望なようです。興味深いことに、OdbcConnection.GetSchema()にはPrimaryKeyプロパティ/メソッドがありますが、PrimaryKeyプロパティがnull以外の場合はありません。

インデックスとテーブルは本当に明らかな選択肢のように思えました。はい、データベースのテーブルには主キーがあり、データベースが機能します。

ここにいくつかのコードがありますが、この質問には本当に必要なものはありません。私はこのサンプルの目的で "テーブル"を選択しましたが、単に "インデックス"(または他のもの)に変更することができます。明らかに、表のためにCOLUMN_NAMEが存在します。私はちょうどそこに何でも、遊んでいる。

public String GetPrimaryKey(String strTable) 
{ 
try 
{ 
    String strPrimaryKey = null; 
    String[] strRestricted = new String[4] { null, null, strTable, null }; 
    DataTable oSchema = null; 


    // Make sure that there is a connection. 
    if (ConnectionState.Open != this.m_oConnection.State) 
     this.m_oConnection.Open(); 

    // DATABASE: Get the schema 
    oSchema = this.m_oConnection.GetSchema("Tables", strRestricted); 

    // Extract the information related to the primary column, in the format "{System.Data.DataColumn[0]}" 
    DataColumn[] oPrimaryKeys = oSchema.PrimaryKey; 

    // Extract: Column Names 
    foreach (DataRow oRow in oSchema.Rows) 
    { 
     // Get the column name. 
     String strColumnName = oRow["COLUMN_NAME"].ToString(); 
    } 

    return strPrimaryKey; 
} 

catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

return null; 
} 

私の研究では、GetSchema()。PrimaryKeyプロパティを使用している人からの投稿を見つけることができなかったことが面白かったです。

どのようにして主キーを特定できますか?

ありがとうございます。

+0

は間違いなく、各テーブルのプロシージャ・コールと共にはmysqlclientとのGetSchema()を使用します。 http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html http://stackoverflow.com/questions/2341278/ph​​p-get-primary-key -of-table – Lloyd

+0

あなたのコメントはマジックキーでした。私は古いインターフェイスが廃止されたことを知らなかった。インデックスのコレクションには "COLUMN_NAME"、Columnsコレクションには "PRIMRY"が残っていないので、適切なコードを見つけることは少し難しかったので、私は2回通過する必要がありましたが、新しいバージョンがはるかに優れています。 –

+0

あなたは他の人のための参考資料としてあなた自身の質問に答えることができます。 – Lloyd

答えて

1

あなたのコメントは魔法のキーでした。私は古いインターフェイスが廃止されたことを知らなかった。インデックスのコレクションには "COLUMN_NAME"、Columnsコレクションには "PRIMRY"が残っていないので、適切なコードを見つけることは少し難しかったので、私は2回通過する必要がありましたが、新しいバージョンがはるかに優れています。

public String GetPrimaryKey(String strTable) 

{ { ブールbIsPrimary = falseをしてみてください。 文字列strIndexName = null; 文字列strColumnName = null; String [] strRestricted = new String [4] {null、null、strTable、null}; DataTable oSchemaIndexes = null; DataTable oSchemaIndexColumns = null;

// Make sure that there is a connection. 
    if (ConnectionState.Open != this.m_oConnection.State) 
     this.m_oConnection.Open(); 

    // DATABASE: Get the schemas needed. 
    oSchemaIndexes = this.m_oConnection.GetSchema("Indexes", strRestricted); 
    oSchemaIndexColumns = this.m_oConnection.GetSchema("IndexColumns", strRestricted); 

    // Get the index name for the primary key. 
    foreach (DataRow oRow in oSchemaIndexes.Rows) 
    { 
     // If we have a primary key, then we found what we want. 
     strIndexName = oRow["INDEX_NAME"].ToString(); 
     bIsPrimary = (Boolean)oRow["PRIMARY"]; 
     if (true == bIsPrimary) 
      break; 
    } 

    // If no primary index, bail. 
    if (false == bIsPrimary) 
     return null; 

    // Get the corresponding column name. 
    foreach (DataRow oRow in oSchemaIndexColumns.Rows) 
    { 
     // Get the column name. 
     if (strIndexName == (String)oRow["INDEX_NAME"]) 
     { 
      strColumnName = (String)oRow["COLUMN_NAME"]; 
      break; 
     } 
    } 

    return strColumnName; 
} 

catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

return null; 

} ODBCは廃止されているので

関連する問題