使用C#&TSQL开发考勤系统时,添加数据遇数据库连接错误求助
Hey, let's tackle those LocalDB connection and schema retrieval issues you're hitting with your school attendance system. Both errors boil down to problems with how your app is accessing the database file, so let's break this down step by step:
Core Cause of Both Errors
Error 1 (login failure) and Error 2 (schema retrieval failure) are directly linked:
- The login failure happens because the LocalDB instance can't access your
.mdffile either due to permission issues for userTom\Tom Slater, or the file is locked by another running process. - The schema retrieval error is just a side effect—if the app can't connect to the database at all, it can't pull any schema information.
Step-by-Step Fixes
1. Fix Connection String Path Mismatch
Your connection string uses |DataDirectory|, but the error log shows an absolute file path. Here's how to align them properly:
- First, confirm your
GardenRegister.mdfandGardenRegister_log.ldffiles are being copied to your project's output directory (usuallybin\Debug\App_Data). If not, add a post-build event to automate this:- Right-click your project → Properties → Build Events
- In the Post-build event command line box, add:
xcopy "$(ProjectDir)GardenRegister.mdf" "$(TargetDir)App_Data\" /Y xcopy "$(ProjectDir)GardenRegister_log.ldf" "$(TargetDir)App_Data\" /Y
- Alternatively, use the absolute path directly in your connection string (though
|DataDirectory|is better for deployment flexibility):string ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\NEA\GARDENREGISTER\GARDENREGISTER\GARDENREGISTER.MDF;Integrated Security=True";
2. Resolve Permission & Database Lock Issues
- Release locked files: Close all open database editors (like SSMS tabs or Visual Studio Server Explorer connections for this database), then restart Visual Studio entirely.
- Set file permissions: Right-click your
.mdffile → Properties → Security → Edit → Add userTom\Tom Slaterand grant Full Control to ensure LocalDB can read and write to the file. - Reset LocalDB instance: If the instance is corrupted, open an elevated Command Prompt and run these commands:
Then re-attach your database in Visual Studio's Server Explorer.sqllocaldb stop MSSQLLocalDB sqllocaldb delete MSSQLLocalDB sqllocaldb create MSSQLLocalDB sqllocaldb start MSSQLLocalDB
3. Fix Code & Stored Procedure Edge Cases
Your registration code and stored procedure have small tweaks that will prevent future issues:
- DOB parameter handling: Using
DT_DobReg.Textcan cause date format mismatches. Instead, pass theDateTimevalue directly:SqlCmd.Parameters.AddWithValue("@DOB", DT_DobReg.Value); - Simplify connection management: You don't need
SQLCon.Close();—theusingblock automatically disposes the connection when it exits. Remove that line to clean up your code. - Verify table-procedure compatibility: Double-check that your
Membertable's column types match the stored procedure parameters (e.g.,NameisVARCHAR(30),DOBisDATETIME) to avoid insertion failures.
4. Validate Connection in Visual Studio
- Open Server Explorer → Right-click Data Connections → Add Connection
- Select Microsoft SQL Server LocalDB, enter the instance name
(LocalDB)\MSSQLLocalDB, then click Attach to select your.mdffile. - If the connection succeeds, right-click the database → Refresh—then test your registration form again.
Final Notes
Start with the connection path and permission fixes—those are the root of your current errors. Once those are sorted, the schema retrieval issue should disappear automatically. Then tweak the date parameter handling to make your registration code more robust.
内容的提问来源于stack exchange,提问作者WMTS

