error connect to remote SQL Server Express 2012 windows 10 c# - c#

i have SQL server express 2012 installed on windows 10 64 bit, the connection from same computer worked fine, problem when i connect from another computer on same network,
i get error message:
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 connection
-provider TCP provider error 0 the wait operation timeout
i try the following things but not fix the problem:
Enable and automaticly start SQL Browser
Enbale TCP/IP and set port 1433 for all IP types
Start Named inslance SQLExpress2012 autmaticaly as Build-in : Network service
connect without user and password with IntegratedSecurity=true
The database is using mixed-mode authentication
add an exception in the firewall for port 1433
enable named pipe
allow remote connection
this is my code:
public static bool TestSQLServerConnection(
string ComputerNameOrIPAddress,
string SQLServerInstanceName,
string PortNumber)
{
try
{
SqlConnectionStringBuilder SQLServerConnectionString = new SqlConnectionStringBuilder();
SQLServerConnectionString.DataSource = ComputerNameOrIPAddress+",+"+PortNumber+"\\"+SQLServerInstanceName;
SQLServerConnectionString.InitialCatalog = DatabseName;
SQLServerConnectionString.TrustServerCertificate = true;
SQLServerConnectionString.IntegratedSecurity = true;
//SQLServerConnectionString.UserID = "...";
//SQLServerConnectionString.NetworkLibrary = "DBMSSOCN";
//SQLServerConnectionString.Password = "...";
SqlConnection con = new SqlConnection(SQLServerConnectionString.ConnectionString);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText= "select 1";
con.Open();
cmd.ExecuteScalar();
con.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
when i ping the ip address or computer name of computer of SQL Server it connected and return response, means the computer is connected to network.
i search for while without any solution, please help me.Thanks

I add
SQL Browser Service UDP Port 1434 on firewall and worked fine.
Use SQL Server Management Studio to connect remotely to an SQL Server Express instance hosted on an Azure Virtual Machine

Related

Can't connect with Azure SQL using SQLConnection on Linux

I'm currently developing an ASP.NET Core WebApi that uses an Azure SQL Db for data persistence.
I use Linux Mint 19 as a development environment.
Unfortunately when dbConnection.Open() is called I keep getting SQLException:
Exception has occurred: CLR/System.Data.SqlClient.SqlException
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.SqlClient.dll but was not handled in user code: '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: 40 - Could not open a connection to SQL Server)'
Here's my code:
private const string connectionString = "Server=tcp:{myserver}.database.windows.net,1433;Initial Catalog={mydb};Persist Security Info=False;User ID={user};Password={pass};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "my_server.database.windows.net";
builder.UserID = "my_user";
builder.Password = "my_pass";
builder.InitialCatalog = "my_db";
using(IDbConnection dbConnection = new SqlConnection(builder.ConnectionString)){
dbConnection.Open();
var result = dbConnection.Query<Task>("SELECT * FROM Tasks");
if (dbConnection.State == ConnectionState.Open)
{
dbConnection.Close();
}
}
I've tried both using SQLConnectionStringBuilder and passing connectionString with my credentials. None of it worked.
I've opened port 1433 using the command:
sudo ufw allow 1433
but it didn't help, still getting the same exception.
Any help would be much appreciated.
Thanks.
UPDATE
I managed to connect with Azure SQL. It looks like my ISP blocks connection. I was able to connect using my smartphone WiFi hotspot.
First, make sure you have created appropriate firewall rule to allow traffic from your network to reach Azure SQL Database. Learn here how to do it.
Please try to ping your SQL Azure server. The ping should fail but it should return the current IP Address of the Azure SQL Database logical server. If you cannot see that IP returned then the issue is a DNS issue.
With the IP address returned on the previous step, try to telnet that IP address on port 1433.
telnet 181.37.11.112 1433
If you cannot connect using telnet, use traceroute command to diagnose when locate where data loss occurs.

Cannot open SQL Server database

I downloaded SQL Server 2016 Express and Visual Studio 2015.
I get this message when I try to open SQL Server is my Visual Studio program.
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 code is
// _ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker; User Id = Track;Password =Track;Trusted_Connection=True;";
_ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
SqlConnection myConnection = new SqlConnection(_ConnectionString);
try
{
myConnection.Open();
return "";
}
catch (Exception e)
{
return e.Message + " " + _ConnectionString;
}
I tried my different connection strings. This is one example.
ARKY is the name of my computer and SQLEXPRESS is my named instance.
See the screen shot of my SQL Server database. I will appreciate any suggestions.
Try this:
_ConnectionString = #"Data Source=ARKY\SQLExpress;InitialCatalog=Tracker;Trusted_Connection=True;Connection Timeout=10";
You need a backslash rather than a front slash in your server name, and several parameters are misnamed.
In general, you can open "Server Explorer" in VS, add a data connection to your server there, then click on the data connection and look for the connection string in the properties window. You can cut and paste from there.
Try to use this one
_ConnectionString = "Data Source=ARKY/SQLExpress;Initial Catalog=Tracker;Trusted_Connection=True;Connect Timeout=10";
_ConnectionString = "Server = ARKY\\SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
Check your connection string as well
From FIX : ERROR : Could not open a connection to SQL Server:
Check if your SQL server services is up and running properly:
Go to All Programs > Microsoft SQL Server 2016 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.
In addition, ensure that your remote server is in the same network. Run sqlcmd -L to ascertain if your server is included in your network list.
Enable TCP/IP in SQL Server Configuration
When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2016 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable
You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.

C# Console Application Unable to Connect to Remote SQL Server 2008 Using TCP/IP Protocol

I am currently unable to connect to a remote SQL Server using the TCP/IP protocol in C#.
Using named pipes does work, but the TCP/IP connection doesn't. The frustrating thing is that I can connect to the database using the TCP/IP protocol in the server explorer window within Visual Studio 2015, but my code cannot, even though I am copying and pasting the exact connection strings.
As soon I a remove "Network Library = dbmssocn" from the connection strings, the code works, but this makes it use named pipes, which I cannot rely on being an an option outside of the development environment.
Just in case, I have verified that the TCP/IP protocol is enabled on the SQL Server. This makes sense, since I can specify the TCP/IP protocol in visual studio's server explorer window and those connections work.
The server is also configured for both SQL authentication and Windows authentication. The windows firewall is probably also not the issue as it is currently turned off.
Here is the code that I'm using:
DbConnection testConnectionIntegrated = new SqlConnection("Data Source = MyServer; Initial Catalog = MyDatabase; Integrated Security = True; Encrypt = True; TrustServerCertificate = True; Network Library = dbmssocn");
try
{
Console.Write("Attempting to connect using Windows Authentication...");
testConnectionIntegrated.Open();
Console.WriteLine(testConnectionIntegrated.State);
}
catch (Exception caughtException)
{
Console.WriteLine("Integrated Security Connection Failed");
Console.WriteLine(caughtException.Message);
}
finally
{
Console.Write("Closing connection...");
testConnectionIntegrated.Close();
Console.WriteLine(testConnectionIntegrated.State);
}
DbConnection testConnectionSQLAuth = new SqlConnection("Data Source = MyServer; Initial Catalog = MyDatabase; Persist Security Info = True; User ID = my_user_name; Password = my_password; Encrypt = True; TrustServerCertificate = True; Network Library = dbmssocn");
try
{
Console.Write("Attempting to connect using SQL Authentication...");
testConnectionSQLAuth.Open();
Console.WriteLine(testConnectionSQLAuth.State);
}
catch (Exception caughtException)
{
Console.WriteLine("SQL Authentication Connection Failed");
Console.WriteLine(caughtException.Message);
}
finally
{
Console.Write("Closing connection...");
testConnectionSQLAuth.Close();
Console.WriteLine(testConnectionIntegrated.State);
}
And this is the error message I'm seeing for both connection attempts:
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 - A non-recoverable error occurred during a database lookup.)
Is there anything that I'm missing?
Edit:
I have verified the following on the server:
Remote connections are allowed.
The TCP/IP protocol is enabled and configured.
The named pipes protocol is enabled and configured.
Firewall is not the problem.
The SQL Server and SQL Server Browser services are running.
I have restarted the services to make sure.
Also, I have restarted my development machine, and the problem still exists.

Connecting to SQL database over Windows Remote Desktop: The Login is from an untrusted domain and cannot be used with WIndows Authentication

I am connecting to an SQL database thus:
using (sqlConnection connection = new SqlConnection(#"Data Source = a.b.c.d;Initial Catalog=mydb;Integrated Security=true"))
{
connection.Open();
The server pointed to by a.b.c.d is a remote server.
When I run this code from my laptop it works just fine - the connection is made.
If, however, I connect to server a.b.c.d via Windows Remote Desktop using my Windows login, and then run the same code locally on a.b.c.d I get an exception thrown from
connection.Open();
The exception is
Login failed. The login is from an untrusted domain and cannot be used with Windows Authentication.
I don't get it - aren't I logging in using the same credentials? Why the exception?
SqlConnection con = new SqlConnection("Server= server ip ;Database=Db.name;User Id= ur_id;Password=ur_ps;Trusted_Connection=false");
Server ip should be your remote desktop's LAN ip or WAN ip.

ADO.NET: Can't connect to mdf database file

I'm writing an application that uses a SQL Server 2005 database. In the connection string I'm specifying the mdf file like this:
connstr = #"Data Source=.\SQLEXPRESS; AttachDbFilename=" + fileLocation + "; Integrated Security=True; User Instance=True";
When I execute the code:
public static void forceConnection()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if(conn != null)
conn.Close();
}
}
I receive an 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)
This code works on XP but not in Vista. I tried to run Visual Studio in admin mode and moved the mdf file to "user data" folders but the error persists.
Any help?
Can you connect to the sql server db in your command prompt? I would make sure you can actually connect first.
Try open the cmd prompt and type sqlcmd -S .\SQLEXPRESS -d your_dbase
If I have mssql-connect problems with my dotnet-sourcecode I try to connect to the database with a different program. I use queryexpress that is also written in dotnet. If this program works then I know that the problem is with my program code else the problem is with connection string, proxy, network, sqlserver or user permissions.
Do you actually have sqlexpress installed? does it use the machineName\sqlexpress or does it run as a default instance?
You have to verify these cases.
and you might wanna use the actual machineName\instance name if you aren't using the default instance.

Categories