AllFallD0wn
AllFallD0wn

Reputation: 551

Connect to External SQL Database in C#

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?

Upvotes: 6

Views: 4507

Answers (2)

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

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

Upvotes: 3

Darren
Darren

Reputation: 70728

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

Upvotes: 5

Related Questions