日期:2014-05-18  浏览次数:21004 次

C#怎么查MYSQL的数据?
MySqlConnection myConnection = new MySqlConnection("server=localhost;user id=root;password=;database=account");
  string query = "select * from account where Name = @Name";
  MySqlCommand myCommand = new MySqlCommand(query, myConnection);
  myCommand.Parameters.Add("@Name",MySqlDbType.VarChar,32);
  myCommand.Parameters["@Name"].Value =this.textBox1.Text;
  myConnection.Open();
  myCommand.ExecuteNonQuery();
  MySqlDataReader myDataReader = myCommand.ExecuteReader();
  string bookres = "";
  while (myDataReader.Read() == true)
  {
  bookres += myDataReader["id"];
  }
  this.label1.Text = bookres;
  myDataReader.Close();
  myConnection.Close();

好象查不出来.`...


------解决方案--------------------
使用?号做为参数占位符而不是@:

如:
C# code


            MySqlConnection myConnection = new MySqlConnection();
            myConnection.ConnectionString = "Persist Security Info=False;database=dbtest;server=localhost;Connect Timeout=30;user id=root; pwd=mysql";
            myConnection.Open();
            MySqlCommand cmd = new MySqlCommand("select * from table1 where id=?id", myConnection);
            cmd.Parameters.Add("?id", MySqlDbType.Int32);
            cmd.Parameters[0].Value = 1;
            object obj = cmd.ExecuteScalar();