VB.NET/C# equivalent for SQL T-SQL syntax

Given the following SQLcommend Text as sample:
"select * from customers where cust_id like '%" + textbox1.text.tostring.trim +"&'"
What's the VB.NET/C# equivalent for it as below:
Dim CustID As String
if CustID ??? textbox1.text.tostring.trim
??? is the code you fill in for the T-SQL "like".

Who is Participating?
Use IndexOf method of the string...


IndexOf looks for a string and returns the position of the string. In your case, if it returns 0 or higher, that means the string had a match in it.

There are other methods as well that might be handy... such as SubString, Replace, etc.
What is it you are trying to accomplish ?
ksfokAuthor Commented:
Look for a string that contains a particular string.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Fernando SotoRetiredCommented:
Hi ksfok;

Try it like this:

' Modify the connection string to meet your needs
Dim cnn As New SqlConnection("Data Source=(local);Initial Catalog=YourDB;Integrated Security=SSPI")
Dim cmd As SqlCommand = cnn.CreateCommand()
cmd.CommandText = "SELECT * FROM customers WHERE cust_id like '%" & Textbox1.Text.ToString.Trim & "'"
cmd.CommandType = CommandType.Text
' Other code to use it goes here.


C# code:
int foo = 0;
string textToSearch = this.textBox1.Text;
string findThis = this.textBox2.Text.Trim();
foo = textToSearch.IndexOf(findThis);
MessageBox.Show(string.Format("Position: {0}",foo.ToString()));
VB Code:
Dim foo As Int32
Dim textToSearch = Me.textBox1.Text.Trim()
Dim findThis = Me.textBox2.Text.Trim()
foo = textToSearch.IndexOf(findThis)
MessageBox.Show(String.Format("Position: {0}", foo.ToString()))

Open in new window

ksfokAuthor Commented:
What about:


It depends on what version of the .Net Framework you are using.

Dim foo As Boolean
foo = textToSearch.Contains(findThis);

This is not available in .Net 1.1 framework but it is available I think starting with 2.0, and definately 3.5
If you're using .Net 1.1, you'll have to stick with the

if (textToSearch.IndexOf(findThis) > -1)  then
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.