GO statements blowing up sql execution in .NET

前端 未结 11 1046
南笙
南笙 2020-12-05 09:48

I have a very simple C# command shell app that executes a sql script generated by SQL Server for scripting schema and data. It\'s blowing up on the \"GO\" statements. Error

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 10:07

    1 additional point to "iamkrillin"'s answer, for using the old DLLs to make it work.

    after adding the references to these DLLs

    Microsoft.SqlServer.ConnectionInfo.dll , Microsoft.SqlServer.Management.Sdk.Sfc.dll Microsoft.SqlServer.Smo.dll , Microsoft.SqlServer.SqlEnum.dll

    from a place like this: "C:\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll" to the project, I needed to add the following "using" directives to the top of my code file:

    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    
    ....
    string query = @" //sql multi-command text here"
    
    using (SqlConnection thisconn = new SqlConnection(connectionString)) {
        Server db = new Server(new ServerConnection(thisconn));
        db.ConnectionContext.ExecuteNonQuery(query);
    }
    

提交回复
热议问题