Hej Hej!Söka i flera databaser
Undrar hur man gör för att kunna söka i flera databaser och flera tabeller??
Koden jag använder för att söka i en databas och en tabell är följande:
<%
Dim Con
Dim SQL
Dim RS
Set Con = Server.CreateObject("ADODB.Connection")
Con.Open "DBQ=" & Server.MapPath("db/news.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
%>
<html>
<head>
<LINK REL=stylesheet HREF="stil.css" TYPE="text/css">
</head>
<body bgcolor="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<div style="position: absolute; top: 5; left: 0; width: 400;">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400">
<tr>
<td width="100%" class="rubrik1" style="border-left-style:none; border-left-width:medium; border-right-style:solid; border-right-width:1; border-top-style:solid; border-top-width:1; border-bottom-style:solid; border-bottom-width:1">SÖK NYHETER</td>
</tr>
<tr>
<td width="100%" style="border-top-style: solid; border-top-width: 1" class="td2">
<form action="sok.asp" method="post">
<input class="form" type="text" name="sokord" size="55"><br>
<input class="knapp" type="submit" value="[sök]">
</form>
</td>
</tr>
<tr>
<td width="100%" class="rum"></td>
</tr>
</table>
</div>
<%
If Request.Form("sokord") <> "" Then
Response.Write "<div style='position: absolute; top: 75; left: 0; width: 400;'>"
Response.Write "<table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=400>"
Response.Write "<tr>"
Response.Write "<td width=100% class=rubrik1 style=border-style: solid; border-width: 1>"
Response.Write "Sökningen efter <i>" & Request.Form("sokord") & "</i> gav följande träffar.<br>" & vbCrLf
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td width=100% style=border-top-style: solid; border-top-width: 1 class=td2>"
SQL = "Select * from news Where Text Like '%" & Request.Form("sokord") & "%' "
SET RS = Con.Execute(SQL)
While Not RS.EOF
Response.Write "<font class=text>"
Response.Write "<b>" & RS("Rubrik") & "</b><br>" & vbCrLf
Response.Write "" & RS("fet_text") & " "
Response.Write "Läs mer!<br><br>"
RS.MoveNext
Wend
RS.Close
Set RS = Nothing
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "</table>"
Response.Write "</div>"
End If
%>
</body>
</html>
<%
Con.Close
Set Con = Nothing
%>
Sv: Söka i flera databaser
Om du vill ansluta till flera databaser samtidigt får du skapa flera con-objekt, ex.
Set Con1 = Server.CreateObject("ADODB.Connection")
Con.Open "DBQ=" & Server.MapPath("db/DATABAS1.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
Set Con2 = Server.CreateObject("ADODB.Connection")
Con.Open "DBQ=" & Server.MapPath("db/DATABAS2.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
Om du vill söka i flera tabeller skapar du flera rs-objekt, ex.
SET RS1 = Con1.Execute(SQL) <---- en tabell i databas 1
SET RS2 = Con1.Execute(SQL) <---- en annan tabell i databas 1
SET RS3 = Con2.Execute(SQL) <---- en tabell i databas 2
SET RS4 = Con2.Execute(SQL) <---- en annan tabell i databas 2
Ett annat tips: Istället för att använda response.write varje gång du skriver HTMLkod till webbläsaren via skriptet kan du köra med flera <% %> enl nedan
<% If Request.Form("sokord") <> "" Then %>
div style='position: absolute; top: 75; left: 0; width: 400;'>
<table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=400>
<tr>
<td width=100% class=rubrik1 style=border-style: solid; border-width: 1>
Sökningen efter <i>" & Request.Form("sokord") & "</i> gav följande träffar.<br><p>
</td>
</tr>
<tr>
<td width=100% style=border-top-style: solid; border-top-width: 1 class=td2>
<%
SQL = "Select * from news Where Text Like '%" & Request.Form("sokord") & "%' "
SET RS = Con.Execute(SQL)
While Not RS.EOF
%>
<font class=text>
<b> <%=RS("Rubrik")%></b><br>
<p><%=RS("fet_text")%>
Läs mer!<br><br>
<%
RS.MoveNext
Wend
RS.Close
Set RS = Nothing
%>
</td>
</tr>
</table>
</div>
<% End If %>
/Mattias Svensson
www.hemekonomi.com - ta en titt =)