【发布时间】: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