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

Posted on 2006-04-08
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.

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?

Question by:twibblejaway
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    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.

    Author Comment

    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?
    LVL 44

    Accepted Solution

    daDataAdapter.SelectCommand.CommandText = "SELECT Title, Image WHERE Cast LIKE '" +  temp + "' FROM Movie";


    Author Comment

    indeed.  That did it.

    LVL 44

    Expert Comment

    glad to help.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now