参数化查询需要未提供的参数 [英] The parameterized query expects the parameter which was not supplied

查看:23
本文介绍了参数化查询需要未提供的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题:

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    list.Items.Clear()

    cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%" & TextBox2.Text & "%')"
    cmd.Connection = con
    cmd.CommandType = CommandType.Text
    con.Open()


    rd = cmd.ExecuteReader()
    If rd.HasRows = True Then
        While rd.Read()

            Dim listview As New ListViewItem

            listview.Text = rd("ID").ToString
            listview.SubItems.Add(rd("Department").ToString)
            listview.SubItems.Add(rd("Purpose").ToString)
            listview.SubItems.Add(rd("Items_Details").ToString)
            listview.SubItems.Add(rd("Requested_by").ToString)
            listview.SubItems.Add(rd("Approved_by").ToString)
            listview.SubItems.Add(rd("Date").ToString)
            listview.SubItems.Add(rd("Status").ToString)
            listview.SubItems.Add(rd("Date_Returned").ToString)

            list.Items.Add(listview)

        End While
    End If
    con.Close()

在文本框中输入字符串以搜索项目后,我收到此错误:

Once I typed in the string in the textbox to search for an item I get this error:

参数化查询 '(@Parameter1 nvarchar(4000))SELECT * FROM借用 where (Departme' 需要参数 '@Parameter1',这是未提供.

The parameterized query '(@Parameter1 nvarchar(4000))SELECT * FROM borrow where (Departme' expects the parameter '@Parameter1', which was not supplied.

有人可以帮我吗?

推荐答案

如果给参数传递空值,即使添加参数也会出现这个错误所以尝试检查值,如果它为空,则使用 DBNull.Value

If you pass null value to parameter,you will get this error even after you add the parameter so try to check the value and if it null then use DBNull.Value

这会起作用

cmd.Parameters.Add("@Department", SqlDbType.VarChar)

If (TextBox2.Text = Nothing) Then
    cmd.Parameters("@Department").Value = DBNull.Value
Else
    cmd.Parameters("@Department").Value = TextBox2.Text
End If

这会将对象层的空值转换为数据库可接受的 DBNull 值.

This will convert the null values from the object layer to DBNull values that are acceptable to the database.

这篇关于参数化查询需要未提供的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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