error: 26 - Error Locating Server/Instance Specified
I was doing unit test in the Visual Stdio 2010. Howev开发者_运维技巧er, it kept throw to exception error:
{"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)"}**
I was searching to solve this problem, and I knew that it was problems of connection between visual studio and sql server. (Maybe, maybe not.)
I was trying to change "App.config" and "Web.config".
Is it right way to fix this problem? If it is yes, can you give example of xml to fix this problem? If it is no, what should i do for fixing this problem?
It's impossible to tell exactly what's going on without code (please post some), but from your comments, I am guessing it's because you haven't correctly set up the SQL connection string.
Most of the time, you would have a connection string specified in the .config file (although you might have it hard-coded in your code?). If you're using something like Entity Framework, app.config might look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Depending on what your code is doing, it might look different (again, post some code). If you have the code working in an application, but it's failing in the unit tests, you can probably just copy the connection string from the application's app.config file into the unit test's app.config file. If your unit tests don't have an app.config file, just create one.
By the way, the config above is just one example of how you might specify a connection string... your code might require something different.
Hope that helps point you in the right direction...
Update: still would like to see some code, but if it is looking for connection strings out of your database, they typically look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=myserver;Initial Catalog=MyDatabase;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Hi Every One this is a solution for those who want connect a database with WampServer or Sql Server Management Studio , i try it both and it works both , read carefully , and sorry for my bad english (Tunisian Dev) :
To create a db from classes, using the EF (Entity Framework) Code First method: **1. First create the Context Common class exmple:
Public class MyClassContext: DbContext
{
Public MyClassContext (): base ("name = ConnStringLinkedForDb") {
Database.SetInitializer (new DropCreateDatabaseAlways <MyClassContext>());
}
Public DbSet <ClassX> ClassXs {get;set;}
}
2. In This second point the tag: , Would be in the 2 files "app.config" to the data layer and "web.config" in the web layer,In the following two cases:
2.1. If you want to make a connection with Wamp Server (idUserName = root, pwd = 'empty') and our bd would be on its server:
<ConnectionStrings>
<Add name = "ConnStringLinkedForDb" connectionString = "server = localhost; user id = root; persistsecurityinfo = True; database = db_name_desired; allowuservariables = True" providerName = "MySql.Data.MySqlClient"/>
</ ConnectionStrings>
2.2.with sql Server Management Studio (ServerName (Source) =YourServerNameInSqlServerManegementStudioProprity(onObjectExplorer), pwd = 'empty') and that our bd would be on its server:
<ConnectionStrings>
<Add name = "ConnStringLinkedForDb" connectionString = "Data Source = YourServerNameInSqlServerManegementStudioProprity(onObjectExplorer); Initial Catalog = db_name_desired ;Integrated Security = true" providerName = "System.Data.SqlClient" />
</ ConnectionStrings>
**
I had a similar issue. If you had:
- Already created the database
- Changed development machine/reinstalled SQL server
- Attached your previous database
- go to your root project folder:
bin->Debug
orbin->Release
- locate your .config file
- Open with notepad and edit your connection string there
- Next (if you had not), open your project in visual studio and locate App.config in Solution Explorer, edit the connection string there to match your current connection.
精彩评论