Remote DB connection string failed - c#

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

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 VS Express 2012 for Web

I am trying to learn to connect to and perform functions with SQL Server database using C# (out of ASP.NET website). When I try to run
SqlConnection con = new SqlConnection("Data Source=.; database=fengshuidb; integrated security = SSPI");
SqlCommand cmd = new SqlCommand("Select * from emails", con);
con.Open();
to connect I get the following error with the con.Open() instruction.
From VS:
"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 do I need to do to configure connection to database? What security settings do I need? Are there any good resources for this topic?
Try entering the Connection this way
SqlConnection myConnection = new SqlConnection("Database=fengshuidb;Server=[Your_PC_Name]\\SQLEXPRESS;Integrated Security=True;connect timeout = 30");
Where "[Your_PC_Name]" is the name of you local machine if the database is local.
Also take a look at this link:
http://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx
try using SqlConnectionStringBuilder to create a valid Connection String
You missed Initial Catalog in your connection part.
Change it to this:
Data Source=Your sql connection ;Initial Catalog=Your Database name;Integrated Security=True

ASP.NET Connection string error when using ip address

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

SQL Server Connection using GPConnNet.dll for non sa user In microsoft dynamic gp

I saw this link non sa user sql connection string.
This is giving datasource name and userid and passworkd and database name. But I have doubt about datasource name :Dynamic GP 2010. I am bit confused using this connection string:
string connection = "data source=Dynamic GP 2010;initial catalog=TWO;integrated security=False;User ID=client;Password=123";
This is giving 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)
Let me know which datasource I have to use. Is GP datasource name (or) sql datasource name.
Econnect must use a trusted connection. You cannot pass a username and password into the connection string. To add a user that does not have sa access, add the domain user to sql server with the appropriate permissions. You will also need to configure the econnect setup to allow the user to access sql through econnect. Secondly, if you are setting this up on a SQL server that has not been setup to allow incoming connections via non-windows connection, this will need to be set up as well. This may be due to not having mixed auth mode on. See this article to try and fix
string connectionString = #"Data Source=localhost; Integrated Security=SSPI;Persist Security Info=False; Initial Catalog=TWO;";

ADO.NET: Open SQL Connection

i want to do some sql code run in my webservice in c#
the code is just
[WebMethod]
public void GetCustomers()
{
SqlConnection MyConn = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/WebService2/WebService2/App_Data/Database1.mdf;Integrated Security=True;User Instance=True");
MyConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = MyConn;
cmd.CommandText = "delete from t1 where name='1'"; //just see ["+month
// [mon+"] it's imp
cmd.ExecuteNonQuery();
}
now i get error like
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)
Correct Format:Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Try:
"Data Source=./SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WebService2\WebService2\App_Data\Database1.mdf; Database=YourDatabasName;Integrated Security=True;User Instance=True"
For more details see:
http://connectionstrings.com/
You might want to check the configuration of your SQL Server as well.
The error remarks the issue could be the named pipes not being active, this is the default setting.
You can always test this fix by going;
Start -> SQL Server xxxx -> Configuration Tools -> SQL Server
Configuration Manager
Where xxxx is the version of SQL Server you are using.
In the tree look at the
-> SQL Native Client xx Configuration -> Client Protocols
Named Pipes is listed.
Set it to being Enabled
Then under
SQL Server Network Configuration -> Protocols for xxxxxx
xxxxxx Being the name of your SQL Server database instance name.
Check that Named Pipes are Enabled there also.
You will need to restart your SQL Server database in order for it to accept Named Pipe calls.
Might be worth a try.

Categories