【问题标题】:Need help on network related error connecting to sql server需要有关连接到 sql server 的网络相关错误的帮助
【发布时间】:2015-01-07 14:38:33
【问题描述】:

我遇到了需要连接到数据库的应用程序的问题。以下是我的一些连接字符串:

 public static SqlConnection CustomerDatabaseConnection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=" + CustomerVariables.CustomerDatabasePath + ";Integrated Security=True;Connect Timeout=30");

 public static SqlConnection TemplateDatabaseConnection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=" + TemplateVariables.TemplateDatabasePath + ";Integrated Security=True;Connect Timeout=30");

使用以下代码获取 CustomerVariables.CustomerDatabasePath 的路径:

 public static string baseDirectory = System.IO.Path.GetFullPath("..\\..\\");

 public static string CustomerDatabasePath =  System.IO.Path.Combine(GeneralVariables.baseDirectory, "CustomerDatabase.mdf");

然后我使用以下数据适配器代码:

 public static string CustomerDatabaseSQL = "SELECT* From " + AccountVariables.Username;

 public static SqlDataAdapter CustomerDataAdapter = new SqlDataAdapter(CustomerVariables.CustomerDatabaseSQL, CustomerVariables.CustomerDatabaseConnection);

但是当我尝试填写数据适配器时出现以下错误:

 CustomerVariables.CustomerDataAdapter.Fill(customersDatabaseDataSet);

与 SQL Server 建立连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接(提供程序:命名管道提供程序,错误:40 – 无法打开与 SQL Server 的连接)

【问题讨论】:

  • 您是否按照错误信息中的建议进行操作?
  • 我该怎么做?
  • 检查你的连接字符串connectionstrings.com/sql-server
  • 您的 SQL 数据库正在运行吗?可以通过 SQL Management Studio 之类的查询工具连接到它吗?
  • 检查是否可以从 SQL Management Studio 连接到数据库。如果是,请检查您的连接字符串。

标签: c# .net sql-server


【解决方案1】:

确保 SQL Server 服务正在运行,很可能没有。

按 Windows 键 + R 并在“运行”窗口中键入 services.msc,然后运行 ​​SQL Server 服务

【讨论】:

  • 这是 SQL Server VSS 编写器
  • 不,它的 Microsoft SQL Server this 可能会有所帮助
  • 我的services.msc中好像没有sql server
  • 您确定您的机器上安装了 Microsoft SQL Server 吗?
  • 我刚刚决定安装 SQL Server Express,因为我找不到它。
【解决方案2】:

问题在于原始连接字符串中的 .mdf,例如

public static SqlConnection CustomerDatabaseConnection = new  SqlConnection("Server=localhost;" + "Database=CustomerDatabase.mdf");

如果为所有用户的数据创建类型化数据表并使用表适配器更新这些数据,则应从原始连接字符串中删除 .mdf,问题将得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多