【问题标题】:Need to convert the DataTable return type to string需要将 DataTable 返回类型转换为字符串
【发布时间】:2010-12-07 10:22:30
【问题描述】:

我有一个函数可以返回数据表中具有主键的表列表,但现在需要以字符串返回类型获取表列表。

我的方法如下:

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 方法中要涵盖的整个逻辑

请大家帮帮我!!!

【问题讨论】:

  • 就像我从服务器绑定表并将其存储在数据表中,然后将其填充到数据网格中
  • 请告诉我们您尝试了什么?

标签: c# sql-server-2005 datatable


【解决方案1】:

要将 DataTable 转换为字符串,您可以使用 DataTable 将自身写入 Xml,然后将 Xml 转换为字符串的能力。

【讨论】:

    【解决方案2】:
    return (from rowItem in dt.AsEnumerable()
                   select Convert.ToString(rowItem["TABLES"])).ToList();
    

    编辑:将您的表名作为 IList 集合返回。

    【讨论】:

      【解决方案3】:

      不确定我是否 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 上进行演示。

      如果我完全误解了您的问题,我深表歉意。

      【讨论】:

        【解决方案4】:
        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 一起使用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-01
          • 1970-01-01
          • 2015-09-04
          • 2015-06-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多