本地数据库连接字符串不起作用,但上下文连接

时间:2018-06-14 21:05:40

标签: c# .net visual-studio-2012 clr

我放弃了。我用谷歌搜索,阅读了大量的SO帖子......我无法弄清楚这一点。我正在使用Visual Studio 2012 .NET 3.5连接到本地数据库实例进行测试。如果我使用

string cnn = @"Context Connection=True";
        using (SqlConnection connection = new SqlConnection(cnn))
        {
            connection.Open();

我连接并且我的代码有效。我现在需要在此解决方案中添加SqlbulkCopy。为此,我将连接字符串切换为:

string cnn = @"Data source= (localdb)\ProjectsV12; Database=TableTesting; Trusted_Connection=True;";
        using (SqlConnection connection = new SqlConnection(cnn))
        {
            connection.Open();

无论我对连接字符串做什么,我都会收到以下错误:

A .NET Framework error occurred during execution of user-defined routine or aggregate "WorkTable": 
System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException: 
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.PermissionSet.Demand()
   at System.Data.Common.DbConnectionOptions.DemandPermission()
   at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at StoredProcedures.WorkTable(String o)

我无法弄清楚如何调试/修复/更正此问题。

1 个答案:

答案 0 :(得分:0)

在遭遇重大混乱之后...... PERMISSION LEVEL似乎需要UNSAFE并且必须检查TRUSTWORTHY此连接string string cnn = "Data Source=(localdb)\\ProjectsV12; Initial Catalog=TableTesting; Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; 工作正常。