?
Solved

Using a string  variable in place of 'constant' when doing a SQL query in C#

Posted on 2006-04-08
5
Medium Priority
?
267 Views
Last Modified: 2010-07-27
I have the following line:
daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE Cast LIKE '%Adam Sandler%' FROM Movie";

This works, and returns a list of movie TITLE's which adam sandler stars in.  I want to make it general now, and use a STRING variable instead of a hard-coded constant.

eg:
String temp = "%Adam Sandler%"
 daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE Cast LIKE temp FROM Movie";

When I try this though, the query no longer works.
How can I make this work?

Thanks,
0
Comment
Question by:twibblejaway
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16408884
daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE [Cast] LIKE ? FROM Movie";
 daDataAdapter.SelectCommand.Parameters.Add "tmp", "%Adam Sandler%"


change the parameter value, and the next time the selectcommand runs it will take it.
0
 

Author Comment

by:twibblejaway
ID: 16408908
You are saying I should make my code look like this:

daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE [Cast] LIKE ? FROM Movie"; daDataAdapter.SelectCommand.Parameters.Add "tmp", "%Adam Sandler%";

Wont this still only work for Adam Sandler??
What exactly does the second line do?  What does the ? in the first line do?
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 2000 total points
ID: 16409478
daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE Cast LIKE '" +  temp + "' FROM Movie";

AW
0
 

Author Comment

by:twibblejaway
ID: 16409808
indeed.  That did it.

Thanks.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16409976
glad to help.

AW
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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