0

这些命令行工具中的任何一个都可以导出到 .csv,例如:

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

ETC?

我不想使用视图或存储过程来破解双引号:)

4

3 回答 3

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]");
        }
    }
}
于 2012-03-13T12:52:58.770 回答
1

执行此操作的内置工具是 SSIS,尽管我很欣赏它可能是一个比您想要的“更重”的解决方案,并且它在 Express Edition 中不完全支持(您没有提到您正在使用的版本或版本)。您可以在包的平面文件连接管理器中定义文本限定符。

Alternatively, write a small script in your preferred scripting language.

于 2012-03-13T13:19:10.383 回答
0

Looks like this confirms my suspicions that the answer is:

No.

Thanks for the alternative suggestions.

于 2012-03-17T14:52:11.053 回答