当我在TextBox中输入学生的身份证号码时,我希望在我的文本框中显示该特定学生的详细信息,而不使用datagrid。我正在使用vb.net。
这是我的代码,我不明白错误:
cmd.CommandText = "SELECT * from tblsupplier where pro_code = '" & txcode.Text & "'"
dr = cmd.ExecuteReader
txname.Text = dr.item("sup_product")
txprice.Text = dr.Item("sup_price")
答案 0 :(得分:1)
首先,通过添加内联文本框的值,您已将应用程序打开到sql注入。请查看此主题:
http://en.wikipedia.org/wiki/SQL_injection
至于你的代码,你需要先调用dr.Read()才能真正访问查询中的任何属性。
while (dr.Read()) {
// Do stuff
}
祝你好运!
答案 1 :(得分:0)
如果您能提供有关错误的其他详细信息,那将会有所帮助。就像您正在使用的SQL一样,错误消息是什么,以及哪个事件触发了此reader命令。
目前看来,您似乎还没有真正发送DataReader来扫描数据库。您需要While reader.read()
循环。
这是一个示例,在获取数据的函数中使用OLEDB
请注意,connstring是我访问数据库的SQL连接字符串
Dim conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=MyDatabase.mdb;Jet OLEDB:Database Password=;")
Function Mycheckifexistss(ByVal sqlCMDstring As String)
Dim sqlCmd As New OLEDBCommand(sqlCMDstring, conn)
Dim reader As OLEDBDataReader
Dim result As String = ""
Try
conn.Open()
reader = sqlcmd.ExecuteReader
While reader.Read
result = reader(0) 'this returns the first field of the queried result.
End While
conn.Close()
Return result
Catch ex As Exception
conn.Close() 'the first thing you want to do in an error, is to close the connection first.
MsgBox(ex.ToString)
End Try
End Function