如何在vb.net asp.net中执行搜索功能?

时间:2018-11-27 03:51:56

标签: sql asp.net vb.net

我想使用VB在我的ASP.NET中实现搜索功能

这些是我的代码,但是“ LIKE”查询中出现了一些错误。我对SQL查询有些困惑。请帮助,谢谢。

Dim searchOwnerSql As String =
        "SELECT Owners.name, Owners.age, Cars.platenumber FROM Owners INNER JOIN Cars LIKE @a"
    searchOwnerCmd = New SqlCommand(searchOwnerSql, conn)

Protected Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click

    Dim search As String = SearchTextBox.Text

    searchOwnerCmd.Parameters.Clear()
    searchOwnerCmd.Parameters.AddWithValue("a", "%" & search & "%")

    Dim adapter As SqlDataAdapter = New SqlDataAdapter(searchOwnerCmd)
    Dim ds As DataSet = New DataSet()
    adapter.Fill(ds, "search")
    Dim dt As DataTable = ds.Tables("search")
    totalCarsGridView.DataSource = dt
    totalCarsGridView.DataSourceID = Nothing
    totalCarsGridView.DataBind()
    If (dt.Rows.Count = 0) Then
        Response.Write("SUCCESS")
    Else
        Response.Write("FAILED")
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您的SQL语法错误,应该是这样的:

SELECT ... FROM LeftTable l
内部联接 RightTable r
开启 l。[SomeKey] = r。[SomeKey]
位置 l。[FilteredColumn] Like '%seachedValue%'

例如,您应该在https://www.w3schools.com/sql/中了解有关SQL的更多信息

如果您的数据库表是这样的:

   --------------
   | TABLE Cars |
   --------------
   CarId int,
   PlateNumber varchar(50),
   OwnerID int

   ----------------
   | TABLE Owners |
   ----------------
   OwnerID int,
   [Name] varchar(50),
   Age int

并且您要搜索特定的PlateNumber,SQL查询应为:

   SELECT Owners.name, Owners.age, Cars.PlateNumber FROM Owners 
   INNER JOIN Cars 
   ON Owners.OwnerID = Cars.OwnerID 
   WHERE Cars.PlateNumber LIKE @a