简体   繁体   English

SQL Server用户实例错误:现有数据库已达到允许的最大数量

[英]SQL Server User Instances error: Existing databases has reached the max number allowed

I'm using C# in Visual Studio 2008 to loop through MDF Files on my PC and extract data from within them. 我在Visual Studio 2008中使用C#在我的PC上循环MDF文件并从中提取数据。 I'm using a Table Adapter to point to the local MDF file. 我正在使用表适配器指向本地MDF文件。

Recently one of my PC's refuses to let me attach any new Data Source as it says 最近我的一台PC拒绝让我附上任何新的数据源,正如它所说的那样

System.Data.SqlClient.SqlException: Unable to create/attach any new database because the number of existing databases has reached the maximum number allowed: 32766 System.Data.SqlClient.SqlException:无法创建/附加任何新数据库,因为现有数据库的数量已达到允许的最大数量:32766

Even if I start a fresh Windows application and try to add an MDF file (on my desktop) to it as a Data Source, I get the above error. 即使我启动一个全新的Windows应用程序并尝试将MDF文件(在我的桌面上)作为数据源添加到它,我也会收到上述错误。

Can anyone tell me how to remove/delete the existing connections ? 谁能告诉我如何删除/删除现有连接?

My code works fine on another PC and I've re-installed Visual Studio on my PC but still get this error. 我的代码在另一台PC上工作正常,我在我的电脑上重新安装了Visual Studio但仍然出现此错误。

C# Table adapter code: C#表适配器代码:

tmTableAdapter.Connection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='" + pathofmdffile + "';Integrated Security=True;Connect Timeout=30;User Instance=True";
tmTableAdapter.Connection.Open();

Reinstalling VS won't help as the error is coming from SQL Server. 重新安装VS将无法解决,因为错误来自SQL Server。

Look at these MSDN pages which have lots of good info: 看看这些有很多好消息的MSDN页面:

Since you are using "User Instances", I guess the DBs won't show up when connecting to the main SQLEXPRESS instance. 由于您使用的是“用户实例”,我想连接到主SQLEXPRESS实例时,DB不会显示。

Things to check: 要检查的事项:

  • In SSMS, connect to your last attached DB by doing the following: 在SSMS中,通过执行以下操作连接到上次连接的数据库:

    • Go to Object Explorer 转到对象资源管理器
    • New Connection to a Database Engine 与数据库引擎的新连接
    • In the "Connect to Server" popup: 在“连接到服务器”弹出窗口中:
    • Click the "Options >>" button 单击“选项>>”按钮
    • Go to the "Additional Connection Properties" tab 转到“其他连接属性”选项卡
    • Enter in the following in the text area: 在文本区域中输入以下内容:
      User Instance = true
    • Click the "Connect" button 单击“连接”按钮
    • Expand "Databases" folder. 展开“数据库”文件夹。 You should see them all, named as the full path to the MDF file 您应该全部看到它们,命名为MDF文件的完整路径
    • Right-click on a database 右键单击数据库
    • Go to "Tasks >" 转到“任务>”
    • First option is "Detach" 第一个选项是“分离”

    Obviously this isn't practical for 32,766 databases, but for a few it is the best option. 显然这对于​​32,766个数据库来说并不实用,但对于少数数据库来说这是最好的选择。

  • By default the sqlservr.exe process hangs around for 60 minutes, and any databases you attach prior to it terminating gets added to the list and likely reset the expiration counter. 默认情况下, sqlservr.exe进程会挂起60分钟,并且在终止之前附加的任何数据库都会添加到列表中,并可能会重置到期计数器。 You can end the process immediately by connecting to the most recent attached DB (as noted above; those steps will work for a New Query, or if connecting via Object Explorer, then right-click on the instance name and go to "New Query"), and run the following: 您可以通过连接到最新附加的数据库立即结束该过程(如上所述;这些步骤适用于新查询,或者如果通过对象资源管理器连接,则右键单击实例名称并转到“新查询” ),并运行以下内容:

     SHUTDOWN; 

    This will clear out all connected databases in one shot. 这将一次性清除所有连接的数据库。

  • Set the timeout to be less than 60 minutes. 将超时设置为小于60分钟。 According to the SQL Server Express User Instances page (same link as above): 根据SQL Server Express用户实例页面(与上面相同的链接):

    A system administrator on the parent instance can set the duration of the time-out period for a user instance by using sp_configure to change the user instance timeout option. 父实例上的系统管理员可以使用sp_configure更改用户实例超时选项来设置用户实例的超时期限。 The default is 60 minutes. 默认值为60分钟。

    In SSMS (make sure you are connected to the parent instance): 在SSMS中(确保您已连接到实例):

     -- View current setting (in the "run_value" field) EXEC sp_configure 'user instance timeout' -- Documentation says default is 60 but mine was 5 -- If you can't see the option, run the following: EXEC sp_configure 'show advanced options', 1; RECONFIGURE; -- To change it, run the following: EXEC sp_configure 'user instance timeout', 5; 
  • Use the "SQL Server Express Utility" to detach one or more databases: 使用“SQL Server Express Utility”分离一个或多个数据库:

  • Look in the following directory which will have the 4 system DBs: 查看以下目录中将包含4个系统DB:

    • On XP / Vista: C:\\Documents and Settings{UserName}\\Local Settings\\Application Data\\Microsoft\\Microsoft SQL Server Data\\SQLEXPRESS 在XP / Vista上: C:\\ Documents and Settings {UserName} \\ Local Settings \\ Application Data \\ Microsoft \\ Microsoft SQL Server Data \\ SQLEXPRESS

    • On Windows 7 and newer: C:\\Users{UserName}\\AppData\\Local\\Microsoft\\Microsoft SQL Server Data\\SQLEXPRESS 在Windows 7及更高版本: C:\\ Users {UserName} \\ AppData \\ Local \\ Microsoft \\ Microsoft SQL Server Data \\ SQLEXPRESS

    This is mostly just informational as these files cannot be deleted so long as the SQL Server process is running. 这主要是信息性的,因为只要SQL Server进程正在运行,就无法删除这些文件。


For an on-going basis, if you will be attaching many DBs in a loop, you can programatically get rid of them in that process by running sp_detach_db when you are done using each DB: 对于持续进行的基础,如果要在循环中附加许多DB,则可以通过在使用每个DB时运行sp_detach_db以编程方式去除它们:

USE [master];
ALTER DATABASE [{DatabaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
EXEC sp_detach_db @dbname =N'{DatabaseName}';

And just FYI, "User Instances" are now deprecated. 仅供参考,“用户实例”现已弃用。 You should look into using SQL Server Express LocalDB . 您应该考虑使用SQL Server Express LocalDB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 达到SQL Server最大数据库数量 - SQL Server max databases number reached 在dotnet核心中读取json文件时出错“已达到inotify实例数量的已配置用户限制(128)” - Error while reading json file in dotnet core “the configured user limit (128) on the number of inotify instances has been reached” SQL Server:已达到最大池大小 - SQL Server: Max pool size was reached SQL Server用户实例(相对于CE和其他嵌入式数据库)的非开发案例是什么? - What are the non-dev cases of SQL Server User Instances (vs CE and other embedded databases)? IdentityServer 4和预先存在的SQL Server数据库 - IdentityServer 4 and pre-existing SQL server databases sql server 2008中的最大数据库数 - Maximum number of databases in sql server 2008 将Twitter示例流保存到RavenDB会导致以下异常:已达到该会话允许的最大请求数(30) - Saving a Twitter Sample Stream to RavenDB results in the exception The maximum number of requests (30) allowed for this session has been reached 更新SQL Server查询循环,直到达到X号 - Updating SQL Server query loop until X number reached 错误达到最大池大小? - Error max pool size was reached? SQL 服务器:将不存在的号码检查到表中 - SQL Server : check not existing number into a table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM