gpt4 book ai didi

sql - vb.net 从数据库中读取数据

转载 作者:搜寻专家 更新时间:2023-10-30 19:40:12 26 4
gpt4 key购买 nike

所以我使用这段示例代码从 MS Access 数据库中检索数据并将其显示在表单的几个文本框中。出现以下错误 -

@ dr = cmd.ExecuteReader - Data type mismatch in criteria expression. dr = cmd.ExecuteReader

这是示例代码 -

Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\example\Desktop\Data.accdb" ' Change it to your Access Database location
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub

Dim r As Random = New Random

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
myConnection.Open()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
Dim str As String
str = "SELECT * FROM Items WHERE (Code = '" & r.Next(1, 3) & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox1.Text = dr("Description").ToString
TextBox2.Text = dr("Cost").ToString
TextBox3.Text = dr("Price").ToString
End While
myConnection.Close()
End Sub

最佳答案

试试这个..

str = "SELECT * FROM Items WHERE (Code = '"& (r.Next(1, 3)).ToString() & "')"

关于sql - vb.net 从数据库中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com