我有一个包含以下列的表
ID(integer)
mat_id(integer)
move_date(Date/Time)
rec_num (Short text)
Qty (number(double))
我以编程方式建立了连接 并尝试了一个查询 代码如下
Public Class ViewMatInfo
Dim ConStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\RMM\RMMDB.mdb;Persist Security Info=True"
Dim Conn As New OleDb.OleDbConnection(ConStr)
Dim ShowTable As New OleDb.OleDbCommand
Dim RAD As OleDb.OleDbDataReader
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
ShowTable.Connection = Conn
ShowTable.CommandType = CommandType.Text
ShowTable.CommandText = "SELECT Sum(qty) AS [totqty] FROM moves WHERE [mat_id] = " & MT_TEXT.SelectedValue.ToString & ""
Conn.Open()
RAD = ShowTable.ExecuteReader()
While RAD.Read
SUM_TEXT.Text = RAD.GetDouble("totqty")
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
Me.MovesTableAdapter.FillByMat(Me.RMMDS.moves, MT_TEXT.SelectedValue)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
当我运行应用程序并从“MT_TEXT”组合框中选择材料时 然后单击按钮 所有材料移动都会大大填充数据网格视图。 但SUM_TEXT文本框仍为空。 并获取错误消息 “从字符串”totqty“转换为整数无效”
请问这个问题的解决方案
答案 0 :(得分:1)
= " & MT_TEXT.SelectedValue.ToString & ""
这看起来不确定。为什么最后有两个双引号,之前只有一个双引号。
该行应该是:
ShowTable.CommandText
= "SELECT Sum(qty) AS [totqty]
FROM moves WHERE [mat_id] = """ & MT_TEXT.SelectedValue.ToString & """"