Searching from special row in sql table

Goofy

Member
Joined
Jan 14, 2003
Messages
14
Ok this is the situation. I need to find some key word from my tables row called "NameID". I think i must somehow tell to vb.net that there is a row called "NameID" am i rigth?

but the code would be something like this.

Code:
TableName = variable of currents table name
TextBox1.Text = KeyWord

Dim da as new SqlDataAdapter ("Select * from" & TableName & Where NameID = % & KeyWord & %, MyConnString)
% because the keyword can start or end diccerence like Lotr keyword will find Lotr2 and The Lotr2...

da.Fill(MyDataSet, TableName)
I cant even run this because it gives error about that NameID.
I think im close to right? :)
 
Yes, youre very close, you just forgot to put quote marks properly
around strings to differentiate them from variables or other
keywords.

Assuming TableName and KeyWord are variables, your code
should look like this, with quotes around the actual SQL statements:

Code:
Dim da As New SqlDataAdapter ("Select * from" & TableName & "Where NameID = %" & KeyWord & "%", MyConnString)
 
Closer... try this:
Code:
Dim da As New SqlDataAdapter ("Select * from [" & TableName & "] Where NameID LIKE %" & KeyWord.Replace("", "") & "%", MyConnString)

I put brackets around the TableName in case its a reserved word. I change "NameID = ..." to "NameID LIKE ..." so that your % search would work. I also wrapped the KeyWord with single quotes (SQL standard for strings) and replaced one single quote with two, in case KeyWord has a value like "OMalley" which would generate a SQL error.

Keep in mind that putting the % BEFORE the keyword will cause a tablescan - could be very VERY slow if the table is large. Otherwise its just slow :)
If you can, only use the % at the end which wont be nearly as bad. Its fairly standard to assume that you must type the *first* few letters when doing searches in applications. Doing "full text searches" arent strange, but less common as they will cause the aforementioned slowness :)

-Nerseus
 
Thanks. works fine now.

Code:
Dim da As New SqlDataAdapter("Select * from [" & TableName & "] Where NameID LIKE " & KeyWord.Replace("", "") & "%", myconnstring)
da.Fill(MyDataSet, TableNAme
 
Back
Top