这是我测试的内容,请注意,我更改了服务器,数据库和查询以匹配我的机器。我正在通过VS2015使用字符串插值。
Module Module1 Sub Main() Dim ServerName As String = "KARENS-PC" Dim DatabaseName As String = "C:DataNORTHWND.MDF" Dim DoubleQuote As String = Chr(34) Dim QueryToExceute As String = $"{DoubleQuote}SELECt CompanyName,ContactName FROM Customers{DoubleQuote}" Dim ExportFileName As String = $"{DoubleQuote}C:DataMyDataFromSqlServer.csv{DoubleQuote}" Dim Process = New Process() Process.StartInfo.UseShellExecute = False Process.StartInfo.RedirectStandardOutput = True Process.StartInfo.RedirectStandardError = True Process.StartInfo.CreateNoWindow = True Process.StartInfo.FileName = "SQLCMD.EXE" Process.StartInfo.Arguments = $"-S {ServerName} -d {DatabaseName} -E -Q {QueryToExceute} -o {ExportFileName} -h-1 -s"","" -w 700" Process.StartInfo.WorkingDirectory = "C:Data" Process.Start() Process.WaitForExit() Console.WriteLine("Done") Console.ReadLine() End SubEnd Module不带VS2015的常规方式
Module Module1 Sub Main() Dim ServerName As String = "KARENS-PC" Dim DatabaseName As String = "NorthWindAzure" Dim DoubleQuote As String = Chr(34) Dim QueryToExceute As String = DoubleQuote & "SELECt CompanyName,ContactName FROM Customers" & DoubleQuote Dim ExportFileName As String = DoubleQuote & "C:DataMyDataFromSqlServer.csv" & DoubleQuote Dim Process = New Process() Process.StartInfo.UseShellExecute = False Process.StartInfo.RedirectStandardOutput = True Process.StartInfo.RedirectStandardError = True Process.StartInfo.CreateNoWindow = True Process.StartInfo.FileName = "SQLCMD.EXE" Process.StartInfo.Arguments = "-S " & ServerName & " -d " & DatabaseName & " -E -Q " & QueryToExceute & " -o " & ExportFileName & " -h-1 -s"","" -w 700" Process.StartInfo.WorkingDirectory = "C:Data" Process.Start() Process.WaitForExit() Console.WriteLine("Done") Console.ReadLine() End SubEnd Module


