如何用c#运行sql server脚本?

时间:2021-10-19 01:11:04

I'm write this code for run sql server script:

我正在编写运行sql server脚本的代码:

string sqlConnectionString = "Data Source=.;Initial Catalog=behzad;Integrated Security=True";
//string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo("d:\\behzadBULK.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);


but this line :

但这一行:

Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn));

i get this error:

我收到此错误:

 Are you missing reference?


what reference should be add to the project?

什么参考应该添加到项目?

2 个解决方案

#1


3  

Your problem here is that a reference to the assembly Microsoft.SqlServer.Server is missing and that's why that error is popping up.

这里的问题是缺少对程序集Microsoft.SqlServer.Server的引用,这就是弹出错误的原因。

You can resolve it by just adding a reference to your project for that particular assembly by right clicking your project and clicking on add reference. That should open a window to show you all the assemblies available and from there you can choose this assembly and add it to your project.

您可以通过右键单击项目并单击添加引用,仅为该特定程序集添加对项目的引用来解决此问题。这应该打开一个窗口,向您显示所有可用的程序集,然后您可以选择此程序集并将其添加到项目中。

After that, make sure you have added the namespace for it in your code and that should do it.

之后,请确保在代码中添加了名称空间,并且应该这样做。

Hope this helps.

希望这可以帮助。

#2


3  

Try Using SQLclient Connection Not SqlServer

尝试使用SQLclient连接而不是SqlServer

using System.Data;
using System.Data.SqlClient;

and Some code like this

和一些像这样的代码

settings = System.Configuration.ConfigurationManager.ConnectionStrings["MyTweetConnection"];
ConnectionString = settings.ConnectionString;
SQLCon = new SqlConnection(ConnectionString);

#1


3  

Your problem here is that a reference to the assembly Microsoft.SqlServer.Server is missing and that's why that error is popping up.

这里的问题是缺少对程序集Microsoft.SqlServer.Server的引用,这就是弹出错误的原因。

You can resolve it by just adding a reference to your project for that particular assembly by right clicking your project and clicking on add reference. That should open a window to show you all the assemblies available and from there you can choose this assembly and add it to your project.

您可以通过右键单击项目并单击添加引用,仅为该特定程序集添加对项目的引用来解决此问题。这应该打开一个窗口,向您显示所有可用的程序集,然后您可以选择此程序集并将其添加到项目中。

After that, make sure you have added the namespace for it in your code and that should do it.

之后,请确保在代码中添加了名称空间,并且应该这样做。

Hope this helps.

希望这可以帮助。

#2


3  

Try Using SQLclient Connection Not SqlServer

尝试使用SQLclient连接而不是SqlServer

using System.Data;
using System.Data.SqlClient;

and Some code like this

和一些像这样的代码

settings = System.Configuration.ConfigurationManager.ConnectionStrings["MyTweetConnection"];
ConnectionString = settings.ConnectionString;
SQLCon = new SqlConnection(ConnectionString);