SQL server DB connection from .net 2010 - c#

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.

Related

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.

C# SqlConnection stop at method: Open()

String connectionString = "server=127.0.0.1:3306;Database=joonggonara;Uid=root;Pwd=apmsetup;";
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
int a = sqlComm.ExecuteNonQuery();
MessageBox.Show(a.ToString());
I execute in debug mode.
I got an 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)
My SQL server version is 5.1.41-community MySQL Community Server
I want to solve this problem
You can't use the SqlConnection class to connect to MySQL Server; it can only connect to Microsoft SQL Server.
Instead, install MySql.Data or MySqlConnector and use the MySqlConnection class.
Additionally, you can't combine the host IP and port into one connection string option. Since the default port for MySQL is 3306, change your connection string to "server=127.0.0.1;Database=joonggonara;Uid=root;Pwd=apmsetup".

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

Can't access MYSQL server from c# forms application - error in connection string

myConnection = new SqlConnection("user id=champion3_test;" +
"password=test;server=10.168.1.58;" +
"Trusted_Connection=true;" +
"database=champion3_sabdb; " +
"connection timeout=30");
This code gives the error:
Thrown: "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)" (System.Data.SqlClient.SqlException) Exception 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 connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)", Exception Type = "System.Data.SqlClient.SqlException", Exception WinRT Data = ""
Here are some screenshots that might help with debugging of my server's settings:
http://imgur.com/a/PeRby
I know that 'champion3_test' is a valid user id
'test' is a valid password for champion3_test
the database name is 'champion3_sabdb'
SQL server isn't MySQL. If you are using MySQL, download the MySQL .NET Connector and use their connection class, MySqlConnection.
To learn how to use the MySQL Connector, you can start here.
You can download the MySql Connector for .Net here.
Once you've installed the driver, you can connect using the MySql Connector classes:
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);

Categories