0

im trying to connect to my xampp mysql server via c# webapi. Perhaps its something with my connection string, but i tried different variations of it(i used https://www.connectionstrings.com/mysql/). I checked my uid and pwd for sql using SELECT * FROM mysql.user, also database tennisapi exists in phpmyadmin.

string connStr = "datasource=localhost;port=3306;username=root;password=abcdef;database=tennisapi;";

MySqlConnection conn= new MySqlConnection(connStr);

{MySql.Data.MySqlClient.MySqlConnection}

CanRaiseEvents: true
ConnectionString: "server=127.0.0.1;port=3306;user id=root;password=abcdef;database=tennisapi"
ConnectionTimeout: 15
Container: null
DataSource: "127.0.0.1"
Database: "tennisapi"
DbProviderFactory: {MySql.Data.MySqlClient.MySqlClientFactory}
DesignMode: false
Events: {System.ComponentModel.EventHandlerList}
IsPasswordExpired: 'conn.IsPasswordExpired' threw an exception of type 'System.NullReferenceException'
ServerThread: 'conn.ServerThread' threw an exception of type 'System.NullReferenceException'
ServerVersion: 'conn.ServerVersion' threw an exception of type 'System.NullReferenceException'
Site: null
State: Closed
UseCompression: false
TwinGin
  • 31
  • 1
  • This seems to answer it - https://stackoverflow.com/questions/21618015/how-to-connect-to-mysql-database – Ido Shichor Sep 19 '19 at 18:18
  • 4
    Possible duplicate of [How to connect to MySQL Database?](https://stackoverflow.com/questions/21618015/how-to-connect-to-mysql-database) – Kate Orlova Sep 19 '19 at 18:20

1 Answers1

0

You can try it this way. Use the DBConnect class with this connection string. With the method DBConnection you are able to test the Connection.

At first you have to add a Reference in your Project. Right-Click on your projectname -> Add -> Reference... -> Tick MySql.Data -> OK

class DBConnect
{

    private MySqlConnection connection;
    private string datasource;
    private string username;
    private string password;
    private string database;


    //Constructor
    public DBConnect()
    {
        Initialize();
    }

    //Initialize values
    public void Initialize()
    {
        datasource = "127.0.0.1";
        username = "username";
        password = "password";
        database = "database_name";

        string connectionString = "datasource=" + datasource + ";" + "username=" + username + ";" + "password=" + password + ";" + "database=" + database + ";";

        connection = new MySqlConnection(connectionString);
    }

    /// <summary>
    /// Test Connection to the Server
    /// </summary>
    private void DBConnection()
    {
        //string ConnectionString = "datasource = localhost; username = root; password = ; database = test ";

        MySqlConnection DBConnect = new MySqlConnection(ConnectionString);

        try
        {
            DBConnect.Open();
            System.Windows.Forms.MessageBox.Show("Sucessfully connected!");
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
        }
    }
}

In your Btn_Click event you can try this.

    private void Btn_Test_Click(object sender, RoutedEventArgs e)
    {
        //Create an instance of DBConnectTest
        DBConnectc DBConnect = new DBConnect();

        DBConnect.DBConnection();
    }
Floo33R
  • 23
  • 6