我正在尝试使用VB.NET代码使用SQLCMD(SQL命令提示符)执行SQL查询。我使用Windows Shell脚本连接到服务器并执行查询并将结果集存储在Excel文件中。下面是我的代码不工作。 以下代码中缺少的是什么?如何使用VB.NET运行SQLCMD脚本
Dim Command
Dim ServerName As String
Dim DatabaseName As String
Dim QueryToExceute As String
ServerName = "IN2175533W1"
DatabaseName = "C:\FileDirectory\XYZ.mdf"
QueryToExceute = "Select * from Table_Name"
Command = """C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE"" " & "-S " & ServerName & " -d " & DatabaseName & " -Q " & QueryToExceute & " -s" & "," & " -o" & "C:\TestQuery.xlsx"
Dim wsh As Object = CreateObject("WScript.Shell")
'Using WScript to execute Stuff
wsh = CreateObject("WScript.Shell")
wsh.Run(Command)
我尝试过程类以及哪个不工作。下面是我的代码:
Dim Command
Dim ServerName As String
Dim DatabaseName As String
Dim QueryToExceute As String
ServerName = "IN2175533W1"
DatabaseName = "C:\ABC\XYZ.mdf"
QueryToExceute = "Select * from Quality"
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 & "-Q" & QueryToExceute & "-s" & "," & "-o" & "C:\Testing1.xlsx"
Process.StartInfo.WorkingDirectory = "C:\users\rahul.wankhade\Desktop"
Process.Start()
Process.WaitForExit()
SQLCMD是一个实用并通过自身的应用程序。你应该使用'Process'类 – Rahul
@Rahul ....我尝试了进程类,但无法执行查询。 – rahul16590