无法通过vb net传递参数来更新sql查询 [英] cannot pass parameter to update sql query with vb net

查看:74
本文介绍了无法通过vb net传递参数来更新sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数从2个列表框传递到SQL表(更新表记录)

此代码返回错误,表示没有参数succes1



I want to pass parameters from 2 listboxes to an SQL table (update the table records)
This code returns error that there is no parameter succes1

Dim query, ss, sd As String
     Dim k, i As Integer
     Dim cmd As New SqlCommand()
     query = "update Table_1 set success  like @succes1 where id_2 like @id2"

     For i = 0 To ListBox1.Items.Count - 1 And ListBox3.Items.Count - 1
        ss = ListBox1.GetItemText(ListBox1.SelectedItems.Item(i).ToString)
         sd = ListBox3.GetItemText(ListBox3.SelectedItems.Item(i).ToString)
                   cmd.Connection = SqlConnection1
         cmd.CommandType = CommandType.Text
         cmd.CommandText = query
                    cmd.Parameters.Item("succes1").Value = sd 'error here
         cmd.Parameters.Item("id2").Value = ss

         Try
             SqlConnection1.Open()
             cmd.ExecuteNonQuery()
             SqlConnection1.Close()



选择所有列表框(1和3)行


all listbox (1 and 3) rows are selected

推荐答案

SET子句看起来很奇怪。如果你想设置成功列的值,请尝试类似

The SET clause looks odd. If you want to set the value of success column, try something like
query = "update Table_1 set success = @succes1 where id_2 like @id2"



你首先需要将参数添加到参数中在为其定义值之前进行收集。我认为最简单的方法是使用 AddWithValue [ ^ ]


这篇关于无法通过vb net传递参数来更新sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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