C# 如何获取每个列的类型和长度,然后使用 padright 的长度来获取每个字段末尾的空格

发布于 2024-09-04 03:01:12 字数 1084 浏览 4 评论 0原文

我有一个控制台应用程序,可以将数据从 SQL 表提取到平面文件。如何获取每个列的类型和长度,然后使用每个列的长度 padright(length) 来获取每个字段末尾的空格。这是我现在拥有的不包含此功能的内容。

谢谢

{
                var destination = args[0];
                var command = string.Format("Select * from {0}", Validator.Check(args[1]));
                var connectionstring = string.Format("Data Source={0}; Initial Catalog=dbname;Integrated Security=SSPI;", args[2]);

            var helper = new SqlHelper(command, CommandType.Text, connectionstring);

            using (StreamWriter writer = new StreamWriter(destination))
            using (IDataReader reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    Object[] values = new Object[reader.FieldCount];
                    int fieldCount = reader.GetValues(values);

                    for (int i = 0; i < fieldCount; i++)
                        writer.Write(values[i].ToString().PadRight(513));

                    writer.WriteLine();
                }

                writer.Close();
            }

I have a console application that extracts data from a SQL table to a flat file. How can I get each column type and length and then use the lenght of each column to padright(length) to get the spaces at the end of each field. Here is what I have right now that does not include this functionality.

Thanks

{
                var destination = args[0];
                var command = string.Format("Select * from {0}", Validator.Check(args[1]));
                var connectionstring = string.Format("Data Source={0}; Initial Catalog=dbname;Integrated Security=SSPI;", args[2]);

            var helper = new SqlHelper(command, CommandType.Text, connectionstring);

            using (StreamWriter writer = new StreamWriter(destination))
            using (IDataReader reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    Object[] values = new Object[reader.FieldCount];
                    int fieldCount = reader.GetValues(values);

                    for (int i = 0; i < fieldCount; i++)
                        writer.Write(values[i].ToString().PadRight(513));

                    writer.WriteLine();
                }

                writer.Close();
            }

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

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

发布评论

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

评论(2

濫情▎り 2024-09-11 03:01:12

IDataReader 提供 GetSchemaTable()方法,它提供 ColumnSize 属性(这因底层提供程序而异 - SQL Server 版本在这里)。

IDataReader offers a GetSchemaTable() method which provides a ColumnSize attribute (this varies by the underlying provider - the SQL Server version is here).

怪我闹别瞎闹 2024-09-11 03:01:12

您可以使用DataReader 的GetSchemaTable 方法来检索列架构信息。 本文提供了有关获取有关检索数据的元数据的完整详细信息。

获得列大小后,您可以使用 String.PadRight 方法向检索到的数据添加尾随字符。

You can use the DataReader's GetSchemaTable method to retrieve column schema information. This article has complete details on getting the metadata about the retrieved data.

Once you have the column size, you can add trailing characters to the retrieved data by using the String.PadRight method.

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