0

Ok So Basically I have made MySQL table on PhpMyAdmin. It's on local host, username root and no password.

I'm working on a Windows Application based on c# on visual express 2008. I have the following code for a button to save/load data from the MySQL(I have followed some links/tuts to get to this point, but dunno how this can theoritically connect to the MySQL @ phpmyadmin, I mean Dont I need a file to download from PhpmyAdmin database and reference or add it as a plugin to the script or something? Tottaly lost here..):

        String connString = "SERVER = localhost; DATABASE = request; User ID = root; ID =; UserName =; Date =; Type =; Rules =;"; 
        MySqlConnection mcon = new MySqlConnection(connString);
        String command = "SELECT * FROM requesttcw";
        MySqlCommand cmd = new MySqlCommand(command, mcon);
        MySqlDataReader reader;

        try
        {
            mcon.Open();
            cmd.ExecuteNonQuery();
            reader = cmd.ExecuteReader();
            cmd.CommandType = System.Data.CommandType.Text;
            while (reader.Read() != false)
            {
                Console.WriteLine(reader["ID"]);
                Console.WriteLine(reader["ClanName"]);
                Console.WriteLine(reader["Date"]);
                Console.WriteLine(reader["Type"]);
                Console.WriteLine(reader["Rules"]);

            }

            Console.ReadLine(); 

        }
        catch (Exception)
        {
            MessageBox.Show("ERROR: There was an error trying to connect to the DB!");
            return;
        }
        cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "' LIMIT 1)";

        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("You're Request Has Been Posted!");
        }
        catch (Exception ex)
        {
            string message = ("ERROR: There was an error submitting your form!" + ex + "");
            DialogResult result = MessageBox.Show(message, "ERROR", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);

            switch (result)
            {
                case DialogResult.Retry:
                    Application.Restart();
                    break;
                case DialogResult.Cancel:
                    this.Close();
                    break;
            }
        }

When I run it, enter my data, and click the button, It gives me this error on line(MySqlConnection mcon = new MySqlConnection(connString); * Keyword not supported. Parameter name: id *

Please tell me how to fully connect this to the MySQL.. I have also downloaded the MySQL Connector and referenced the mysql.data.dll file. So that part is done too...

3 Answers 3

3

Try the following connection string:

string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";

Also try cleaning your code and ensure that you are properly disposing IDispoable resources such as SQL connections and commands by wrapping them in using statements. Also make sure that you are using prepared statements or your code is vulnerable to SQL injection attacks and your database could be ruined very quickly (in a blink of an eye):

string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";

using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
    mcon.Open();
    cmd.CommandText = "SELECT * FROM requesttcw";
    using (MySqlDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader["ID"]);
            Console.WriteLine(reader["ClanName"]);
            Console.WriteLine(reader["Date"]);
            Console.WriteLine(reader["Type"]);
            Console.WriteLine(reader["Rules"]);
        }
    }
}

using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
    mcon.Open();
    cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES (@ClanName, @Date, @Type, @Rules)";
    cmd.Parameters.AddWithValue("@ClanName", textBox1.Text);

    // Warning if the Date column in your database is of type DateTime
    // you need to parse the value first, like this:
    // cmd.Parameters.AddWithValue("@Date", Date.Parse(textBox2.Text));
    cmd.Parameters.AddWithValue("@Date", textBox2.Text);
    cmd.Parameters.AddWithValue("@Type", textBox3.Text);
    cmd.Parameters.AddWithValue("@Rules", richTextBox1.Text);

    cmd.ExecuteNonQuery();
}

As far as exception handling is concerned, I have omitted it in my example but you could obviously wrap the using statements in try/catch blocks.

Sign up to request clarification or add additional context in comments.

4 Comments

Darin Dimitrov thanks man! That fixed it all!!!!! I spent like a day trying to figure this out but you simply helped me out of it!! :D
@DamageDz, great, I am glad I could help. If this post was helpful to you, please consider marking it as answer by clicking on the tick next to it.
@vucetica, ADO.NET manages a connection pool. So the second using block doesn't open a new connection to the database. It just draws an existing connection from the connection pool. At te end of the using statement the connection is not closed, but it is returned to the connection pool in order to be reused. As far as your second remark is concerned, I prefer to separate the concerns in my application. Those two would normally go into 2 completely separate methods that can be later reused.
Pooling will work, and you can create that new command, but there is really no need to have that additional code, except if that is your preference.
0

In your connString variable you have ID =; after User ID = root.

Comments

0

Your connectionstring specifies the connection between your application and the MySQL server. The error you get is, because you've made a mistake in your connectionstring. I think that the following will suit your needs better:

Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.