Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VB.NET运行SQLCMD脚本_Sql_.net_Vb.net - Fatal编程技术网

如何使用VB.NET运行SQLCMD脚本

如何使用VB.NET运行SQLCMD脚本,sql,.net,vb.net,Sql,.net,Vb.net,我正在尝试使用VB.NET代码使用SQLCMD(SQL命令提示符)执行SQL查询。我使用Windows Shell脚本连接到服务器,执行查询并将结果集存储在Excel文件中。下面是我不工作的代码。 下面的代码缺少什么 Dim Command Dim ServerName As String Dim DatabaseName As String Dim QueryToExceute As String ServerName = "IN2175533W1"

我正在尝试使用VB.NET代码使用SQLCMD(SQL命令提示符)执行SQL查询。我使用Windows Shell脚本连接到服务器,执行查询并将结果集存储在Excel文件中。下面是我不工作的代码。 下面的代码缺少什么

    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)
我也尝试了process类,但它不起作用。下面是我的代码:

    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()

这是我测试的内容,注意我更改了服务器、数据库和查询以匹配我的机器。我通过VS2015使用字符串插值

Module Module1
    Sub Main()
        Dim ServerName As String = "KARENS-PC"
        Dim DatabaseName As String = "C:\Data\NORTHWND.MDF"
        Dim DoubleQuote As String = Chr(34)
        Dim QueryToExceute As String =
            $"{DoubleQuote}SELECT CompanyName,ContactName FROM Customers{DoubleQuote}"
        Dim ExportFileName As String =
            $"{DoubleQuote}C:\Data\MyDataFromSqlServer.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 Sub
End 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:\Data\MyDataFromSqlServer.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 Sub
End Module

我做的另一种方法是在代码下面,读取字符串对象中的所有代码,用替换Go到任何其他符号来解析它,然后逐个执行数组

Sub ExecuteWithCommand()
    Try
        Dim sFile As FileInfo = New FileInfo(Directory.GetCurrentDirectory.ToString() & "\Script.sql")
        If (sFile.Exists = True) Then
            Dim dbObj = New SqlDataAccess()   
           'your SQL data-access class'
            Dim objReader As New System.IO.StreamReader(Directory.GetCurrentDirectory.ToString() & "\Script.sql")
            Dim scriptArr As String()
            Dim i As Integer
            dbObj.Command.CommandText = objReader.ReadToEnd.Replace("GO", "#")  
            'use any other symbol which you want, keeping in mind, its not used elsewhere in script'
            scriptArr = dbObj.Command.CommandText.Split("#")

            For i = 0 To scriptArr.Length - 1
                dbObj.Command.CommandText = scriptArr.GetValue(i)
                dbObj.Command.ExecuteNonQuery()
            Next

        End If
    Catch ex As Exception            
        Console.WriteLine(ex.Message)
    End Try

SQLCMD本身就是一个实用程序和应用程序。您应该使用
过程
class@Rahul....I尝试了process类,但无法执行查询…非常感谢..我正在使用Visual Studio 2012,其中字符串插值不起作用..您是否可以更正您的代码以在Visual Studio 2012中使用它这就是为什么指示版本是个好主意:-)我用第二个VS2012的版本,