我如何将参数传递给 sqldataadapter?

How could I pass parameter to sqldataadapter?

我试过给参数传递一个值,但是returns什么也没有。如果我使用不带参数的简单 sql 命令,我可以从我的数据库中检索所有数据。 这是我的代码:

Public Shared Function RetrieveData() As DataTable
    Connection.Open()
    Try
        sql = "SELECT DEP_ID AS CODE, DEP_NAME AS DEPARTMENT FROM DEPART_TBL WHERE " & FieldName & " LIKE N'%@criteria%'"
        da = New SqlDataAdapter
        da.SelectCommand = New SqlCommand(sql, Connection.SQLConnection)
        da.SelectCommand.Parameters.Add("@criteria", SqlDbType.NChar)
        da.SelectCommand.Parameters("@criteria").Value = "KCL"
        'da.SelectCommand.Parameters.Add("@criteria", SqlDbType.NChar, 10, "DEP_ID")
        'Message.ShowInfo(da.SelectCommand.CommandText.ToString)
        'Exit Function
        dt = New DataTable
        da.Fill(dt)
    Catch ex As Exception
        Message.ShowError(ex.Message)
    End Try
    da.Dispose()
    Connection.Close()
    Return dt
End Function

你正在搜索字符串@criteria,你想使用参数,所以使用:

sql = "SELECT DEP_ID AS CODE, DEP_NAME AS DEPARTMENT FROM DEPART_TBL WHERE " & FieldName & " LIKE N'%' + @criteria + '%'"

或者你可以在参数中使用它:

da.SelectCommand.Parameters("@criteria").Value = "%KCL%"