You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已引用System.Data.SqlClient,C#仍无法识别SqlConnection求助

Fixing "The type or namespace 'SqlConnection' could not be found" in Visual Studio

Hey there, let's work through this SqlConnection error you're seeing—even with the using System.Data.SqlClient; directive in place, there are a few common gotchas in Visual Studio that might be causing this. Here's what to check step by step:

  • Verify you've added the correct reference/NuGet package

    • For .NET Framework projects: SqlConnection lives in the System.Data assembly. Right-click your project in Solution Explorer → select Add → Reference → go to the Assemblies tab, find System.Data, check the box next to it, and click OK.
    • For .NET Core/.NET 5+ projects: The old System.Data.SqlClient namespace has been moved to a NuGet package. Right-click your project → select Manage NuGet Packages → go to the Browse tab, search for Microsoft.Data.SqlClient, install the latest stable version. After installing, you can either use using Microsoft.Data.SqlClient; (the modern recommended namespace) or keep using System.Data.SqlClient; (most versions still support this for compatibility).
  • Check your project's target framework
    Make sure your project is targeting a framework version that supports SqlConnection. Right-click your project → Properties → go to the Application tab, check the Target framework dropdown. For .NET Framework, aim for 4.6.1 or newer; for .NET Core/.NET, 3.1 or newer works best.

  • Clean and rebuild your solution
    Visual Studio's build cache can sometimes cause false errors. Go to the top menu → Build → Clean Solution, then Build → Rebuild Solution. If that doesn't help, try restarting Visual Studio entirely to clear any cached issues.

  • Quick note on your code
    Also, a small side issue: your entry point method should be Main (capital M) instead of main—C# is case-sensitive, so public static void Main() is required for the program to run correctly once you fix the SqlConnection error.

内容的提问来源于stack exchange,提问作者user9758771

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:12:27