需要有关连接到 sql 服务器的网络相关错误的帮助

Need help on network related error connecting to sql server

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

 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);

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: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

确保 SQL 服务器服务是 运行ning,很可能不是。

按 Windows 键 + R 并在 运行 window 中输入 services.msc 然后 运行 SQL 服务器服务

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

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

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