Arthur Perin
Arthur Perin

Reputation: 15

How can I make my DB connection works?

I set up a MySQL database in my AZURE account, but I cannot connect it in any way. I tried:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection sqlconnection = new SqlConnection("Database=Banco1; Data Source=br-cdbr-azure-south-a.cloudapp.net;User Id=USERID Password=PASSWORDid");
            Console.WriteLine("1");
            sqlconnection.Open();
            Console.WriteLine("2");
            SqlCommand cmd = new SqlCommand("CREATE TABLE Customer(First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date datetime); ", sqlconnection);
            Console.WriteLine("3");
            cmd.ExecuteNonQuery();
            Console.WriteLine("4");
            Console.ReadLine();
        }
    }
}

I tried putting prints through the commands, and the program only prints "1" wich means I can't get the connection opened... how can I fix that?

The output error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Error in the network or specifics to the instance when stablishing conexion to the SQL Server. The server was not found or was not avaible. Check if the instance name is correct and if the SQL Server is configured to allow remote conexions. (provider: Named Pipes Provider, error: 40 - Could not stablish connection to SQL Server)

Upvotes: 0

Views: 66

Answers (1)

forester123
forester123

Reputation: 1579

SqlConnection is used for connecting SQL Database, for MySQL Database, you need to use MySqlConnection. Please first install MySql Connect/NET and then add reference enter image description here

Now you can connect to your Mysql database successfully with the code below:

MySqlConnection con = new MySqlConnection("your_mysql-connect_string");

Upvotes: 1

Related Questions