2017-05-05 212 views
0

我試圖讓使用此代碼從MS Access數據庫中的數據,但我不能,這是我的代碼這是正確的從數據庫asp.net中獲取數據

Dim query As String = "SELECT [data] FROM tabless WHERE user = '" & user.Text & "'" 
Using connection As New OleDbConnection(connectionString) 

    Dim cmd As New OleDbCommand(query) 
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(query, connection)  
    Dim com As New OleDbCommand(query, connection)  
    connection.Open() 
    'on the line below I get an error: connection property has not been initialized  
    Dim reader As OleDbDataReader = cmd.ExecuteReader() 

    While reader.Read() 
     Label1.Text = (reader(0).ToString()) 
    End While 

    reader.Close() 

End Using 

數據庫

|data| 
    asl 

試圖從數據庫中獲取數據並試圖在標籤中顯示它是可能的

+0

錯誤是什麼?調試時會發生什麼? – JKerny

+0

'Dim reader As OleDbDataReader = cmd.ExecuteReader()' 連接屬性尚未初始化 – Craftx398

+1

該信息應該是您問題的一部分。請在此處進行修改,並意識到您需要在發佈的每個問題中都包含這類信息。我們不應該問你這個問題。 – mason

回答

2

您從未將cmd與連接關聯,並且您n曾經使用comadapter。這是你可以通過一行一行地逐行檢查代碼並檢查其狀態的方法。

Dim query As String = "SELECT [data] FROM tabless WHERE user = '" & user.Text & "'" 

Using connection As New OleDbConnection(connectionString) 

    Dim cmd As New OleDbCommand(query, connection) 
    connection.Open() 
    Dim reader As OleDbDataReader = cmd.ExecuteReader() 

    While reader.Read() 
     Label1.Text = (reader(0).ToString()) 
    End While 

    reader.Close() 

End Using 

此外,您的代碼容易受到SQL注入攻擊。您不應該將字符串連接在一起以形成查詢。你應該改用參數化查詢。