Entity framework 4 Underlaying provider failed on Open - c#

I am trying to connect to the database but I am getting this exception:
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've googled a lot, but I could not find the answer.
Here's my configuration:
I use MS SQL Server 2008 Express. Both Sql Server and Sql Server Browser use my account (I am an administrator). I've also used local system and local service. Without success.
Here is my connectionstring in app.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="EventManagerEntities" connectionString="metadata=res://*/Entities.RelationModel.csdl|res://*/Entities.RelationModel.ssdl|res://*/Entities.RelationModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=martijn-laptop\sql2008;Initial Catalog=EventManager;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
I've also tried to use the sql server username and password, also without success.
I am using winforms. What else can I try?
When I check netstat -an I get this:
TCP 0.0.0.0:1433 0.0.0.0:0 LISTENING
TCP [::]:1433 [::]:0 LISTENING
I don't think this is correct, right? But how do I set it right?

It cannot find instance/server. Telnet the 1433th or the configured port of martijn-laptop\sql2008 to verify its accessible

Go into the SQL Server Configuration Manager and make sure you have the protocols you want to use enabled under "SQL Server Network Configuration" > "Protocols for Sql2008"

From the error it seem like there is connectivity to database issue. Assuming this solution has been working elsewhere have you setup and connected to a different database?
Is so validate that the code generated with edmx has the correct default database connection name or that you are overriding it:
var context = new EventManagerContext("EventManagerEntities");
Or whatever the correct systax for EF is.

Related

I am unable to connect to my SQL Server 2008 on Remote Server

I am Trying to connect to my SQL Server but I keep getting this 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) (.Net SqlClient Data Provider)
This is my connection string
<connectionStrings>
<add name="myDB"
providerName="System.Data.SqlClient"
connectionString="Server=Usman-PC\SQLExpress; Database=myDB; Integrated Security=True;"/>
</connectionStrings>
I have google for solution and tried many things but keep getting this error.
I was developing accounts app and now it is time for deployment on production server. Please somebody help me.
Below are my settings
Computer name: Usman-PC
WorkGroup: USMAN-SERVER
Ip address: 192.168.0.1
Server Name: Usman-PC\SQLExpress
Instance Name: SQLEXPRESS
What I have tried:
I have looked at connection setting in Server Properties and they are good.
I also have checked and server is running.
I also have enabled TCP/IP and set the port to 1433.
I also have added new rule for port 1433 in firewall.
I am also able to transfer files between both computers.

Unable to connect to SQL Server in a C# program but system is able to connect

I am attempting to connect to a SQL Server 2014. A week or so ago it seemed to work, now it does not. The systems team did upgrade recently to 2014. What's weird is that I'm able to use SQL Server Management Studio to connect, ODBC, and telnet. It's just an issue with C#. Additionally, this exact program runs on another server as compiled/release. It's just not running on my system using debug (or fully compiled/release).
I checked through the documentation -- disabled all the firewalls, had STP check the server... nada
Error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: 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)
My C# code:
namespace SQLTest
{
class Program
{
static void Main(string[] args)
{
string connectionString = ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString;
string OrganizationSqlStr = #"SELECT * FROM school";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(OrganizationSqlStr, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.Write(reader["School_ID"] + " | ");
Console.Write(reader["title"] + "\n\r");
}
Console.ReadKey();
}
}
}
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="SQLConn"
connectionString="server=SERVER;database=DATABASE;uid=USERNAME;password=PASSWORD"/>
</connectionStrings>
</configuration>
I would try to force the .net program to use tcp
http://www.connectionstrings.com/sqlconnection/
find the section labeled
Connect via an IP address
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Network Library=DBMSSOCN is the magic sugar.
see also:
What does DBMSSOCN stand for?
APPEND:
If that still produces an error, then something is probably blocking your access.
Here is the in-general tool to determine.
https://www.microsoft.com/en-us/download/details.aspx?id=24009
Specify the IP and Port Number, and it'll tell you it the port is blocked or filtered.
My personal history is that we had a new router installed that was filtering the port number and stopping my sql server access. The portqueryui revealed the issue. PortQry works "outside" of sql server, so the network guys can't blame sql-server for the lack of connectivity.
APPEND
The OP posted this in the comments of this answer
portqry.exe -n db-alexandrite.domain.edu -e 1433 -p TCP exits with return code 0x00000000
This is your problem. "db-alexandrite.domain.edu" is not resolving.
You can fix it by either changing "db-alexandrite.domain.edu" to be the actual IP address of that machine.
OR figuring out why "db-alexandrite.domain.edu" does not resolve.
Ask your team to check whether they have enabled all network protocols the server. Either tcp or named pipe protocol should be enabled.
This error is helpful:
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
It seems that your connection string, or SqlConnection by default, wants to use the named pipe provider to connect to the SQL server.
Given that your server was recently changed/upgraded, it's certainly possible that the named pipe provider was disabled. It's a common mistake.
You can force your connection to use simple TCP instead of named pipe by altering your connection string:
server=tcp:mysqlserver.domain.com;...
I recently set up a new test server with sql and went through the usual checklist.
Make sure remote connections are allowed.
Make sure you select integrated mode is using both Windows and Sql Server logins.
Make sure port 1440/your port and tcp/your protocol is enabled in sql configuration manager.
Make sure your firewall allows incoming and outgoing.
Make sure your connection string is valid.
These 4 steps will eliminate most connection issues. However, on new servers, the Windows Defender will block by default. I had to disable private and public profiles in defender as it was blocking. Since we do not use windows defender this was ok. However, if you rely on it then you need to create a rule.
<add name="SQLConn" connectionString="Data Source=SERVER;Integrated Security=False;User ID=USER;Password=PASSWORD;Initial Catalog=DATABASE" providerName="System.Data.SqlClient" />

error: 40 - Could not open a connection to SQL Server

I am getting following error when trying to login on asp.net application.
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)
In my web.config file, the connection string is like
<add name="DefaultConnection"
connectionString="Server=1.1.1.1; Database=XYZ; Uid=user; Pwd=pswd;"
providerName="System.Data.SqlClient" />
Web and database server are separated. There are other websites on same web server pointing to different database on same db server, work fine.
When I double click on user name in SQL Server 2008 R2 (Security -> Users -> user) I get the following error:
I have checked SQL Server service and they are running fine. Just wondering if there is anything to do with this user in SQL Server.
This error is not user related. Check to ensure remote connections are enabled on SQL Server via the SQL Server Configuration Manager utility and port 1433 is open on the firewall. You can test port connectivity using TELNET or with the Powershell command below.
1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",$_)) "server listening on TCP port $_" }
Named Pipes protocol (see error) is only for connection on same machine and this means (probably) that on SQL Server is not enabled tcp/ip protocol for external communication.
Another possible reason for the error is the named instance of sql server; on your connection string you put the ip address of the server without instance name (better to use the machine name and put the ip on DNS or in the hosts file), this means that your sql server don't have a named instance is correct ? For check the named instance see on services.
Other possible problems maybe:
The firewall (on client and on server),
The listening port of sql server.
The user that attempt to connect to sql server (wrong password, disabled, no permissions on db and/or on sql instance)
I had the same problem and for some reason I had to add the standard SQL port (1433) and then everything worked. In your case that would be:
<add name="DefaultConnection"
connectionString="Server=1.1.1.1,1433; Database=XYZ; Uid=user; Pwd=pswd;"
providerName="System.Data.SqlClient" />

Getting SQL Server error in ASP.net application when using different computer

I have a simple ASP.NET C# application which works fine on the computer it was created. However, whenever this project is compiled and run on any other computer, I get the following 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)
On the following line:
bool admin = HttpContext.Current.User.IsInRole("Admin");
I could previously run the application on a different computer without error. I have made no changes to any SQL server settings.
In Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-INT422Project-20140320070224.mdf;Initial Catalog=aspnet-INT422Project-20140320070224;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="DataContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Lab5;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|INT422Project.mdf" />
</connectionStrings>
Your better change your connection String value to use a real sql server database.
SQL Server Express or LocalDB is not used for production web applications because it is not designed to work with IIS.
Please check the following:
Make sure the sql server service is up and running.
Make sure the TCP/IP ports are enabled.
If both are fine, I would suggest you to read the servername and database name from the app.config/ web.config which makes life easier.
Dude, Check this answer.
https://stackoverflow.com/a/16054490/3081461
you can read more about localdb at microsoft TechNet as well :
http://technet.microsoft.com/en-us/library/hh510202.aspx

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found,etc

I'm just publishing my website to my domain name and when I try to access it in my browser I get the Following 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 think the problem is into my connection String ==>
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="TennisOnlineContext" connectionString="Data Source=|DataDirectory|Tennis.sdf" providerName="System.Data.SqlServerCe.4.0 " />
</connectionStrings>
...
</configuration>
Sorry but it's my first web site where I try to publish... So, please what I'm doing wrong ? Thanks in advance...
Have you tried localhost\SQLEXPRESS? Ensure all of your connection keywords abide by those outlined by Microsoft here.
You can check if a connection string is valid, or indeed build a valid connection string that you can copy, by using the SQL/Database Connection utility that ships with VS2010. In VS2010 goto Tools, Connect to database. This will launch the connection dialog; here you can build and test connection strings. For advance connection features, once in the dialog click Advanced. From the advance dialog you can copy and paste connection strings.
Microsoft have also released this dialog to be freely used and distributed in .NET applications. If you interested in this it can be found here.
I hope this helps.
If you are connecting from Windows machine A to Windows machine B (server with SQL Server installed), and are getting this error, you need to do the following:
On machine B:
1.) turn on the Windows service called "SQL Server Browser" and start the service
2.) in the Windows firewall, enable incoming port UDP 1434 (in case SQL Server Management Studio on machine A is connecting or a program on machine A is connecting)
3.) in the Windows firewall, enable incoming port TCP 1433 (in case there is a telnet connection)
4.) in SQL Server Configuration Manager, enable TCP/IP protocol for port 1433
If you look into your release version of web.config you will find <roleManager enabled="false"
Change this configuration setting to True
You can find details here at this link below
http://dotnet1blogger.blogspot.com/2012/04/network-related-or-instance-specific.html
please check once TCP/IP properties in SQL server configuration manager
for more details here
Enable TCP/IP and Named Pipes in SQL Server Configuration Manager.
From SQL Server Configuration Manager, select SQL Server Network Configuration, then select the appropriate instance (if you have multiple). Right click and select enable for Named Pipes and TCP/IP.

Categories