相关疑难解决方法(0)

已经有一个与此命令关联的打开DataReader,必须先关闭它

我有这个查询,我在这个函数中得到错误:

var accounts = from account in context.Accounts
               from guranteer in account.Gurantors
               select new AccountsReport
               {
                   CreditRegistryId = account.CreditRegistryId,
                   AccountNumber = account.AccountNo,
                   DateOpened = account.DateOpened,
               };

 return accounts.AsEnumerable()
                .Select((account, index) => new AccountsReport()
                    {
                        RecordNumber = FormattedRowNumber(account, index + 1),
                        CreditRegistryId = account.CreditRegistryId,
                        DateLastUpdated = DateLastUpdated(account.CreditRegistryId, account.AccountNumber),
                        AccountNumber = FormattedAccountNumber(account.AccountType, account.AccountNumber)
                    })
                .OrderBy(c=>c.FormattedRecordNumber)
                .ThenByDescending(c => c.StateChangeDate);


public DateTime DateLastUpdated(long creditorRegistryId, string accountNo)
{
    return (from h in context.AccountHistory
            where h.CreditorRegistryId == creditorRegistryId && h.AccountNo == accountNo
            select h.LastUpdated).Max();
}
Run Code Online (Sandbox Code Playgroud)

错误是: …

c# entity-framework entity-framework-4

592
推荐指数
7
解决办法
41万
查看次数

配置EF6使用varchar作为默认值而不是nvarchar?

我知道我可以为每个属性添加一个数据注释来设置它的列类型VARCHAR但是我有数百个列我想要VARCHAR而不是NVARCHAR有一些方法来设置EF6使其成为默认值吗?

ef-code-first entity-framework-6

19
推荐指数
1
解决办法
7157
查看次数

无法使用.NET Core 2.0连接到SQL 2008数据库

UPDATE

我永远无法使用"Windows身份验证"(域)用户进行此操作.但是使用"SQL Server身份验证"用户,一切都像它应该的那样工作.

原始问题

我的connectionString: 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;"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我将它传递给"Startup.cs"文件中的静态类,如下所示:

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

    Orm.DatabaseConnection.ConnectionString = Configuration["ConnectionStrings:ConnectionString"];
}
Run Code Online (Sandbox Code Playgroud)

这是我发起连接的地方:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

public string Get()
{
    using (var databaseConnection = …
Run Code Online (Sandbox Code Playgroud)

sql-server sql-server-2008 dapper .net-core asp.net-core-2.0

17
推荐指数
1
解决办法
1056
查看次数

如何找到泄漏的数据库连接池句柄?

我看到了可怕的"在从池中获取连接之前超时时间已过去"错误.

我搜索了代码中任何未闭合的数据库连接,但找不到任何.

我想要做的是:下次我们收到此错误时,让系统转储一个列表,其中包含哪些proc或http请求持有所有句柄,因此我可以找出导致问题的代码.

更好的是看看这些手柄有多长时间,所以我可以发现使用但未封闭的连接.

有没有办法做到这一点?

asp.net connection-pooling

16
推荐指数
2
解决办法
2万
查看次数

Dapper.NET连接/查询最佳实践

所以我已经阅读了一堆链接/ SO问题,但我仍然无法得到明确的答案.

在ASP.NET应用程序中使用Dapper执行SQL查询时,打开/关闭连接的最佳做法是什么?

这是我目前正在遵循的模式:

using (var db = new SqlConnection(_connectionString))
{
    return db.Query("dbo.SomeQuery");
}
Run Code Online (Sandbox Code Playgroud)

基本上,每次根据需要打开/关闭SQL连接.

根据我的理解,上面的代码应该自动打开/关闭SQL连接(例如我不需要明确做db.Open或者db.Close).

我看到的问题是,过了一段时间,我得到了一堆这些错误:

InvalidOperationExceptionTimeout已过期.从池中获取连接之前经过的超时时间.这可能是因为所有池连接都在使用中并且达到了最大池大小.

在这段时间里我有一个SQL分析器跟踪,我没有看到任何会阻塞任何内容的长查询,所以看起来好像我的ASP.NET Web应用程序没有连接(而不是花费太长时间)执行查询).

有人能告诉我我做错了什么吗?

旁注:我的应用程序是作为Azure Web App运行的,所以很遗憾我实际上无法看到Web应用程序正在打开多少个连接.:(

.net c# sql-server asp.net dapper

10
推荐指数
1
解决办法
4247
查看次数

超时已过期.从池中获取连接之前经过的超时时间.

我正在使用WebApiAngularJS开发应用程序.我花了一些时间申请后得到这个例外.我在这个应用程序中使用EntityFramework.

"Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."

堆栈跟踪

at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
? at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

6
推荐指数
3
解决办法
4万
查看次数

如何重用重新打开连接的代码?

我们的生产服务器会终止非活动连接,因此我们的API需要在需要时恢复它们.以下代码有效,但它非常重复:

   private const int MaxRetryCount = 3;

    public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
    {
        int retryCount = 0;

        while (retryCount++ < MaxRetryCount)
        {
            try
            {
                if (command.Connection.State == ConnectionState.Closed)
                    command.Connection.Open();
                return command.ExecuteReader();
            }
            catch(Exception e)
            {
                if(!e.Message.ToLower().Contains("transport-level error has occurred"))
                {
                    throw;
                }
            }
        }
        throw new Exception("Failed to restore connection for command:"+command.CommandText);
    }

    public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command)
    {
        var retryCount = 0;
        while(retryCount++ < MaxRetryCount)
        {
            try
            {
                if (command.Connection.State == ConnectionState.Closed)
                    command.Connection.Open();
                command.ExecuteNonQuery();
                return;
            }
            catch(Exception e)
            { …
Run Code Online (Sandbox Code Playgroud)

c# ado.net refactoring c#-4.0

1
推荐指数
1
解决办法
489
查看次数

如何关闭实体框架中的连接字符串对象

今天,当我从下面的代码运行我的应用程序时出现异常

dbContext.ManageTasks.Where(x => x.IsDeleted == false).ToList();
Run Code Online (Sandbox Code Playgroud)

错误是

超时已过。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且达到了最大池大小。

我从这个讨论中得到了一个解决方案:如何解决 ASP.NET 和 SQL Server 之间的连接池问题?

解决方案是:如果我关闭当前的连接字符串对象,那么这个错误就会消失。

SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
// some code
myConnection.Close();        
Run Code Online (Sandbox Code Playgroud)

上面的 ADO.NET 代码,而不是实体框架。

如何关闭实体框架中的连接字符串对象?

c# asp.net entity-framework

0
推荐指数
1
解决办法
2876
查看次数