更新时间:2023-02-22 10:49:09
首先,我会更改您的查询以使用参数化命令
First of all, I would change your query to use a parameterized command
da = New Odbc.OdbcDataAdapter("Select distinct lname FROM tblemployee WHERE emp_id =?", con)
da.SelectCommand.Parameters.AddWithValue("@p1", TextBox1.Text.Trim())
dr = cmd.ExecuteReader
If dr.Read Then
txtlname.Text = dr("lname").ToString
txtlname.Refresh()
Else
MessageBox.Show("No record found!")
End If
然后,我添加了一条消息,以防 dr.Read
返回 false.(没有符合 where 条件的记录)至少你会知道文本框保持空白是否有一个简单的原因.
Then, I have added a message in case dr.Read
return false. (No record matching the where condition)
At least you will know if there is a simple reason why the textbox remains blank.
最后一点.如果 emp_id
是表 tblEmployee
中的数字字段,则传递给 SelectCommand
参数集合的值应转换为整数,因为原样,它作为字符串传递,这可能是丢失结果的来源.
A final note. If emp_id
is a numeric field in the table tblEmployee
the value passed to the SelectCommand
parameters collection should be converted to an intenger because, as is, it is passed as a string and this could be the source of missing results.