使用nonexecutequery执行SQL Server脚本 [英] Execute the sql server script using nonexecutequery

查看:170
本文介绍了使用nonexecutequery执行SQL Server脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个SQL Server脚本文件.我想通过C#执行此脚本文件.

我使用了以下代码.

Hi,

I have one SQL server script file. I want to execute this script file through C#.

I used the following code.

SqlConnection conn = new SqlConnection();
                conn = new SqlConnection(SQLConnection);
                Server server = new Server(new ServerConnection("ConnectionString"));
                server.ConnectionContext.ExecuteNonQuery(Script);



为此,我从"C:\\ ProgramFiles \\ MicrosoftSqlServer \\ 100 \\ SDK \\ Asseblies"中加载了dll.


但是我遇到了类似混合模式.net版本``v2.0.02527''到.net版本``v4.0''的错误.


那么如何使用ExecuteNonQuery或其他方式执行脚本文件?

请帮帮我

谢谢



For this I loaded the dll from "C:\\ProgramFiles\\MicrosoftSqlServer\\100\\SDK\\Asseblies".


But I am getting error like mixed mode .net version ''v2.0.02527'' to .net version ''v4.0''.


So how to execute the script file using ExecuteNonQuery or some other way?

Please help me

Thank you

推荐答案

SqlConnection已内置到.net中,因此您所需的只是项目中的System.Data参考.

不需要引用特定于SQL的程序集.
SqlConnection are built into .net so all you need is the System.Data reference in your project.

You do not need to reference SQL specific assemblies.


这里很少.首先,如Timerbird所说,这不是添加引用的方法.只需将.NET中的System.Data添加到引用中即可.如果您使用的是.NET4,请确保不使用客户端配置文件,因为它可能没有.似乎没有任何用处.

我不相信您可以像这样单个命令来运行整个脚本.脚本通常是由关键字"go"分别调用的一系列命令.如果运行多个命令,以为您会遇到问题.要么使用osql进行外壳处理,要么随时进行脚本拆分,然后分别提交每个命令.

是的,如上所述,您不需要添加任何有关它的参考思想.它已经在那里...
Few things here. Firstly as Timerbird says, that''s not the way to add a reference. Just add System.Data from .NET in references. If you''re using .NET4 make sure your not using the Client Profile, as that probably won''t have it. Doesn''t seem to have anything of any use.

I don''t believe you can run a whole script as a single command like that. A script is usually a sequence of commands individually invoked by the keyword ''go''. Think you''ll have issues if running multiple commands. Either shell out to osql to do it, or split the script on gos and submit each command individually.

Yes, as pointed out above, you don''t need to add any reference thinking about it. It''s already there....


这篇关于使用nonexecutequery执行SQL Server脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆