解决方法:给字符串name = {0}加单引号,即‘{0}’
//常量查询
public DataTable GetMessage(string name) {
string P_Str_ConnectionStr = string.Format("server={0};user id = {1};port = {2};database=my_db2;pooling = false;", "localhost", "root", 3306);
string condition = string.Format("SELECT name,age FROM student where name='{0}'", name); //常量查询,字符串需要加‘ ’(单引号)
MySqlDataAdapter adapter = new MySqlDataAdapter(condition,P_Str_ConnectionStr);
DataTable table = new DataTable();
adapter.Fill(table);
this.dataGridView1.DataSource = table;
return table;
}
//查询
private void select_Click(object sender, EventArgs e)
{
//int age = int.Parse(textBox1.Text);
string name = textBox1.Text;
GetMessage(name);
}