System.Data.SqlConnection无法正常工作,寻求技术协助
Hey there, let’s work through this issue step by step—first off, a quick clarification: System.Data.SqlConnection is part of the .NET Framework’s core libraries, so manually adding a "System.Data.SqlConnection.dll" file isn’t the right approach here. That’s likely where you’ve hit a snag. Here are the actionable fixes to try:
Confirm your project’s .NET Framework target version
System.Data comes included with .NET Framework 2.0 and later, so make sure your project is targeting a compatible framework:- Right-click your project in Solution Explorer → Properties
- Navigate to the Application tab
- Check that the Target framework dropdown has a valid .NET Framework version selected (avoid .NET Core/.NET 5+ here unless you’re using the updated SqlClient package)
Add the correct System.Data reference (if missing)
If your project doesn’t have the core System.Data assembly referenced:- Right-click References in Solution Explorer → Add Reference
- Go to Assemblies → Framework, then find and check System.Data
- Click OK to add the reference to your project
Validate your connection string format
Invalid connection strings are a super common culprit. Double-check yours follows the standard pattern for .NET Framework:string connectionString = "Data Source=YOUR_SERVER_NAME;Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True;";Swap out
YOUR_SERVER_NAMEandYOUR_DATABASE_NAMEwith your actual server and database details.Check for missing namespace imports
Make sure you’ve added the required namespace at the top of your code file:using System.Data.SqlClient;(Note: For newer .NET versions, this moved to Microsoft.Data.SqlClient, but Visual Studio 2017 + .NET Framework uses the System.Data.SqlClient namespace.)
Clean and rebuild your project
Build artifacts can sometimes cause weird reference issues. Try:- Go to Build → Clean Solution
- Then Build → Rebuild Solution
From your error screenshot (which points to a compilation issue tied to missing references or incorrect namespaces), these steps should cover the most common fixes. If you’re still stuck, share the exact error text from the Output window—that’ll help us narrow things down even more!
内容的提问来源于stack exchange,提问作者karim merdessi

