首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#如何查MYSQL的数据

2012-01-13 
C#怎么查MYSQL的数据?MySqlConnection myConnection new MySqlConnection(serverlocalhostuser idro

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(); 

热点排行