6

I've got an SQL server and database setup on an external server (let's call the domain name "hello.com" for the purposes of this), and I want to connect to this server via a C# program. So far I have this (All server/database details are different to the real ones):

private static void SetupSQL()
{
    string connectionString = "server=hello.com; database=db1; uid=user1; pwd=xxxxx;";
    connection = new SqlConnection();
    connection.ConnectionString = connectionString;
    try
    {
        connection.Open();
        Console.WriteLine("Connected");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
}

This is giving me an 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 connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I have checked all the connection string, and I am allowed remote access, as I have SQLWorkbench open querying the database right now on the same computer.

Any ideas?

6
  • 1
    Why is this tagged as mysql if the database is sql server? Commented Oct 11, 2012 at 12:19
  • check your sqlInstance with the name in connectionString Commented Oct 11, 2012 at 12:19
  • It's a MySQL Server. Sorry if I didn't make that clear. Commented Oct 11, 2012 at 12:20
  • Have you read this: blog.sqlauthority.com/2009/05/21/… ? Edit: No, it's not clear since a SqlConnection is clearly a SQL-Server connection. Commented Oct 11, 2012 at 12:24
  • does SQL SERVER Status is Running? Read This Article Here if you are using MySQL you should Install MySQL Connector. Commented Oct 11, 2012 at 12:24

2 Answers 2

5

You'll need the MySQL driver:

http://dev.mysql.com/downloads/connector/net/

You can then use the the MySqlConnection connection class to connect.

MySqlConnection connection = new MySqlConnection(connectionString);

http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

Sign up to request clarification or add additional context in comments.

Comments

3

You can't use SqlConnection object to connect to MySQL database, you should use MySqlConnection instead after you import its dll

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.