2010-11-20 44 views
5

エラーが発生しました:このプログラムを起動すると、接続が有効で開いている必要があります。 Googleで検索したところ、同じエラーが発生した2人しか見つかりませんでした。これを修正することは可能ですか? Thx接続は有効でオープンエラーである必要があります

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;Uid=root;Pwd=******;Database=testing;"; 
     MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString); 

     connection.Open(); 

     string insertQuery = "ALTER TABLE `user` ADD lol INT"; 
     MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery); 
     myCommand.ExecuteNonQuery(); 
     connection.Close(); 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 
} 
} 
+0

なぜodbcタグを配置しましたか?この質問はODBCとは関係ありません。 –

+2

1時間前に同じ質問をしました:http://stackoverflow.com/questions/4232943/database-query-c-not-working – Fernando

答えて

11

接続オブジェクトについてのコマンドを伝える必要があります。次のようにコマンドのコンストラクタに接続を渡してください。 接続パラメータを次の行に追加します。

MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery, connection); 
4

コマンドには接続割り当てがありません。

MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery, connection); 
関連する問題