Что-то, что я быстро сделал.Если вы знаете 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]");
}
}
}