将数据导出到具有固定长度记录的文本文件

时间:2017-10-27 03:35:06

标签: c# mysql winforms

我正在使用MYSQL,WinForms和c#

当我接下来从我的数据库中获取数据并导出到文本文件时,我有一个要求但是每个条目都需要是特定长度,而不管数据库带来的信息是否小于所需的总数量,空空间是跟随它。

E.g First Name可以带来Sam或Samatha,但我需要导出长度为8个字符 " Sam" (总共8个字符) " Samatha"(总共8个字符)

这是我正在使用的功能 - 非常感谢任何帮助

public MySqlDataReader RunQueryTextFile(string query, string s1,string pathName)
{
    MySqlConnection conDataBase = new MySqlConnection(connString);
    MySqlCommand cmdDatabase = new MySqlCommand(query, conDataBase);
    MySqlDataReader myReader;
    StreamWriter sw = new StreamWriter(@pathName);
    conDataBase.Open();

    myReader = cmdDatabase.ExecuteReader();
    while (myReader.Read())
    { 
        sw.WriteLine (myReader[s1].ToString());
    }

    sw.Close();
    conDataBase.Close();
    return myReader;
}

2 个答案:

答案 0 :(得分:0)

使用PadRight

int totalLength = 8;
myString.PadRight(totalLength, " ").

答案 1 :(得分:0)

您尝试编写过程代码。想想对象。我会做这样的事情

// create enum - how to pad
public enum PadStyle    
{
    Right
    Left
}
// create attribute class
public class PaddingAttribute : Attribute
{
     public int TotalLen {get;set;}
     public PadStyle Style {get;set;}
     public char PadChar {get;set;}
     public int Order {get;set;}
}


// Create class record base - this one knows how to get properties, read attributes and pad values into one string
public class RecordBase
{
     protected string CreateRecordProtected()
     {
         // here you 
         // 1 - use reflection to get properties
         // 2 - use reflection to read PaddingAttribute from properties
         // 3 - pad property values using data in PaddingAttribute
         // 4 - concatenate record 
         // something like
         var result = o.GetPropeties().Where(...).Select(...padding logic...);
         return string.Join("", result);
         // padding logic = string.PadRight or string.PadLeft
     }
}

Read here how to deal with attribute classes

// Create class record - this one corresponds to your DB
public class Record : RecordBase
{
     private const string _fn_FiedName = "firstName";
     private const string _ln_FiedName = "lastName";

     public Record(IDataReader r)
     {
         FirstName = r[_fn_FiedName];
         LastName = r[_ln_FiedName];
     }

     [Padding(TotalLen = 15, Style = PadStyle.Right, PadChar = " ",  Order = 1)]
     public string FirstName{get;set;}
     [Padding(TotalLen = 20, Style = PadStyle.Right, PadChar = " ",  Order = 2)]
     public string LastName {get;set;}

     public override string ToString()
     {
         return base.CreateRecordProtected();
     }

现在,你的代码有一些mods

var recList = new List<Record>();
using (var conn = new MySqlConnection(connString))
{
    using (var cmd = new MySqlCommand(query, conn))
    {
        conn.Open();
        using (var reader = cmdDatabase.ExecuteReader())
        {
            while (reader.Read())
            { 
                recList.Add(new Record(reader));
            }
        }
    }
    conn.Close();
} 

if (!recList.Any()) // System.Linq
    return;
IEnumerable<string> textFileRecords = recList.Select(x => x.ToString());
File.WriteAllLines(pathName, textFileRecords); // System.IO

现在,您有一些不错的可重用代码。 注意:如果数据量很大,您可以在

时追加
var rec = new Record(reader);
rec.ToString() // append this