我尝试制作一个.exe文件,该文件可以从文本文件中读取全局参数,并将数据从数据库导出到csv文件
基本上我可以读取连接字符串并从文本文件中读取命令,但是我无法创建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);
{
}
}
}
}
有人可以帮我吗?
答案 0 :(得分: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
希望这会对你有所帮助。
答案 1 :(得分:0)
此链接可帮助您以CSV格式导出数据
http://www.aspsnippets.com/Articles/Export-GridView-To-WordExcelPDFCSV-in-ASP.Net.aspx
答案 2 :(得分:0)