ASP.NET Connection string error when using ip address - c#

I'm trying to debug an ASP.NET page I have trying to connect to a local SQLServer DB, when the connection string I'm using has the name of the local machine, it works fine like in below
string ConnectionString = "Integrated Security=SSPI;" +
"Initial Catalog=ARTICLEDB;" +
"Data Source=MYMACHINENAME\\MYSQLSVR;";
... later on...
conn = new SqlConnection(ConnectionString);
// Open the connection
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
//don't work here when using ##.##.##.##\MYSQLSVR as datasource
}
return true;
}
catch
{
return false;
}
when I use the IP machine as the value for the datadource such as: ###.###.###.###\\\MYSQLSVR the I get an exception saying:
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: 28 - Server doesn't support requested
protocol)
This problem originated from trying to run the site from IIS and getting a similar error where the connection was not opened.
How can I reference the DB in the connection string so that IIS can find the DB?
I've also tried adding the connection string in IIS for the site but not sure how to reference that IIS connection string from my project/site

go to SQL Server Configuration Manager and turn on "Named Pipes" and "TCP/IP"
and your connection string should look like below
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
http://www.connectionstrings.com/sql-server-2008

Check the following it is working for me.
Data Source=190.190.200.100,1433;Initial Catalog=DBName;Integrated Security=False;user id=userid;password=password

Related

Unable to connect to MySql Server in Visual C#

I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error saying
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)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/

How to connect to SQL Server database in Godaddy from a winform application using connection string

Have a Winforms application and want to connect to a SQL Server database in godaddy using connection string.
I am getting the following error - please help.
connString = #"Data Source=IP\SERVER2014;Initial Catalog= ;User ID= ;Password= ";
SqlConnection conn = new SqlConnection(connString);
Getting same error when open from SQL Server Management Studio (local computer)
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)
Connection string was the problem.
Data Source=IP\SERVER2014 instead used Data Source=IP.
connString = #"Data Source=IP;Initial Catalog= ;User ID= ;Password= ";
Thanks.

Remote DB connection string failed

I need to connect the db on another system. while i tried the connection string as
SqlConnection con = new SqlConnection(#"Data Source=(192.168.0.125)\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=db_Stock;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
error shown:
"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 - No such host is known.)"
You don't need the brackets
Data Source=192.168.0.125\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=db_Stock;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
ConnectionStrings
You need to create a database user for remote connection, and pass them in your web.config file.
Also you need to set Integrated security=false.
According to [Microsoft][1]
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
Example: (192.168.0.125)\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=db_Stock;Integrated Security=True;user id=sa;password=sa123;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
i fixed the issue by following the below site and instructions
link to the site]site

SQL server DB connection from .net 2010

I am trying to connect to SQL server running on a VM Image with the C# program from my laptop.
my connection string look like the following
conn.ConnectionString =
#"Data Source=192.168.22.182;" +
"Initial Catalog=master;" +
"User id=sa;" +
"Password=livelink;"+
"Connection Timeout=30";
conn.Open();
i always got 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I also tried using the name of the VM machine, but still got the same error.
Please help me in resolving this problem.
First check these:
Make sure that you are reaching that ip from your local machine.
Make sure that your VM is not blocking SQL Server's port which is 1433 on default.
Try this connection string:
conn.ConnectionString =
#"Data Source=192.168.22.182,1433;" +
"Initial Catalog=master;" +
"User id=sa;" +
"Password=livelink;"+
"Connection Timeout=30";
conn.Open();
"," value is the port value for SQL Server.

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