0

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.

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by G_Waddell
0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.