Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to External SQL Database in C#

Tags:

c#

.net

sql

mysql

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?

like image 421
AllFallD0wn Avatar asked Oct 11 '12 12:10

AllFallD0wn


1 Answers

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

like image 197
Darren Avatar answered Sep 29 '22 10:09

Darren