Problems with SQL Server connection in console app - c#

The SQL server is located on a remote machine.
I can connect to it with MVC app.
I can connect to it with SSMS.
But I can not do it in console app.
Neither entity nor SqlConnection is working.
Connection string is the same as in console and mvc app (which works). What can it be? Any ideas?
Error I am getting
System.Data.SqlClient.SqlException: '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)'
What can it be?
<add name="DBContext" connectionString="data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient";
cn.Open();
Console.WriteLine(cn.State.ToString());
The problem is that console app uses 445 and 139 port. In terms of security. But why MVC app doesn't use it. And how I can I use it without opening this port. Even dividing db part in different library didn't work.

Related

C# WebSite connection to a mySQL database

I want to connect my WebSite to a local database.
My connection String is the following:
<connectionStrings>
<add name ="localhost"
connectionString="server=localhost;user id=root;persistsecurityinfo=True;database=doctorappointments"
providerName="MySql.Data.MySqlClient"
/>
I can connect from MySql Workbench and also from Visual Studio if I add a new connection in Server Explorer. The connection String from above is the same as the one from server Explorer.
I checked that my server is running, I also put a firewall rule for the port that it connects to.
This how I am using the connectionString:
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
{
con.Open();
Response.Redirect("MainPage.aspx");
}
And on the con.Open() it gives me this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in >System.Data.dll but was not handled in user code
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)
and also:
ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
I am new to this and I have tried everything I found online. I do not know what am I doing wrong...
SqlConnection is for Sql Server.
Make sure you have installed MySql.Data via nuget package and use MySqlConnection instead.

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.

sql connection over VPN in WPF program

I'm trying to connect to a remote server via VPN, and I have connected to the VPN successfully, and when I'm using management studio, it's also all good and well, but when I'm trying to connect via my WPF program I'm getting this error
On connection.Open();
{"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: TCP Provider, error: 0 - The remote computer refused the network connection.)"}
and this is my Connection string as copied from visual studio
Data Source=192.168.232.10\PRI2014,4443;Integrated Security=True;User ID=c_Vision;Password=********;Connect Timeout=15; Encrypt=False; TrustServerCertificate=True; Trusted_Connection=True;
i have looked here and here for answers
Port number should be placed after Ip
Data Source=192.168.232.10,4443\PRI2014;Integrated Security=True;User ID=c_Vision;Password=********;Connect Timeout=15; Encrypt=False; TrustServerCertificate=True; Trusted_Connection=True;

SQLException cannot connect in ASP.NET MVC [duplicate]

This question already has answers here:
Cannot Connect to Server - A network-related or instance-specific error
(54 answers)
Why did a network-related or instance-specific error occur while establishing a connection to SQL Server?
(15 answers)
A network-related or instance-specific error occurred while establishing a connection to SQL Server [closed]
(1 answer)
update-database: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"
(5 answers)
Closed 5 years ago.
I have moved a project that used to work fine but now it cannot acces my database anymore. I use Entity Framework and have connected to the database in SQL Server 2012 successfully. Here is what my connection string looks like :
<connectionStrings>
<add name="PrincipalServerContext"
connectionString="Data Source=ServerName\SQLEXPRESS;Initial Catalog=PrincipalServerDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
When I launch the website, after authentication I retrieve data, but it throws the exception
System.Data.SqlClient.SqlException
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)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
I believe it might have to do with several things but am not sure, and don't know how to fix them :
User permissions in SQL Server
Connection strings could be wrong but I checked and re checked a hundred times
Could not make it work like this, and the exception is throw on Linq queries to access DB.
EDIT I did try to publish the web site to test, and have a second connection string :
<connectionStrings>
<add name="PrincipalServerContext_DatabasePublish"
connectionString="Data Source=ServerName\SQLEXPRESS;Initial Catalog=PrincipalServerDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Though I'm just trying to debug on the new computer I set up the project, if I remove this line, I have an exception. So maybe my program is searching for this DB and finds nothing?
System.InvalidOperationException cannot find PrincipalServerContext_DatabasePublish connection string
(I translate from french)
It likely isn't related to user permission. Rather, the new server you moved the project to cannot access the server SQL Server is located on.
If the new server has telnet, try this in Command Prompt on the Application Server:
telnet serverName 1433
it'll likely hang there trying to connect. Few things you could do:
Make sure there is no firewall blockage between the two servers if they are on different zones.
Check if SQL Server's Server has an inbound rule open for port 1433 (SQL Server Express doesn't open this port by default, but Enterprise and Standard do).
Check to see SQL Server is accepting TCP IP and Named Pipe connections through Sql Server Configuration Manager.

Database Connection: Web.config Asp.net

What's wrong with my connection string?
I copied my files from one computer to another. Then I altered my connection string because I moved my files from another computer, it is not connecting to database. When I tried connecting to SQL Management Studio, it successfully connected to my server and when I try running my codes, it didn't.
Here's my Connection String: (Web.config) - My server is Windows Authentication
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True" providerName="System.Data.SqlClient" />
It gives me 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)
Is there any conflict if my database is from SQL Server Authentication then I moved it to Windows Authentication?
Thank you.
I believe you need this Integrated Security = true into your connection string and it should work. If you don't have this in your connection string you need to specify password and username.
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True;Integrated Security=True" providerName="System.Data.SqlClient" />

Categories