从数据库中以字符串格式文本检索表名

发布于 2024-10-06 23:56:27 字数 762 浏览 1 评论 0原文

我有一个这样的查询,

@"SELECT 
                                               TABLE_NAME
                                              AS
                                               TABLES
                                            FROM 
                                               INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                           WHERE 
                                               CONSTRAINT_TYPE = 'PRIMARY KEY'
                                             AND
                                               TABLE_NAME <> 'dtProperties'
                                        ORDER BY
                                               TABLE_NAME";

当从此查询中检索表列表时,有什么方法可以将表格形式转换为字符串格式形式。

I have a query like this

@"SELECT 
                                               TABLE_NAME
                                              AS
                                               TABLES
                                            FROM 
                                               INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                           WHERE 
                                               CONSTRAINT_TYPE = 'PRIMARY KEY'
                                             AND
                                               TABLE_NAME <> 'dtProperties'
                                        ORDER BY
                                               TABLE_NAME";

Is there any way to convert the tabular form to string formatted form when retrieving list of tables from this query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别低头,皇冠会掉 2024-10-13 23:56:27

通过 .NET,两者都不是 - 这只是 SSMS 显示。根据前面的问题,听起来您只是很难将本专栏读成字符串;

也许只是这样的东西:(

    List<string> list = new List<string>();
    using(var conn = new SqlConnection(connectionString))
    using (var cmd = conn.CreateCommand()) {
        cmd.CommandText = @"
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
            AND TABLE_NAME <> 'dtProperties'
            ORDER BY TABLE_NAME";
        conn.Open();

        using (var reader = cmd.ExecuteReader()) {
            while (reader.Read()) {
                list.Add(reader.GetString(0));
            }
        }
    }

在本地测试;工作正常)

如果您想要分隔的东西,那么也许:

string s = string.Join("|", list);

或者涉及 StringBuilder 的东西(本例中的换行符):

    StringBuilder sb = new StringBuilder();
    using(var conn = new SqlConnection(connectionString))
    using (var cmd = conn.CreateCommand()) {
        cmd.CommandText = @"
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
            AND TABLE_NAME <> 'dtProperties'
            ORDER BY TABLE_NAME";
        conn.Open();

        using (var reader = cmd.ExecuteReader()) {
            while (reader.Read()) {
                sb.AppendLine(reader.GetString(0));
            }
        }
    }
    string s = sb.ToString();

Via .NET it is neither - that is just the SSMS display. Based on the previous questions, it sounds like you are just having difficulties reading this column into strings;

Perhaps just something like:

    List<string> list = new List<string>();
    using(var conn = new SqlConnection(connectionString))
    using (var cmd = conn.CreateCommand()) {
        cmd.CommandText = @"
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
            AND TABLE_NAME <> 'dtProperties'
            ORDER BY TABLE_NAME";
        conn.Open();

        using (var reader = cmd.ExecuteReader()) {
            while (reader.Read()) {
                list.Add(reader.GetString(0));
            }
        }
    }

(tested locally; works fine)

If you want something delimited, then perhaps:

string s = string.Join("|", list);

Or something involving a StringBuilder (newlines in this example):

    StringBuilder sb = new StringBuilder();
    using(var conn = new SqlConnection(connectionString))
    using (var cmd = conn.CreateCommand()) {
        cmd.CommandText = @"
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
            WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
            AND TABLE_NAME <> 'dtProperties'
            ORDER BY TABLE_NAME";
        conn.Open();

        using (var reader = cmd.ExecuteReader()) {
            while (reader.Read()) {
                sb.AppendLine(reader.GetString(0));
            }
        }
    }
    string s = sb.ToString();
难忘№最初的完美 2024-10-13 23:56:27

这样的事情有帮助吗?

string[] myObjArray = new string[DataTable1.Rows.Count];

DataTable1.Rows.CopyTo(myObjArray, 0);

Something like this helps?

string[] myObjArray = new string[DataTable1.Rows.Count];

DataTable1.Rows.CopyTo(myObjArray, 0);
暖风昔人 2024-10-13 23:56:27

我不知道你所说的格式化表格是什么意思。
您可以使用以下 sql 语句检索包含所有表名称的字符串:

declare @out varchar(max)
set @out = ''
select @out = @out + table_name + char(10) + char(13) from information_schema.table_constraints
where constraint_type = 'PRIMARY KEY'
and table_name <> 'dtProperties'
order by table_name

select @out

I don't know what you mean with formatted form.
You can retrieve a string with all table names with following sql statement:

declare @out varchar(max)
set @out = ''
select @out = @out + table_name + char(10) + char(13) from information_schema.table_constraints
where constraint_type = 'PRIMARY KEY'
and table_name <> 'dtProperties'
order by table_name

select @out
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文