【问题标题】:Can't connect to SQL 2008 database using .NET Core 2.0无法使用 .NET Core 2.0 连接到 SQL 2008 数据库
【发布时间】:2017-07-26 11:49:56
【问题描述】:

更新

我永远无法使用“Windows 身份验证”(域)用户来完成这项工作。但是对于“SQL Server 身份验证”用户,一切正常。

原始问题

我的连接字符串:Server=ip;Database=dbname;User Id=xxx\user;Password=pass;

连接字符串位于 appsettings.json 中,如下所示:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "ConnectionString": "Server=ip;Database=dbname;User Id=xxx\user;Password=pass;"
  }
}

然后我将它从“Startup.cs”文件传递给静态类,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    Orm.DatabaseConnection.ConnectionString = Configuration["ConnectionStrings:ConnectionString"];
}

这是我发起连接的地方:

using System.Data.SqlClient;

namespace MyProject.Orm
{
    public static class DatabaseConnection
    {
        public static string ConnectionString { get; set; }

        public static SqlConnection ConnectionFactory()
        {
            return new SqlConnection(ConnectionString);
        }
    }
}

这是我的控制器:

public string Get()
{
    using (var databaseConnection = Orm.DatabaseConnection.ConnectionFactory())
    {
        var sections = databaseConnection.Query("SELECT * FROM myTable").ToList();
        return sections.ToString();
    }
}

这一行在哪里:

var databaseConnection = Orm.DatabaseConnection.ConnectionFactory();

返回:

ServerVersion: "'databaseConnection.ServerVersion' threw an exception of type 'System.InvalidOperationException'"
Message: "Invalid operation. The connection is closed."
Source: "System.Data.SqlClient"
StackTrace: "at 
System.Data.SqlClient.SqlConnection.GetOpenTdsConnection()\n   
at 
System.Data.SqlClient.SqlConnection.get_ServerVersion()"

我在new SqlConnection:"error CS0119: 'SqlConnection' is a type, which is not valid in the given context" 上收到此错误。

但是程序执行并没有因为这些错误而停止。

然后应用程序挂在以下行:

var sections = databaseConnection.Query("SELECT * FROM myTable").ToList();

我使用 Dapper 作为我的 ORM(不是 EntityFramework)。在“myTable”中,sql 表只有 17 行和 5 列,因此加载速度应该很快。

我尝试了各种不同的连接字符串,但总是失败。如果我尝试使用 .NET Framework 4.5,一切正常。问题是 .NET Core 2.0。

欢迎任何关于修复它的想法。因为我已经在这上面花了太多时间了。

【问题讨论】:

  • 尝试将 Server=ip 替换为 Server=IP\您的 SQL 实例名称
  • @andy 我试过但得到了完全相同的错误。
  • 你确定它会抛出 that 行吗? SqlConnection 构造函数不应该用ServerVersion 属性做任何事情。如果您尝试在关闭的连接 (new SqlConnection("").ServerVersion) 上读取 .ServerVersion,这正是您得到的错误,我希望稍后会发生这种情况。
  • 我同意@JeroenMostert 简单地创建一个新的SqlConnection 对象不应该抛出这个错误afaik。它与您尝试使用 SqlConnection 时有更多关系,可能与 Open() 一起使用。
  • 我不知道这条线,因为没有抛出异常。只有当我设置断点时,我才能看到这个错误。但是没有提供它发生的地方。 :(

标签: sql-server sql-server-2008 .net-core dapper asp.net-core-2.0


【解决方案1】:

尝试添加databaseConnection.Open()

public string Get()
{
    using (var databaseConnection = new SqlConnection(@"Server=ip;Database=dbname;User Id=xxx\user;Password=pass;Pooling=false;"))
    {
        databaseConnection.Open();
        var sections = databaseConnection.Query("SELECT * FROM myTable").ToList();
        return sections.ToString();
    }
}

为避免在 cmets 中描述的连接池出现问题,请将 Pooling=false; 添加到连接字符串: Server=ip;Database=dbname;User Id=xxx\user;Password=pass;Pooling=false;

编辑: 我对连接字符串进行了硬编码并删除了工厂以使示例更小

【讨论】:

  • 现在挂在:databaseConnection.Open();。但在此之前,如果我设置一些断点,我会在new SqlConnection 上收到此错误:"error CS0119: 'SqlConnection' is a type, which is not valid in the given context"。程序执行并没有因为这个错误而停止。
  • 这是 DatabaseConnection.ConnectionFactory() 中的编译错误吗?我没有任何new SqlConnection 行。之前是怎么编译的?
  • @JedatKinports,我只更改连接字符串,不会导致任何编译错误
  • 你能连接成功吗?你能分享你使用的connectionSting(当然隐藏凭据)吗?或者,如果您可以分享您的 connectionString 在 appsettings.json 文件中的外观。赞赏。
  • @JedatKinports 老实说,我没有测试我的解决方案。我有一个想法,将Pooling=false; 添加到连接字符串可以帮助您解决连接池问题并编写答案。您提供的代码必须经过编译。如果您有编译问题,请更新您的问题。您还应该在工厂中对连接字符串进行硬编码,因为阅读配置不属于您的问题
【解决方案2】:

尝试创建一个self-contained deployment,这应该消除和奇怪的依赖关系。如果它有效,那么至少您知道这是由于某些程序集绑定类型的东西造成的。

异常 "error CS0119: 'SqlConnection' is a type, which is not valid in the given context" 闻起来像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多