我必须创建一个存储过程,它将搜索数据库中的匹配行并将它们显示在 Gridview 中。用户可以通过选择多个选项的表单进行搜索,其中一些选项不是强制性的。
我创建了一个存储过程,但它仅在我在 SQL Server Management Studio 控制台中执行时才显示结果。存储过程未在 aspx 页面的 gridview 中显示任何记录。
请帮我看看我在这里做错了什么。
我创建的存储过程是这样的:
ALTER PROCEDURE dbo.ProcName
@abc varchar(50) = null,
@def varchar(50) = null,
@ghi Int = null,
@jkl varchar(50) = null,
@mno varchar(50) = null
As
Begin
SELECT abc,def,ghi,jkl,mno,rst from TableName
where(@abc IS NULL OR abc= @abc)
AND (@def IS NULL OR def = @def)
AND (@ghi IS NULL OR ghi = @ghi)
AND (@jkl IS NULL OR jkl = @jkl)
AND (@mno IS NULL OR mno = @mno)
End
Go
这是我在 asp.net 页面中用于在 gridview 中显示记录的代码:
Sub showResult(Source as Object, E as EventArgs)
Dim oConn As SQLConnection
Dim oComm As SQLCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim sSQL As String
Dim sConn As String
Dim strloc, listtype, roomno, furnishing, spec, forrent, price, area, email
strloc=List_Location.SelectedItem.Text
listtype=List_Type.SelectedItem.Text
roomno=List_RoomNo.SelectedItem.Value
furnishing=List_Furnishing.SelectedItem.Text
forrent=List_RentSale.SelectedItem.Value
sConn = ConfigurationManager.ConnectionStrings("xyz").ConnectionString
oConn = New SQLConnection(sConn)
oComm = New SQLCommand("SearchP",oConn)
oConn.Open()
oComm.CommandType = CommandType.StoredProcedure
oComm.Parameters.Add(New SqlParameter("@abc", SqlDbType.VarChar, 50))
oComm.Parameters("@abc").Value = strloc
oComm.Parameters.Add(New SqlParameter("@def", SqlDbType.VarChar, 50))
oComm.Parameters("@def").Value = listtype
oComm.Parameters.Add(New SqlParameter("@ghi", SqlDbType.Int))
oComm.Parameters("@ghi").Value = roomno
oComm.Parameters.Add(New SqlParameter("@jkl", SqlDbType.VarChar, 50))
oComm.Parameters("@jkl").Value = furnishing
oComm.Parameters.Add(New SqlParameter("@mno", SqlDbType.VarChar, 50))
oComm.Parameters("@mno").Value = forrent
da = New SqlDataAdapter(oComm)
ds = New DataSet()
Try
panel1.visible="true"
da.Fill(ds,"TableName")
myGridView.DataSource=ds.Tables("TableName").DefaultView
myGridView.EmptyDataText = "No records found"
myGridView.DataBind()
Catch ex As Exception
lblResults.text = ex.Message
Finally
oComm.Dispose()
oConn.Dispose()
oConn.Close()
End Try
End Sub