使用C#执行.sql脚本 [英] Execute .sql scripts with C#

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

问题描述

您好,我正在尝试执行.sql脚本.我已经引用了所有必需的程序集

Hi, am trying to execute a .sql script. i have referenced all the required assemblies

using Microsoft.SqlServer;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;



这是我的代码的一部分:
u



This is part of my code:
u

sing (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
            {
                ServerConnection svrConnection = new ServerConnection(sqlConnection);
                Server server = new Server(svrConnection);
                server.ConnectionContext.ExecuteNonQuery(script);
            }


我正在使用SQL Server 2005 Express Edition和Visual Studio 2010和.net 4.0

当我尝试执行该代码时,该代码将引发以下异常:



I am using sql server 2005 express edition and visual studio 2010 and .net 4.0

The code throws the following exception when i try to execute it:


Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.



我该如何解决以上问题?

在此先感谢.



How can i resolve the above issue?

Thanks in advance.

推荐答案

里德·科普西 [ ^ ]:

为了使用CLR 2.0混合模式程序集,您需要修改App.Config文件以包括:
Solution found on SO[^] written by Reed Copsey[^]:

In order to use a CLR 2.0 mixed mode assembly, you need to modify your App.Config file to include:
<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>


关键是useLegacyV2RuntimeActivationPolicy标志.这将导致CLR使用最新版本(4.0)来加载您的混合模式程序集.没有这个,它将无法工作.

请注意,这仅对混合模式(C ++/CLI)程序集有效.您可以加载所有托管的CLR 2程序集,而无需在app.config中指定.


干杯!


The key is the useLegacyV2RuntimeActivationPolicy flag. This causes the CLR to use the latest version (4.0) to load your mixed mode assembly. Without this, it will not work.

Note that this only matters for mixed mode (C++/CLI) assemblies. You can load all managed CLR 2 assemblies without specifying this in app.config.


Cheers!


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

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