Solved

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

Posted on 2008-06-24
7
471 Views
Last Modified: 2008-06-26
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".
Thanks.
 




0
Comment
Question by:ksfok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:LiorRon
ID: 21857556
What is it you are trying to accomplish ?
0
 

Author Comment

by:ksfok
ID: 21857577
Look for a string that contains a particular string.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 50 total points
ID: 21857685
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.

Fernando
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 8

Accepted Solution

by:
Chumad earned 200 total points
ID: 21858190
Use IndexOf method of the string...

textbox1.Text.IndexOf(CustID)

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.
0
 
LVL 2

Assisted Solution

by:larker1
larker1 earned 250 total points
ID: 21858293

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

0
 

Author Comment

by:ksfok
ID: 21867725
What about:

textToSearch.contains(findThis)

?
0
 
LVL 2

Assisted Solution

by:larker1
larker1 earned 250 total points
ID: 21868055
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
...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question