2015-08-08 103 views
0

我與下列的表MS Access和VB.net查詢錯誤

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

的[文檔SELECT](https://msdn.microsoft.com/en-us/library/office/ff821148.aspx)不顯示任何分隔符排序別名 - 它是否工作,如果你使用'SELECT Sum(qty)AS totqty FROM'...?另外,我建議使用SQL參數,而不是將查詢值串聯到SQL字符串中。 –

+0

哪一行會引發錯誤?這是一個嗎? SUM_TEXT.Text = RAD.GetDouble(「totqty」) – HarveyFrench

+0

@Andrew。你是否修復了代碼?我給了一個修復的答案? – HarveyFrench

回答

1
= " & MT_TEXT.SelectedValue.ToString & "" 

這看起來前途未卜的任何解決方案。爲什麼最後有兩個雙引號,而且之前只有一個。

如若線讀取:

ShowTable.CommandText 
= "SELECT Sum(qty) AS [totqty] 
FROM moves WHERE [mat_id] = """ & MT_TEXT.SelectedValue.ToString & """"