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());
    }
}
}
Marcus
  • 441
  • 3
  • 15
  • 27
  • I'd suggest looking at the `using Statement` and `try/catch/finally` when working with something such as DB connections. – myermian Nov 20 '10 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. – Ash Burlaczenko Nov 20 '10 at 13:33
  • @Ash, you're probably right. Too many strange questions today I guess. – Frédéric Hamidi Nov 20 '10 at 13:35
  • Also, using `root` to do DB transactions is a big no no... – myermian Nov 20 '10 at 13:38
  • Asked the same question here: http://stackoverflow.com/questions/4233185/connection-must-be-valid-and-open-error – Fernando Nov 20 '10 at 14:52

2 Answers2

2

You need to execute the command:

myCommand.ExecuteNonQuery();
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
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);
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900