2014-09-24 78 views
-17

如何在C#中使用sql(SQLServer)查詢的輸出創建csv文件?如何使用sql查詢的輸出創建csv文件?

這是到目前爲止,我已經試過代碼:

public static void CreateManifestFile(string SQLFileToExecute, string ConnectionString, StreamWriter logfile) 
{ 
    int success = 0; 
    List<string> results = new List<string>(); 
    string script = File.ReadAllText(@SQLFileToExecute); 
    Logging.WriteToLog(" ", logfile); 
    Logging.WriteToLog(" ", logfile); 
    Logging.WriteToLog("=== Processing file: [" + SQLFileToExecute + "] ===", logfile); 

    // split script on GO command 
    IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase); 

    SqlConnection Connection = new SqlConnection(ConnectionString); 
    Connection.Open(); 

    Connection.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e) 
    { 
     Logging.WriteToLog("Msg: " + e.Message, logfile); 
     success = 1; 
    }; 
    SqlCommand sqlcmd = new SqlCommand(); 
    sqlcmd.Connection = Connection; 
    foreach (string commandString in commandStrings) 
    { 
     if (commandString.Trim() != "") 
     { 
      success = 0; 
      Console.WriteLine(commandString.ToString()); 
      logfile.WriteLine(commandString.ToString()); 
      results.Add(sqlcmd.ExecuteReader(commandString)); 
      if (success == 0) 
      { 
       Logging.WriteToLog("Command executed successfully.", logfile); 
      } 
     } 
    } 
    Connection.Close(); 
    int length = results.Count; 
    string delimter=","; 
    using(System.IO.TextWriter writer = File.CreateText("manifest.csv")) 
    { 
     Logging.WriteToLog("manifest count:" + length, logfile); 
     for(int index = 0; index < length; index++) 
     { 
      writer.WriteLine(string.Join(delimter,results[index])); 
     } 
    } 

} 

但我上線得到錯誤:

results.Add(sqlcmd.ExecuteReader(commandString)); 

錯誤:

錯誤1最佳重載方法匹配 'System.Collections.Generic.List.Add(string)'有一些無效的 參數

錯誤2參數1:不能從 'System.Data.SqlClient.SqlDataReader' 到 '字串'

錯誤3爲 「System.Data.SqlClient.SqlCommand.ExecuteReader最好重載的方法匹配(轉換的System.Data.CommandBehavior)」 有一些無效參數

錯誤4參數1:不能從轉換 '字符串' 到 '的System.Data.CommandBehavior'

我FOL低於this post來做到這一點。

+5

你嘗試過什麼嗎? – 2014-09-24 07:22:38

+2

對不起,但'-1'爲零努力。 – 2014-09-24 07:23:11

+1

如果您有任何問題,請發佈您的代碼或您迄今爲止所嘗試的內容。 – Taosique 2014-09-24 07:23:32

回答

0

你還沒有嘗試過任何東西,所以這裏有一點動機讓你開始。

SqlServerStorage storage = new SqlServerStorage(typeof(Order)); 
string sqlConnectionString ="Server=SqlServer;Database=SqlDataBase;Trusted_onnection=True"; 
storage.ConnectionString = sqlConnectionString; 
storage.SelectSql = "select * from Yourtable"; 
storage.FillRecordCallback = new FillRecordHandler(FillRecordOrder); 
FileDataLink link = new FileDataLink(storage); 
link.FileHelperEngine.HeaderText = headerLine; 
link.ExtractToFile("file.csv"); 

這是給你包含大量數據的表的超時時間,但嘿,我不會比你懶惰。所以弄清楚。希望它有助於好運。

1
public string ExportToCSVFile(DataTable dtTable) 
{ 
    StringBuilder sbldr = new StringBuilder(); 
    if (dtTable.Columns.Count != 0) 
    { 
     foreach (DataColumn col in dtTable.Columns) 
     { 
      sbldr.Append(col.ColumnName.Replace(",", "") + ','); 
     } 
     sbldr.Append("\r\n"); 
     foreach (DataRow row in dtTable.Rows) 
     { 
      foreach (DataColumn column in dtTable.Columns) 
      { 
       sbldr.Append(row[column].ToString().Replace(",", "").Trim() + ','); 
      } 
      sbldr.Append("\r\n"); 
     } 
    } 
    return sbldr.ToString(); 
}