2010-12-07 89 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 2010-12-07 10:35:21

回答

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的使用。

適應您的PrimaryKeyChecker代碼,並返回表的字符串,你可以寫這樣的事情:

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

不過,我會建議在最起碼,返回一個列表,因此它可以很容易地搜索並過濾並用於在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方法。隨意更改我的答案中的方法名稱,無論您需要的是什麼,以及參數。重要的部分是字符串。與LINQ一起使用。

相關問題