我不确定您是否应该在数据库中强制执行“至少两个条件”条件,因为您可能永远都不知道已经填写了两个条件。相反,这可能对您有用-
这是我使用的一种模式经常并且应该应付条件的任意组合(我假设这在存储的proc中!):
DECLARE PROCEDURE PropertyList@StreetName NVARCHAr(50) = NULL,@Town NVARCHAr(50) = NULL,@Postpre NVARCHAr(10) = NULLASSET NOCOUNT ONSELECt *FROM VWTenantPropertiesResultsWHERe ContentBedrooms BETWEEN 1 AND 4AND ContentPrice BETWEEN 50 AND 500AND (@ContentStreet IS NULL OR ContentStreet = @ContentStreet)AND (@ContentTown IS NULL OR ContentTown = @ContentTown)AND (@ContentPostpre IS NULL OR ContentTown = @ContentTown)ORDER BY ContentPrice
要从您的ASP页面调用此代码,您需要一些类似以下的代码(这 可能 需要一些调试,我的ASP的ADO和VBscript非常生锈!):
Dim cnn 'As ADODB.ConnectionDim cmd 'As ADODB.CommandDim prmStreet 'As ADODB.ParameterDim prmTown 'As ADODB.ParameterDim prmPostpre 'As ADODB.ParameterDim rstProperty 'As ADODB.RecordSetDim i 'As IntegerSet cnn = Server.CreateObject("ADODB.Connection")cnn.ConnectionString = MyConnectionStringSet cmd = Server.CreateObject("ADODB.Command")Set cmd.ActiveConnection = cnn'Set the CommandText property to the name of the stored proc we want to callcmd.CommandText = "PropertyList"cmd.CommandType = 4 'or adCmdStoredProc if you're using ADOVBS.incIf Request.Form("StreetTextBox") = "" Then 'No street entered so don't pass it to the stored procElse 'A street has been entered so create a parameter... Set prmStreet = cmd.CreateParameter("@StreetName", 203, 1, 50, Request.Form("StreetTextBox")) ' and add it to the Parameters collection of the Command object cmd.Parameters.Add(prmStreet)End IfIf Request.Form("TownTextBox") = "" Then 'No town entered so don't pass it to the stored procElse 'A town has been entered so create a parameter... Set prmTown = cmd.CreateParameter("@Town", 203, 1, 50, Request.Form("TownTextBox")) ' and add it to the Parameters collection of the Command object cmd.Parameters.Add(prmTown)End IfIf Request.Form("PostpreTextBox") = "" Then 'No postpre entered so don't pass it to the stored procElse 'A postpre has been entered so create a parameter... Set prmPostpre = cmd.CreateParameter("@Postpre", 203, 1, 10, Request.Form("PostpreTextBox")) ' and add it to the Parameters collection of the Command object cmd.Parameters.Add(prmPostpre)End Ifcnn.Open'This is the line that'll actually call the stored procedureSet rstProperty = cmd.Execute()cnn.CloseIf rstProperty.BOF And rstProperty.EOF Then 'If BOF And EOF are true then this is an empty recordset - we got no records back Response.Write "No records returned"Else 'We have records so write them out into a table Response.Write "<table><tr>" For i = 0 To rstProperty.Fields.Count - 1 Response.Write "<td>" Response.Write rstProperty.Fields(i).Name Response.Write "</td>" Response.Write "<td> </td>" Next Response.Write "</tr>" Do While rstProperty.Eof = False Response.Write "<tr>" For i = 0 To rstProperty.Fields.Count - 1 Response.Write "<td>" Response.Write rstProperty.Fields(i).Value Response.Write "</td>" Next Response.Write "<td>" Response.Write "<a href='ViewDetails.asp?id='" & rstProperty.Fields("PropertyId").Value & "'>View Details for this property</a>" Response.Write "</td>" Response.Write "</tr>" rstProperty.MoveNext Loop Response.Write "</table>"End If这 应该 适用于任何参数组合,无论您是不输入参数,输入部分参数还是全部输入!



