以下代码在执行期间给出错误。
string connectionString = "Data Source=D:\\Base.sdf;Persist Security Info=False";
SqlConnection sqlConnection = new SqlConnection(connectionString))
sqlConnection.Open();
错误是:
A network-related or instance-specific error occured 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 not configured to allow remote connections. (provider:SQL network Interfaces, error: 26 - Error locating Server/Instance Specified)
我尝试使用
SqlCeConnection
而不是SqlConnection
,但编译器找不到具有该类的库。请帮助解决这个问题。
最佳答案
您的数据库是Sql Server精简版之一,必须使用:
SqlCeConnection sqlConnection = new SqlCeConnection(connectionString);
从此处下载库Microsoft SQL Server Compact 4.0
System.Data.SqlServerCe.dll
的引用System.Data.SqlServerCe;
SqlCeConnection
代替SqlConnection
关于c# - 连接数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157209/