Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我想在我的文本框中显示记录_Vb.net - Fatal编程技术网

Vb.net 我想在我的文本框中显示记录

Vb.net 我想在我的文本框中显示记录,vb.net,Vb.net,当我在文本框中输入学生的id号时,我希望在文本框中显示特定学生的详细信息,而不使用datagrid。我正在使用vb.net 这是我的代码,我不明白出了什么问题: cmd.CommandText = "SELECT * from tblsupplier where pro_code = '" & txcode.Text & "'" dr = cmd.ExecuteReader txname.Text = dr.item("sup_product")

当我在文本框中输入学生的id号时,我希望在文本框中显示特定学生的详细信息,而不使用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")

首先,通过内联添加文本框的值,使应用程序对sql注入开放。请查阅本主题:

至于您的代码,您需要先调用dr.Read(),然后才能从查询中实际访问任何属性

while (dr.Read()) {
    // Do stuff
}

祝你好运

朋友,如果您能提供有关错误的更多详细信息,那会有所帮助。就像您正在使用的SQL一样,错误消息是什么,以及触发此读卡器命令的事件是什么

目前看来,您似乎还没有真正发送DataReader来扫描数据库。您需要
While reader.read()
循环

下面是一个示例,在获取数据的函数中使用OLEDB

请注意,connstring是我到Access数据库的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

cmd.CommandText=“SELECT*from tblsupplier where pro_code='”&txcode.Text&“'dr=cmd.ExecuteReader txname.Text=dr.Item(“sup_product”)”这是错误的答案,表示“调用Read()之前访问字段的尝试无效”txprice.Text=dr.Item(“sup_price”),如果您认为这个答案解决了您的问题,请别忘了把它标为正确答案。”这是合适的网络礼仪。我应该包括dr.项目吗?它有一个错误,先生。While(dr.Read())txname.Text=dr.Item(“sup_产品”)txprice.Text=dr.Item(“sup_价格”)结束时