2

I want to connect to database from my c# windows forms application. I tried using

using(SqlConnection conn = new SqlConnection()) {
 conn.ConnectionString = "Data Source=localhost; User Id=root; Password=; Initial Catalog=dbName";
 conn.Open();
}

and when I build my project I get an error that server wasn't found or wasn't accessible.

I tried connecting through data source configuration wizard, but also database can't be found.

I use WAMP server, and I can find my database through phpMyAdmin.

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
wdc
  • 2,623
  • 1
  • 28
  • 41
  • what error occurred? – Sachith Wickramaarachchi Apr 11 '17 at 09:46
  • @SachithMW It's a runtime error and it says: `Unhandled exception has occured in your application ... ... A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or was not accessible. ...` – wdc Apr 11 '17 at 09:51

2 Answers2

2

You're trying to connect to Microsoft SQL Server.

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

public class Tutorial1
{
    public static void Main()
    {
        string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            // Perform database operations
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
        Console.WriteLine("Done.");
    }
}

Check this links,hope this will be helped to you

you can install MySql.Data by ,open Package Manager Console Tools > NuGet Package Manager > Package Manager Console then type Install-Package MySql.Data -Version 6.9.9 in there

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
0

You would need to use a MySqlConnection to connect to a MySQL database, see answer here

How to connect to MySQL Database?

Community
  • 1
  • 1
toby
  • 40
  • 5