2012-03-13 30 views
0

可在任何該等命令行工具導出爲.csv,如:帶有封裝文本字段的BCP/sqlcmd/osql?

"int_field", "varchar_field", "another_int_field" 
10, "some text", 10 
5, "more text", 1 

等?

我不想使用一個視圖或存儲過程砍在:)

+0

你是否還希望圍繞一些int字段?或者是「int_field」只是你想引用的一些文本? – Jaques 2012-03-13 11:28:57

+0

對不起 - 沒有得到格式正確。第一行應該是列標題(我想這將被全部引用,因爲它們都是字符串) – 2012-03-13 11:34:45

+0

你能用C#編寫代碼嗎?據我所知,目前還沒有標準的命令行工具,但寫一個 – Jaques 2012-03-13 11:55:25

回答

0

看起來這證實了我的懷疑,答案是:

感謝其他建議。

1

財產以後,雙引號,我已經迅速完成。如果你知道c#你可以添加它,否則它可能是無用的。不是我最好的代碼,但它正在完成這項工作。所有的字段類型都不在這裏添加,因此需要完成。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.SqlClient; 
using System.IO; 

namespace SQLCSVExport 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      bool trustedConn = false; 
      string Servername = ""; 
      string Username = ""; 
      string Password = ""; 
      bool quotestring = false; 
      string fieldterminater = ","; 
      string tablename = ""; 
      string operation = ""; 
      string datafile = ""; 
      bool includeheadings = false; 

      if (args.Length < 3) 
      { 
       ShowOptions(); 
       return; 
      } 
      else 
      { 
       tablename = args[0]; 
       operation = args[1]; 
       datafile = args[2]; 
       for (int i = 3; i < args.Length; i++) 
       { 
        switch (args[i].Substring(0, 2)) 
        { 
         case "-Q": 
          quotestring = true; 
          break; 
         case "-T": 
          trustedConn = true; 
          break; 
         case "-S": 
          Servername = args[i].Substring(2); 
          break; 
         case "-U": 
          Username = args[i].Substring(2); 
          break; 
         case "-P": 
          Password = args[i].Substring(2); 
          break; 
         case "-t": 
          fieldterminater = args[i].Substring(2); 
          break; 
         case "-H": 
          includeheadings = true; 
          break; 
        } 
       } 
      } 
      SqlConnection conn; 

      if(File.Exists(datafile)) 
      { 
       try 
       { 
        File.Delete(datafile); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
        ShowOptions(); 
        return; 
       } 
      } 
      if (trustedConn) 
       conn = new SqlConnection("Integrated Security=True;Initial Catalog=master;Data Source=" + Servername); 
      else 
       conn = new SqlConnection("Password=" + Password + ";Persist Security Info=True;User ID=" + Username + ";Initial Catalog=master;Data Source=" + Servername); 
      try 
      { 
       conn.Open(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       ShowOptions(); 
       return; 
      } 
      SqlCommand cmd = new SqlCommand(); 
      SqlDataReader read = null; 
      cmd.Connection = conn; 
      if (operation == "out") 
       cmd.CommandText = "Select * from " + tablename; 
      else 
       cmd.CommandText = tablename; 
      try 
      { 
       read = cmd.ExecuteReader(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       ShowOptions(); 
       return; 
      } 
      string Dummy = ""; 
      if (read.HasRows) 
      { 
       if(includeheadings) 
       { 
        for (int i = 0; i < read.FieldCount; i++) 
        { 
         if (quotestring) 
          Dummy += "\"" + read.GetName(i) + "\"" + fieldterminater; 
         else 
          Dummy += read.GetName(i) + fieldterminater; 
        } 
        WriteStrToFile(datafile, Dummy, fieldterminater); 
       } 
       while (read.Read()) 
       { 
        Dummy = ""; 
        for (int i = 0; i < read.FieldCount; i++) 
        { 
         switch (read[i].GetType().ToString()) 
         { 
          case "System.Int32": 
           Dummy += read[i].ToString() + fieldterminater; 
           break; 
          case "System.String": 
           if (quotestring) 
            Dummy += "\"" + read[i].ToString() + "\"" + fieldterminater; 
           else 
            Dummy += read[i].ToString() + fieldterminater; 
           break; 
          case "System.DBNull": 
           Dummy += fieldterminater; 
           break; 
          default: 
           break; 
         } 
        } 
        WriteStrToFile(datafile, Dummy, fieldterminater); 
       } 
      } 
     } 

     static void WriteStrToFile(string datafile, string dummy, string fieldterminator) 
     { 
      FileStream fs = new FileStream(datafile, FileMode.Append, FileAccess.Write); 
      StreamWriter sr = new StreamWriter(fs); 
      if (dummy.Trim().Substring(dummy.Trim().Length - 1) == fieldterminator) 
       dummy = dummy.Substring(0, dummy.Trim().Length - 1); 
      sr.WriteLine(dummy); 
      sr.Close(); 
      fs.Close(); 
      sr.Dispose(); 
      fs.Dispose(); 
     } 

     static void ShowOptions() 
     { 
      Console.WriteLine("usage: SQLCSVExport {dbtable | query} {out | queryout} datafile"); 
      Console.WriteLine("[-q quote string fields]   [-S Server Name]  [-U User Name]"); 
      Console.WriteLine("[-P Password]     [-T Trusted Connection] [-t field terminator]"); 
      Console.WriteLine("[-H Add Headings]"); 
     } 
    } 
} 
1

內置的工具,做這個SSIS,雖然我明白,不是你想它可能是一個「重」的解決方案,它不是完全Express Edition的支持(你沒有提到任何版本或您正在使用的版本)。您可以在包中的flat file connection manager中定義文本限定符。

或者,用您喜歡的腳本語言編寫一個小腳本。

+0

不熟悉SSIS,你可以執行一個查詢來導出到一個文本文件,而無需首先創建一個包? – Jaques 2012-03-13 13:24:17

+0

不,SSIS是基於軟件包的,因此您無法避免創建一個。就個人而言,如果我不能使用SSIS,那麼我會使用一種已經具有CSV庫支持的語言(比如Perl或Python),因爲它比第一次編寫CSV文件更困難。例如,您的代碼似乎無法處理字符串包含引號的情況。 – Pondlife 2012-03-13 13:32:30