2010-12-07 14 views
0

データテーブルに主キーを持つテーブルのリストを返す関数がありますが、今度はテーブルのリストを文字列戻り型で取得する必要があります。次のようにDataTableの戻り値の型を文字列に変換する必要があります

私の方法は次のとおりです。

public DataTable GetAllPrimaryKeyTables 
    (string localServer, string userName, string password, string selectedDatabase) 
{ 

    // Create the datatable 
    DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames"); 

    SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder(); 
    objConnectionString.DataSource = localServer; ; 
    objConnectionString.UserID = userName; 
    objConnectionString.Password = password; 
    objConnectionString.InitialCatalog = selectedDatabase; 

    // Query to select primary key tables. 
    string selectPrimaryKeyTables = @"SELECT 
              TABLE_NAME 
              AS 
              TABLES 
             FROM 
              INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
             WHERE 
              CONSTRAINT_TYPE = 'PRIMARY KEY' 
            ORDER BY 
              TABLE_NAME"; 

    // put your SqlConnection and SqlCommand into using blocks! 
    using(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString)) 
    using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection)) 
    { 
     try 
     { 
      // Create the dataadapter object 
      SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection); 

      // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself 
      // (and also close it again after it is done) 
      sDataAdapter.Fill(dtListOfPrimaryKeyTables); 

     } 
     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); 
     } 
    } 

    // return the data table to the caller 
    return dtListOfPrimaryKeyTables; 
} 

しかし、今、私はこのロジックは、以下の機能で呼ばれるようにしたい...私が試してみましたが、それが行われていません。

君たちが私を助けてくださいだろう

public class PrimaryKeyChecker : IMFDBAnalyserPlugin 
{ 
    public string RunAnalysis(string ConnectionString) 
    { 
     return "string"; 
    } 
} 

私はRunAnalysis方法でカバーされる文字列型と全体のロジックに関数の戻り値のを調整する必要があります!

+0

私はサーバーからテーブルをバインドしてデータテーブルに格納して、データグリッドにデータを格納しています – Srivastava

+0

何を試してくださいか教えてください。 – TalentTuner

答えて

0

DataTableを文字列に変換するには、DataTableの機能を使用してXmlに自身を書き込んだ後、Xmlを文字列に変換します。

0
return (from rowItem in dt.AsEnumerable() 
       select Convert.ToString(rowItem["TABLES"])).ToList(); 

編集:テーブル名をIListコレクションとして返します。

0

は、私は100%があなたの質問を必ず理解していないが、あなたがする必要があるすべてがあることが表示されます、次のいずれか

  1. あなたが持っているコードを実行し、文字列
  2. リファクタリングに変換コードをデータレアに追加し、DataSets/DataTablesの使用を省略して直接戻します。私はそれが簡単に検索できるように、リストを返す、非常に少なくとも、推薦しかし

    public string RunAnalysis(string localServer, string userName, string password, string selectedDatabase) 
    { 
        DataTable dt = GetAllPrimaryKeyTables(localServer, userName, password, selectedDatabase); 
        StringBuilder sb = new StringBuilder(); 
        foreach (DataRow dr in dt.Rows) 
        { 
         sb.AppendLine(dr.IsNull(0) ? "" : dr[0].ToString()); 
        } 
        return sb.ToString(); 
    } 
    

あなたPrimaryKeyCheckerコードを適応し、テーブルの列を返すために、あなたはこのような何かを書くことができますフィルタリングされ、UI上での提示に使用される。

質問が間違っているとお詫び申し上げます。

0
using System.Linq; 
... 

public string GetAllPrimaryKeyTableNames(string localServer, string userName, string password, string selectedDatabase) { 
    DataTable table = GetAllPrimaryKeyTables(localServer, userName, password, selectedDatabase); 

    return string.Join(",", table.AsEnumerable().Select(r => r.ItemArray[0]).ToArray()); 
} 

これは、カンマで区切られたテーブル名を含む文字列を返します。私はあなたが持っているあなたの質問RunAnalysisという名前のメソッドで参照

EDIT

。私の答えの中のメソッド名は、必要なものだけでなく、パラメータも自由に変更してください。重要な部分はLINQの文字列です。

関連する問題