为什么会出现此错误,请给我解决方案.(在更新时未提供一个或多个必需参数的值) [英] why this error come please give me solution.(No value given for one or more required parameters) while doing updatetion

查看:71
本文介绍了为什么会出现此错误,请给我解决方案.(在更新时未提供一个或多个必需参数的值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim cmdupdate As OleDbCommand = New OleDbCommand
      con.Open()
      sql = "UPDATE Info SET [add] = '" & txtadd.Text & "'," _
     & "[phno] = '" & txtphno.Text & "'," _
     & "[emailid] = '" & txtemailid.Text & "'," _
      & "[gender] = '" & txtgender.Text & "'," _
      & "[dob] = '" & txtdob.Text & "'," _
     & "[quali] = '" & txtqual.Text & "'," _
    & "[designation] = '" & txtdegn.Text & "', " _
    & "[salary] = '" & txtsal.Text & "' ," _
    & "[bank] = '" & txtbank.Text & "'" _
    & "WHERE name = " & cmbname.Text & ""

      cmd = New OleDbCommand(sql, con)
      cmd.ExecuteNonQuery()
      con.Close()
      MsgBox(cmbname.Text = "Record updated.")

推荐答案

您的代码几乎不可读,但问题只是SQL语法.如果您完全改变方法,也许您会看到它并能够更正查询.您确实非常需要使用参数化查询,并且出于许多重要原因.
开始于:
http://msdn.microsoft.com/en-us/library/ms254953.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/yy6y35y8%28v = VS.100%29.aspx [ ^ ](此处是一些OleDb代码示例).

顺便说一句,多字符串串联确实不是一个好主意.这是因为字符串是不可变的,因此对性能不利.我什至必须解释为什么吗?使用string.Format可以解决此问题,并且可读性更高.在其他情况下(例如循环),请使用( mutable )System.Text.StringBuilder.

参见:
http://msdn.microsoft.com/en-us/library/system.string.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.text. stringbuilder.aspx [ ^ ].

—SA
Your code is hardly readable, but the problem is just the SQL syntax. Perhaps you will see it and will be able to correct the query if you totally change the approach. You really, really need to use parametrized queries, and by a number of much more important reasons.

Start with:
http://msdn.microsoft.com/en-us/library/ms254953.aspx[^],
http://msdn.microsoft.com/en-us/library/yy6y35y8%28v=VS.100%29.aspx[^] (some OleDb code samples here).

By the way, multiple string concatenation is really bad idea. This is because strings are immutable, so it''s bad for performance. Do I even have to explain why? Using string.Format is free from this problem and also much more readable. In other cases (such as cycles), use (mutable) System.Text.StringBuilder.

See:
http://msdn.microsoft.com/en-us/library/system.string.aspx[^],
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

—SA


同意SA.使用参数化查询.在这里,我指出您的一些错误.也有建议
Agree with SA. Use parametrized queries. And here I point out some of your mistakes. Also suggestion(s)
sql = "UPDATE Info SET [add] = '" & txtadd.Text & "'," _
     & "[phno] = '" & txtphno.Text & "'," _
     & "[emailid] = '" & txtemailid.Text & "'," _
      & "[gender] = '" & txtgender.Text & "'," _
      & "[dob] = '" & txtdob.Text & "'," _
     & "[quali] = '" & txtqual.Text & "'," _
    & "[designation] = '" & txtdegn.Text & "', " _
    & "[salary] = '" & txtsal.Text & "' ," _
    & "[bank] = '" & txtbank.Text & "'" _
    & "WHERE name = " & cmbname.Text & ""



在字段名称上放置方括号.
在您的代码中,名称字段是字符串类型,因此您应包括单引号(WHERE name = ''" & cmbname.Text & "''").并在WHERE关键字前留一个空格.



Put square brackets for the field names.
Here in your code, name field is string type so you should have included the single quotes(WHERE name = ''" & cmbname.Text & "''"). And give a space before the WHERE keyword.


这篇关于为什么会出现此错误,请给我解决方案.(在更新时未提供一个或多个必需参数的值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆