Hi There;

I am learning how to establish a connection to mysql in c#. Here are my codes :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class _06_web_form_elemanlari_08_button : System.Web.UI.Page
{
    protected void connect(Object Sender, CommandEventArgs arguman)
    {
        try
        {

            MySqlConnection connection; // bağlantı nesmenizi tanımladık

            string connString;

            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); //" MySqlConnectionStringBuilder" bize bağlantı kurmamızı kolaylaştıran yapı

            builder.UserID = "root";

            builder.Password = "123456";

            builder.Database = "connectcsharptomysql";

            builder.Server = "localhost";

            connString = builder.ToString();

            connection = new MySqlConnection(connString);

            connection.Open();

            Response.Write("Connection is successfull.");

            connection.Close();

        }

        catch (Exception ex)
        {

            Response.Write("Bağlantı başarılı DEĞİL.");
            Response.Write(ex.Message);


        }
    }
}


When I run this code : I catched this error: "Keyword not supported. Parameter name: MySql.Data.MySqlClient.MySqlConnectionStringBuilder"

Is there any ideas to solve this problem? Thanks in advance.

After some investigation, the corrected version is this :

protected void connect(Object Sender, CommandEventArgs arguman) 
{
    String connString = "Server=localhost;Database=connectcsharptomysql;Uid=root;password=123456;";
    MySqlConnection conn = new MySqlConnection(connString);
    MySqlCommand command = conn.CreateCommand();
    command.CommandText = "Select * from tableinfo";
    try
    {
        conn.Open();
    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
    }

    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(reader["text"].ToString());

    }
    Console.ReadLine();
}

This is the corrected version. But the String, connString is faulty. What could be the corrected version, do you have any suggestions?

I normally use pwd in place of password but I don't know for sure if using password causes MySql to throw an error. But that is the only thing I can see wrong with your connection string - unless your database name is wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.