3

I've read many ODBC tutorials for C# on the net, and this code is the only one that didn't give me error. But the problem is, it doesn't do anything -.- How to fix ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.Sql;
namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
        string connectionString = "Server=localhost;User       ID=root;Password=****;Database=testing;Port=3306;Pooling=false";
        MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

        connection.Open();

        string insertQuery = "ALTER TABLE `user` ADD lol INT (15)";
        MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery);
        connection.Close();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}
5
  • I'd suggest looking at the using Statement and try/catch/finally when working with something such as DB connections. Commented Nov 20, 2010 at 13:32
  • @Frederic - Thats not very helpful. What he meant to say was you can either use VARCHAR(15) or INT. Not INT(15). I don't think thats your problem though because that would throw an exception. The problem must be before that. Have you tried stepping through your code in debug mode. Commented Nov 20, 2010 at 13:33
  • @Ash, you're probably right. Too many strange questions today I guess. Commented Nov 20, 2010 at 13:35
  • Also, using root to do DB transactions is a big no no... Commented Nov 20, 2010 at 13:38
  • Asked the same question here: stackoverflow.com/questions/4233185/… Commented Nov 20, 2010 at 14:52

2 Answers 2

2

You need to execute the command:

myCommand.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

3 Comments

Why would you want it to run more than once?
"Connection must be valid and open" it says now
You need to pass along the connection to the Command object. Either by constructor, or property.
2

Allow me to tidy; important points:

  • using to ensure proper disposal
  • setting the command's Connection
  • executing the command with ExecuteNonQuery()

Code:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string connectionString = "Server=localhost;User ID=root;Password=****;Database=testing;Port=3306;Pooling=false";
string insertQuery = "ALTER TABLE `user` ADD lol INT (15)";
using(MySql.Data.MySqlClient.MySqlConnection connection =
    new MySql.Data.MySqlClient.MySqlConnection(connectionString))
using(MySql.Data.MySqlClient.MySqlCommand myCommand =
    new MySql.Data.MySqlClient.MySqlCommand(insertQuery))
{
    myCommand.Connection = connection;
    connection.Open();
    myCommand.ExecuteNonQuery();
    connection.Close();
}
using(Form1 form1 = new Form1()) {
    Application.Run(form1);
}

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.