|
-
May 17th, 2004, 11:23 PM
#1
Thread Starter
Lively Member
[RESOLVED] Dynamic SQL Challenge...
Here's a dynamic sql command that I wrote to be able to take a string array and use it to match a database entry for any word in that string in a specific column.. What I would like to do and failed horribly at doing is to figure out a way to get this to return a result if any of the words are found in TWO columns.
VB Code:
Public Function ClientFindTorrents(ByVal keywords() As String) As Torrent()
Dim DynamicSQL As New System.Text.StringBuilder( _
"SELECT DISTINCT ID, Time, Tracker, TrackerType, Hash, Section, Name, " & _
"Description, Size, Seeds, Peers, InfoUrl " & _
"FROM AvailableTorrents " & _
"WHERE ")
Dim i As Integer
For i = 1 To keywords.Length
DynamicSQL.Append("Name LIKE '%" + keywords(i - 1) + "%' ")
If Not i = keywords.Length Then DynamicSQL.Append("OR ")
Next
Dim con As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand(DynamicSQL.ToString, con)
Dim r As SqlDataReader
Dim Torrents As New ArrayList
' SQL execution code ommited.
Right now it checks to see if the words can be found in the "Name" column. But what if I wanted it to find words in column "Description" also?
Any ideas?
Thanks,
KT
Last edited by Kt3; May 17th, 2004 at 11:55 PM.
-
May 17th, 2004, 11:55 PM
#2
Thread Starter
Lively Member
Nevermind! Figured it out.
VB Code:
Public Function ClientFindTorrents(ByVal keywords() As String) As Torrent()
Dim DynamicSQL As New System.Text.StringBuilder( _
"SELECT DISTINCT ID, Time, Tracker, TrackerType, Hash, Section, Name, " & _
"Description, Size, Seeds, Peers, InfoUrl " & _
"FROM AvailableTorrents " & _
"WHERE ")
Dim i As Integer
For i = 1 To keywords.Length
DynamicSQL.Append("Name LIKE '%" + keywords(i - 1) + "%' ")
If Not i = keywords.Length Then DynamicSQL.Append("OR ")
Next
DynamicSQL.Append("OR ")
For i = 1 To keywords.Length
DynamicSQL.Append("Description LIKE '%" + keywords(i - 1) + "%' ")
If Not i = keywords.Length Then DynamicSQL.Append("OR ")
Next
System.Diagnostics.Debug.WriteLine(DynamicSQL.ToString)
Dim con As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand(DynamicSQL.ToString, con)
Dim r As SqlDataReader
Dim Torrents As New ArrayList
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|