sql
  • vb.net
  • ms-access
  • 2016-02-12 91 views 0 likes 
    0

    我無法修復代碼,它總是顯示沒有給出一個或多個參數的值。我想知道我應該改變什麼來修復它。我的數據庫是基於我懷疑的電子郵件地址是問題的根源,因爲它包含一個@字符的查詢訪問2007刪除給定的一個或多個參數沒有值

    嘗試

     dataB = "Update login set username = '" & txtUserName.Text.Replace("'", "''") & "' , dateofb = '" & dtpDOB1.Text.Replace("'", "''") & "', placeofb = '" & txtPOB.Text.Replace("'", "''") & "', email = '" & txtEmailID.Text.Replace("'", "''") & "' where userid = " & useridlbl.Text 
    
         ConnDB() 
         cmd = New OleDbCommand(dataB, conn) 
         Dim i As Integer 
         i = cmd.ExecuteNonQuery 
         If i > 0 Then 
          MsgBox("Update Succesfully", MsgBoxStyle.Information, "Confirmation") 
          Me.Dispose() 
          userinfofrm.Show() 
    
    
         Else 
          MsgBox("Failed Updating", MsgBoxStyle.Information, "Alert!") 
         End If 
        Catch ex As Exception 
         MsgBox(ex.Message) 
        Finally 
         cmd.Dispose() 
         conn.Close() 
    
        End Try 
    
    +0

    請問您還可以添加數據庫的圖表嗎? – Dave

    +0

    Userid --text,username - text,pass - 文本,dateofb - 文本,placeofb - 文本,電子郵件 - 文本 –

    +0

    如果在調試器中檢查dataB的值,是否存在問號或命名參數像文本中的@參數名稱? – Markus

    回答

    3

    。 OleDbCommand將其解釋爲參數,因此查找參數值。由於您未指定參數值,因此會引發錯誤。

    爲了解決這個問題,請在您的查詢中更好地使用參數。儘管您在語句中使用了單引號,但使用參數來防止SQL注入攻擊要安全得多。參數也規避了關於日期值等地區特定格式的問題。另外,使用參數化查詢允許數據庫服務器(或MS訪問)爲後續請求緩存查詢計劃。

    如下更改聲明:

    dataB = "Update login set username = @userName, dateofb = @dateOfB, " + _ 
         "placeofb = @placeOfB, email = @email where userid = @userId " 
    
    ConnDB() 
    
    cmd = New OleDbCommand(dataB, conn) 
    cmd.Parameters.AddWithValue("@userName", txtUserName.Text) 
    cmd.Parameters.AddWithValue("@dateOfB", DateTime.Parse(dtpDOB1.Text)) ' Maybe your control can provide you with the DateTime value right away 
    cmd.Parameters.AddWithValue("@placeOfB", txtPOB.Text) 
    cmd.Parameters.AddWithValue("@email", txtEmailID.Text) 
    cmd.Parameters.AddWithValue("@userId", useridlbl.Text) 
    
    Dim i As Integer 
    i = cmd.ExecuteNonQuery 
    ' ... 
    

    請注意,我用的是MS接入理解命名參數語法。其他OleDB提供者可能需要以問號形式提供未命名的參數。正如@Steve在註釋中指出的那樣,語句中參數的順序應該與它們被添加到Parameters集合的順序相匹配。

    +0

    是的。它爲我工作,謝謝你.... –

    +0

    @JaemineSanchez不客氣 – Markus

    +0

    @JaemineSanchez作爲網站的新用戶,我推薦閱讀[如何接受答案工作](http://meta.stackexchange.com/ question/5234/how-do-accepting-an-answer-work) – Steve

    相關問題