2017-10-27 6 views
0

データベースがもたらす情報にかかわらず、特定の長さを必要とする場合、必要な総量よりもデータが少ない場合、空のスペースがそれに従う必要があります。のデータのエクスポート

例:まず名前はサムまたはサマタ瞑想に持って来ることができるが、私は長さが8つの文字
「サム」(合計8文字) 「サマタ瞑想」(合計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; 
} 
+0

ながら右を追加することができます。あなたの試みの具体的なスニペットはありますか? – jdphenix

+0

'string.PadRight' ... https://msdn.microsoft.com/en-us/library/36f2hz3a(v = vs.110).aspx問題点は何ですか? –

答えて

0

使用PadRight

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

*各エントリは、データベースがもたらす情報に関係なく、特定の長さでなければなりません。* - あなたのコードはこれを満たしていません。 – jdphenix

+0

@jdphenix私はそれが要件を満たしていると思います。 – imlokesh

+0

@imlokesh文字列が特定の長さ(8など)でなければならない場合、 '' Christopher ".PadRight(8)'はそれ自身を返します。これは11文字です。 – jdphenix

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(); 
    } 

そして今、いくつかの改造

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 
+0

非常に詳細な答えは、本当にあなたの時間を感謝:) –

+0

明日あなたのレコード形式が変更されたらどうしますか?このコードはその変更を容易にします –

関連する問題