最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - After updating to VS2022 17.13.2, .NET program cannot find LocalDB (MDF) database - Stack Overflow

programmeradmin4浏览0评论

I have an existing .NET Framework 4.8 program that uses a MSI installation. It uses SQL Server 2016 Express with a LocalDB and worked fine for several years.

Recently, I updated VS2022 to 17.13, and the program still runs without issue inside the IDE (whether in debug or release), the setup project still builds the MSI without error, and the MSI installs without error on the test box, but when running the program, it doesn't like the database.

When first encountering this, I get the error:

.Net SqlClient Data Provider--The database 'C:\USERS\TEST\APPDATA\ROAMING\ADVANCED APPLICATIONS\VRDB\VRDB.MDF' cannot be opened because it is version 904. This server supports version 852 and earlier. A downgrade path is not supported.

I realized then that I hadn't upgraded the setup project to require SQL Server 2019 instead of SQL Server 2016. I made that change in the prerequisites and the install still succeeded, but I still got that same error.

I reverted the test box to the base -- thus removing both SQL Server 2016 and SQL Server 2019 installations, as well as this program -- and installed again. It dutifully installed with success but now I get a new error:

.Net SqlClient Data Provider--A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

I've checked the database and it is reporting it is, indeed, a SQL Server 2019 v15 database. And, again, it runs and works fine within the IDE. But something about the project settings(?) isn't happening correctly after installation via the MSI. It is like the program is still trying to operate using SQL Server 2016 but cannot find a suitable database?

I am sure I'm missing something simple but I just don't see it.

ADDL INFO

The event log shows these two errors repeated 61 times in a couplet, in this sequence

Windows API call RegOpenKeyExW returned error code: 2. Windows system error message is: The system cannot find the file specified.
Reported at line: 1035.

LocalDB parent instance version is invalid: MSSQL13E.LOCALDB


PS C:\Users\test> sqllocaldb info
MSSQLLocalDB

So, that first error is saying it cannot find a RegistryKey? That doesn't make sense... I'll dig.

The path to the MDF file is derived using this:

@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\{AssyInfo.Company}\{AssyInfo.Product}"

Which resolves to:

C:\Users\test\AppData\Roaming\Advanced Applications\VRDB\

The connection string resolves to:

@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\test\AppData\Roaming\Advanced Applications\VRDB\VRDB.mdf;Integrated Security=True;Connect Timeout=60"

REGISTRY ERROR

I dug into the registry and found something. Read on and help me understand from where it is getting this key value?

This key:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances\{AB9423F5-FD22-4251-9D76-83264E6792D2}\DataDirectory

Has this value:

C:\Users\test\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB

But should be:

C:\Users\test\AppData\Roaming\Advanced Applications\VRDB

So, I updated that value and ran VRDB again and it worked.

Why is it now trying to find things in different places? I certainly didn't make any changes to that code. Could this be related to having upgraded my dev box to Windows 11 since I last made changes to that program?

发布评论

评论列表(0)

  1. 暂无评论