0

テキストファイルからグローバルパラメーターを読み取り、データベースからcsvファイルにデータをエクスポートできる.exeファイルを作成しようとしています

基本的に、接続文字列を読み取ってテキスト ファイルからコマンドを読み取ることはできますが、csv ファイルを作成することはできません。

私のコードは以下のようなものです

static class CsvGenerator
{
public static string gb_Localconn = DBManager.GetParameter("ConnStr=");
public static string gb_Delimiter = DBManager.GetParameter("Delimiter=");
public static string gb_Quote = DBManager.GetParameter("Quote=");
public static string gb_QuoteType = DBManager.GetParameter("Quote_Type=");
public static string gb_CsvOutput = DBManager.GetParameter("Output_path=");
public static string gb_Prefix = DBManager.GetParameter("Output_Prefix=");
public static string gb_LogOutput = DBManager.GetParameter("Logfile_path=");
public static string gb_VesselList = DBManager.GetParameter("Vessel_list=");
private static DataTable dt;
private static int i;


public static void Main()
{
    StreamReader oRead = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Query.cfg");

    string txtline = null;
    string QueryName = null;
    string query = null;
    while (oRead.Peek() != -1)

        try
        {
            txtline = oRead.ReadLine();
            if (txtline.IndexOf(":") != -1)
            {
                QueryName = txtline.Substring(0, txtline.IndexOf(":"));
                query = txtline.Substring(txtline.IndexOf(":") + 1, txtline.Length - QueryName.Length - 1);
                query = query.Replace("|Vessels|", gb_VesselList);
                MessageBox.Show(query);

                {

                    {
                        dt = DBManager.DBExecuteSelect(query, gb_Localconn);

                        DataColumn dcol = null;
                        if (dt.Rows.Count > 0)
                            MessageBox.Show(dcol.ToString());
                    }
                    DataRow drow = null;
                    for (i = 0; i <= dt.Rows.Count - 1; i++)
                        MessageBox.Show(drow.ToString());


                }
            }
        }
        catch (InvalidCastException e)
        {
            Console.WriteLine("{0} Exception caught.", e);
            {
            }




        }
}
} 

誰かがこれで私を助けることができますか?

4

2 に答える 2

0

CSV Helperは、CSV ファイルの作成に使用できる優れたツールです。

ファイルを作成するための Model クラスを用意することをお勧めします。

于 2013-04-11T05:57:25.220 に答える
0

私のために働いている次のコードを参照してください:

var lines = new string[5][];

var csvFile = string.Join("\r\n", lines.Select(words =>
              string.Join(",", words.Select(word =>
                  "\"" + word.Replace("\"", "\"\"") + "\"")))));

File.WriteAllText("yourFileName.csv", csvFile);

c# および csv のインポートとエクスポートに関するその他の詳細については、次のリンクを参照してください。

http://www.codeproject.com/Articles/30705/C-CSV-Import-Export

これがお役に立てば幸いです。

于 2013-04-11T05:04:43.067 に答える