【问题标题】:C# MySQL Search queryC# MySQL 搜索查询
【发布时间】:2016-01-11 12:35:28
【问题描述】:

此代码具有搜索患者房间分配的功能:

else if(radioButton1.Checked == true)
{
    DataTable table = new DataTable();
    cmd = new MySqlCommand("Select * from patientinfo where LastName and FirstName = '" + textBox1.Text + "'", con.con);
    reader = cmd.ExecuteReader();
    MySqlDataReader dr = cmd.ExecuteReader();
    if(dr.Read())
    {
        label2.Text = dr.GetString("LastName") + "," + dr.GetString("FirstName");
        label6.Text = dr.GetString("Room");
        if(label6.Text == "506")
        {
            label3.Text = "Payward Building";          
        }                
        else if(label6.Text == "507")                
        {                
            label3.Text = "Payward Building";                    
        }                    
    }
    else
    {                     
        MessageBox.Show("No Data! Please Try Again", "Warning", MessageBoxButtons.OK);                        
        con.Close();                     
    }                    

我很难搜索患者姓名,该姓名在数据库中存储为两个不同的字段,FirstNameLastName。我只想输入名字和姓氏的组合。

【问题讨论】:

  • where LastName != FirstName?
  • 您的代码在label6.Text = dr.GetString("Room"); 行之后似乎缺少if 语句。那是错字吗?还要考虑正确缩进代码以使其更具可读性。
  • 不,它可能会先在姓氏中查找数据,然后再搜索名字。感谢您帮助我,
  • @EnnT 不要在评论中发布更正的代码。改为编辑您的问题。
  • 对不起,我好像根本没有看到编辑问题,谢谢你提醒我。

标签: c# mysql


【解决方案1】:

尝试将您的 SQL 代码更改为:

cmd = new MySqlCommand("Select * from patientinfo where LastName + ' ' + FirstName like '%" + textBox1.Text + "%'", con.con);

【讨论】:

  • 似乎只是将like替换成=。感谢您的大力帮助。
猜你喜欢
  • 2012-11-12
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2018-07-16
  • 2013-07-19
  • 2015-03-27
  • 2011-10-04
相关资源
最近更新 更多