简体   繁体   English

选择* From table where列Like语句c#

[英]Select * From table Where column Like statement c#

I failed to get the correct result with this code in Form2: 我无法在Form2中使用以下代码获得正确的结果:

conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * From udbTable Where Username Like '" + f1.textBox1.Text + "%'", conn);
OleDbDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            label5.Text = reader["Username"].ToString();
        }
        conn.Close();

I have 3 samples data in the table, but i'm always getting the same result which is the first entry of the database. 我在表中有3个样本数据,但是我总是得到相同的结果,这是数据库的第一个条目。 Whenever i input the last entry or second entry in the textbox1.Text , i still getting the first entry. 每当我在the textbox1.Text输入最后一个条目或第二个条目时,我仍然会获得第一个条目。

textbox1.Text is from Form1, and i set it's property Modification to Public . textbox1.Text来自Form1,我将其属性Modification设置为Public label5.text is the output. label5.text是输出。

try this fix 试试这个修复

conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection=conn;
command.CommandText = "Select * From udbTable Where Username Like ?";
cmd.Parameters.Add("@Username",OleDbType.VarChar);
cmd.Parameters["@Username"].Value=f1.textBox1.Text;
OleDbDataReader reader = cmd.ExecuteReader();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM