Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

'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? :)

  • *Experts*
Posted

Yes, you're 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:

 

Dim da As New SqlDataAdapter ("Select * from" & TableName & "Where NameID = %" & KeyWord & "%", MyConnString)

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

Closer... try this:

Dim da As New SqlDataAdapter ("Select * from [" & TableName & "] Where NameID LIKE '%" & KeyWord.Replace("'", "''") & "%'", MyConnString)

 

I put brackets around the TableName in case it's 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 "O'Malley" 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 it's just slow :)

If you can, only use the % at the end which won't be nearly as bad. It's fairly standard to assume that you must type the *first* few letters when doing searches in applications. Doing "full text searches" aren't strange, but less common as they will cause the aforementioned slowness :)

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks. works fine now.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...