【问题标题】:Converting DataTable to string type将 DataTable 转换为字符串类型
【发布时间】:2010-12-08 10:54:29
【问题描述】:

我有这样的功能

       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'
                                                     AND
                                                       TABLE_NAME <> 'dtProperties'
                                                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;



            }

然后我想将返回类型作为字符串而不是作为 DataTable,所以我这样做了..

  public string GetAllPrimaryKeyTables(string ConnectionString)
        {
            string result = string.Empty;

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

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

                    // 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);
                    using(StringWriter sw = new StringWriter())
                    {
                        dtListOfPrimaryKeyTables.WriteXml(sw);
                        result = sw.ToString();
                    }
                }
                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 result;
        }

但这并没有返回它应该返回的字符串......

你能告诉我需要修改的地方吗?

【问题讨论】:

  • 我更正了格式...更明显的是有两段代码(之前和之后)
  • 你希望数据集的哪一部分成为字符串?
  • 您期待什么样的字符串?例如:一个分隔的表名列表?
  • 是的,这就是我想要的东西

标签: c# string stringwriter system.data.datatable


【解决方案1】:

代替StringWriter,使用StringBuilder,然后您可以使用WriteWriteLine 方法。

【讨论】:

    【解决方案2】:

    如果您想要一个逗号分隔的列表,您可以使用如下所示的内容。

    private string GetList(ds)
    {
        var result = string.Empty;
        for(int i = 0; i < ds.Rows.Count; i++)
        {
             result += ds.Row[i][0] + ",";
        }
        result = result.Trim(',');
        return result;
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多