我在Visual Studio中有两个项目:我的正常项目和我的测试项目。我选择使用SpecFlow进行测试,因此我的测试是基于UI的。因此,UI验证不需要数据库连接。
在某些情况下,我必须为系统设置一些先决条件,例如在项目中填充一些样本数据。我试图将我的项目数据库连接到我的测试项目,但它无法正常工作。我尝试将连接字符串添加到我的测试项目app.config中,如下所示:
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=C:\Users\Martijn\Documents\VS11\Projects\Gastouderuren.nl\testprojectl\App_Data\example.sdf"
providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
当我在代码中使用此连接字符串时,如下所示:
MyContext context = new MyContext(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
context.Product.Count();
当我运行测试时,会发生以下错误:
The provider did not return a ProviderManifestToken string. -> This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection. -> 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
我已经搜索了一个解决方案,但找不到任何东西。我该如何解决这个问题?
谢谢!
答案 0 :(得分:0)
您无法连接到外部 localdb (基于文件),就像您用来连接普通SQL数据库一样 (连接字符串)
因为localdb限制(一次只能连接1个应用程序)
但我有一个问题:(如何使用2个应用程序和1个localdb)
你也可以设置一些预先定义的命令(我做同样的事情)
SERVER :
if(CLIENT DATA . equals("ReadProducts"){
sql.execute("SELECT * FROM Products");
//convert...
SendBack(result);
}
CLIENT 1 >SEND> SERVER : "ReadProducts" (Predefinded command)
SERVER >SEND> CLIENT 1 : "Apple,Melon,Orange"
CLIENT 1 :
data = SERVER RESPONSE;
string[] sp=data.split(',');
//add a for loop which add all products to your listview
CLIENT 2 >SEND> SERVER : "INSERT INTO Products(name) VALUES(@name)"
SERVER :
if(CLIENT DATA . contains("INSERT"){
sql.execute(CLIENT DATA);
}
使用算法abow,您可以处理与单个LocalDb文件的多个连接
从其他计算机,但你必须处理IP问题
如果您正在开发大型的
,请获取服务器应用的静态IP地址