从mySQL提取计数(列)到VB.NET
嘿,伙计们,我必须从数据库中提取一些值,并将它们放在我的文本框中。在以下位置有一个问题:从mySQL提取计数(列)到VB.NET,mysql,vb.net,Mysql,Vb.net,嘿,伙计们,我必须从数据库中提取一些值,并将它们放在我的文本框中。在以下位置有一个问题: TextBox1.Text = TextBox1.Text & DR.Item("id") & Space(3) & DR.Item("Nume") & Space(3) & DR.Item("COUNT(pontaj.prezente)") VB中的错误: 我的选择是这样的: Dim dbCon = New MySqlConnection("Server =
TextBox1.Text = TextBox1.Text & DR.Item("id") & Space(3) & DR.Item("Nume") & Space(3) & DR.Item("COUNT(pontaj.prezente)")
VB中的错误:
我的选择是这样的:
Dim dbCon = New MySqlConnection("Server = localhost;Database = users; Uid=root; Pwd = password ")
'SELECT users1.id,users1.Nume, COUNT(pontaj.prezente) FROM users1, pontaj WHERE users1.id = pontaj.id
Dim strQuery = "SELECT users1.id,users1.Nume, COUNT(pontaj.prezente)" & _
"FROM users1, pontaj "
Dim SQLCmd = New MySqlCommand(strQuery, dbCon)
' Pwd = password
' Open
dbCon.Open()
Dim DR = SQLCmd.ExecuteReader
TextBox1.Text = TextBox1.Text & DR.Item("id") & Space(3) & DR.Item("Nume") & Space(3) & DR.Item("COUNT(pontaj.prezente)") & vbCrlf
While DR.Read
End While
'Close
DR.Close()
dbCon.Close()
错误很明显,在调用Read之前,您不能访问DataReader的字段。 调用需要将读卡器定位到查询返回的第一条记录上,然后在后续记录上前进,直到到达返回记录的末尾 此外,查询的语法似乎不正确,您引用查询第三列的方式也不正确
Dim dbCon = New MySqlConnection(............)
Dim strQuery = "SELECT users1.id,users1.Nume, COUNT(pontaj.prezente) as countPrezente " & _
"FROM users1 INNER JOIN pontaj ON users1.id = pontaj.id " & _
"GROUP BY users1.id, users1.Nume"
Dim SQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
Dim DR = SQLCmd.ExecuteReader
' If Read returns true then you have one or more record to show'
While DR.Read()
TextBox1.Text = TextBox1.Text & _
DR.Item("id") & Space(3) & _
DR.Item("Nume") & Space(3) & _
DR.Item("countPrezente") & vbCrlf
End While
DR.Close
dbCon.Close
看看前面的问题,users1和pontaj之间的外键名为id,因此我在两个表之间使用了显式连接来链接两个表中的记录