sql
  • vb.net
  • ms-access
  • 2016-02-12 94 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

    相关问题