Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#SQL表到文本文件输出格式_C#_Sql - Fatal编程技术网

C#SQL表到文本文件输出格式

C#SQL表到文本文件输出格式,c#,sql,C#,Sql,我遇到了一点问题。我的一个字段行{4}的字符长度可变。但是,它在文件中的结束位置必须是第{3}行末尾的23个字符。所以我需要添加多少空格,以使第{4}行的23个字符相等。正如你所看到的,我已经用for循环尝试过了。不幸的是,它不起作用。我不确定是我使用的方法还是语法问题 if ((checkResp91 != null) || (checkResp00 != null)) { string selectCommandText91

我遇到了一点问题。我的一个字段行{4}的字符长度可变。但是,它在文件中的结束位置必须是第{3}行末尾的23个字符。所以我需要添加多少空格,以使第{4}行的23个字符相等。正如你所看到的,我已经用for循环尝试过了。不幸的是,它不起作用。我不确定是我使用的方法还是语法问题

      if ((checkResp91 != null) || (checkResp00 != null))
            {
                string selectCommandText91 = "SELECT * FROM EFT_BANK_INFORMATION1";
                using (SqlDataAdapter adapter91 = new SqlDataAdapter(selectCommandText91, connection))
                {
                    using (DataTable table91 = new DataTable("EFT_BANK_INFORMATION1"))
                    {
                        adapter91.Fill(table91);
                        System.Text.StringBuilder commaDelimitedText = new System.Text.StringBuilder();
                        //commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
                        foreach (DataRow row in table91.Rows)
                        {
                            string spaceholder = "";
                            string spacer = row[4].ToString();
                            int countit = spacer.Length;
                            for (int i = countit; i < 23; i++)
                            {
                                spaceholder = spaceholder + " ";
                            }

                            string value = string.Format("{0}{1}                {0}         {2}{3}{4}{spaceholder}{5}", row[1], row[2], row[3], row[4], spaceholder, row[5], row[6]); // how you format is up to you (spaces, tabs, delimiter, etc)
                            commaDelimitedText.AppendLine(value);
                        }
                        System.IO.File.WriteAllText("C:\\BillingExport\\tbl9_1_export.txt", commaDelimitedText.ToString());
                    }
                }
            }
因为某种原因,这给了我额外的空间,当我需要的时候,把它们放在字符前面

上述代码的输出:

09                  09         011301390011111119              AAAAAAA AAAABBBBBBBB BBBBBBBBB CO.
需要的时候

09                  09         011301390011111119AAAAAAA AAAA           BBBBBBBB BBBBBBBBB CO.

试图重新发明Fortran记录文件,嗯

只需在
string.Format()中使用参数长度即可:


我也会用它来去掉那些空格,但这取决于你。

string value=string.Format(“{0}{1}{2}{3}{5}{6}”,行[1],行[2],行[3],行[4],空格持有者,行[5],行[6]);您将{spaceholder}作为占位符,而不是参数列表中的数字位置-{5}。谢谢。这是有效的,尽管它将空格放在文件中字符的前面,我需要它放在后面。@Dave,如果您想使用其他对齐方式,请使用
{4,-23}
09                  09         011301390011111119AAAAAAA AAAA           BBBBBBBB BBBBBBBBB CO.
string.Format("{0}{1}                {0}         {2}{3}{4,23}{5}", row[1], row[2], row[3], row[4], row[5], row[6]);