可在任何該等命令行工具導出爲.csv,如:帶有封裝文本字段的BCP/sqlcmd/osql?
"int_field", "varchar_field", "another_int_field"
10, "some text", 10
5, "more text", 1
等?
我不想使用一個視圖或存儲過程砍在:)
可在任何該等命令行工具導出爲.csv,如:帶有封裝文本字段的BCP/sqlcmd/osql?
"int_field", "varchar_field", "another_int_field"
10, "some text", 10
5, "more text", 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]");
}
}
}
內置的工具,做這個SSIS,雖然我明白,不是你想它可能是一個「重」的解決方案,它不是完全Express Edition的支持(你沒有提到任何版本或您正在使用的版本)。您可以在包中的flat file connection manager中定義文本限定符。
或者,用您喜歡的腳本語言編寫一個小腳本。
你是否還希望圍繞一些int字段?或者是「int_field」只是你想引用的一些文本? – Jaques 2012-03-13 11:28:57
對不起 - 沒有得到格式正確。第一行應該是列標題(我想這將被全部引用,因爲它們都是字符串) – 2012-03-13 11:34:45
你能用C#編寫代碼嗎?據我所知,目前還沒有標準的命令行工具,但寫一個 – Jaques 2012-03-13 11:55:25