2012-02-12 16 views
0

Okだから、基本的に私はPhpMyAdmin上にMySQLテーブルを作成しました。ローカルホスト、ユーザー名root、パスワードなし。Visual C#Express 2008とのMySQL接続

visual express 2008のC#に基づいてWindowsアプリケーションを開発しています。私は、以下のコードを使ってMySQLからデータを保存/読み込みしています(この時点までにリンクしていますが、 phpmyadminデータベースにダウンロードして、スクリプトや何かにプラグインとして追加するファイルが必要なのはなぜですか?

 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; 
      } 
     } 

私はそれを実行すると、私のデータを入力し、ボタンをクリックすると、私にこのエラーが表示されます(MySqlConnection mcon = new MySqlConnection(connString); * キーワードはサポートされていません。 パラメータ名:id *

これを完全にMySQLに接続する方法を教えてください。また、MySQL Connectorをダウンロードし、mysql.data.dllファイルを参照しました。だから、その部分があまりにも行われている...

答えて

3

は、次の接続文字列を試してみてください。

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

また、あなたのコードを掃除してみてください、あなたは適切でそれらをラップすることによって、このようなSQL接続やコマンドなどIDispoableリソースを配置していることを確認しますusingステートメント。また、あなたがプリペアドステートメントを使用しているか、(目の点滅で)あなたのコードは、SQLインジェクション攻撃に対して脆弱であり、あなたのデータベースは非常に迅速に台無しにすることができることを確認してください:

例外処理が関係している限り
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(); 
} 

、私の例では省略しましたが、明らかにtry/catchブロックにusingステートメントをラップすることができます。

+0

ダーリン・ディミトロフそれはすべてそれを固定!!!!!私はこれを理解しようとする一日のように過ごしましたが、あなたは単にそれを手伝ってくれました! :D – DamageDz

+0

@DamageDz、偉大な、私は助けることができてうれしいです。この投稿が役に立ちましたら、[回答としてマークする](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を検討してください。それ。 –

+0

@vucetica、ADO.NETは接続プールを管理します。したがって、2番目の 'using'ブロック**はデータベースへの新しい接続**を開けません。 [接続プール](http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connection-pooling.html)から既存の接続を取得するだけです。 usingステートメントの終了時には、接続は閉じられませんが、接続プールに戻されて再利用されます。あなたの2番目の発言に関する限り、私は私のアプリケーションで懸念を分けることを好む。これらの2つは、通常、後で再利用できる2つの完全に別々の方法になります。 –

0

connString変数には、User ID = rootの後にID =;があります。

0

接続文字列は、アプリケーションとMySQLサーバー間の接続を指定します。 接続文字列を間違えたため、エラーが発生します。 私はあなたのニーズに合った以下のものが良いと思います:

Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword; 
関連する問題