从字母数字值查找查询

时间:2018-07-27 04:52:34

标签: c# mysql

实习生ID是一个字母数字值。

问题是运行该程序后,搜索代码仅适用于数字。不适用于字母数字。我该怎么解决。

private void button1_Click(object sender, EventArgs e)
    {

        if (txtsearch.Text != "")
        {
            MessageBox.Show("Please type intern number");
        }
        else
        {
            try
            {
                con.Open();
                string SelectQuery2 = "SELECT * FROM interndetails WHERE internnumber =" + Convert.ToString(txtsearch.Text);
                cmd4 = new MySqlCommand(SelectQuery2, con);
                mdr2 = cmd4.ExecuteReader();
                if (mdr2.Read())
                {
                    textBox11.Text = mdr2.GetString("name");
                    textBox9.Text = mdr2.GetString("idnumber");
                    textBox6.Text = mdr2.GetString("status");
                    textBox12.Text = mdr2.GetString("statusdate");
                    textBox13.Text = mdr2.GetString("intakedate");
                    textBox14.Text = mdr2.GetString("fplacementdate");
                    textBox10.Text = mdr2.GetString("secbrdpt");
                    textBox15.Text = mdr2.GetString("periodcomplertion");
                    mdr2.Close();
                    con.Close();
                }
                else
                {
                    MessageBox.Show("No Interns were found!");
                    txtsearch.Clear();
                    txtsearch.Focus();
                }
                con.Close();
            }
            catch (MySqlException x)
            {
                MessageBox.Show(x.Message);
                con.Close();
            }

        }

    }


}

2 个答案:

答案 0 :(得分:0)

类似的事情应该起作用:

string SelectQuery2 = "SELECT * FROM interndetails WHERE internnumber = @internNumber";
cmd4 = new MySqlCommand(SelectQuery2, con);
cmd4.Parameters.Add("@internNumber", MySqlDbType.String).Value = txtsearch.Text;

答案 1 :(得分:0)

可能对您有帮助

SELECT * FROM intern详细信息,其中concat(internnumber,'')='“ + Convert.ToString(txtsearch.Text)+”'“