Writing SQL Code in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NiteshR
    New Member
    • Mar 2007
    • 21

    Writing SQL Code in C#

    Hi,
    Will someone please help me with this.
    I am using Visual Studio 2005 (C#).
    I would like to write SQL queries in my code and assign it a variable thereafter.
    Would someone please help me with this urgently.
    Thanx.
  • gomzi
    Contributor
    • Mar 2007
    • 304

    #2
    Originally posted by NiteshR
    Hi,
    Will someone please help me with this.
    I am using Visual Studio 2005 (C#).
    I would like to write SQL queries in my code and assign it a variable thereafter.
    Would someone please help me with this urgently.
    Thanx.
    Your query cant be more vague than that man!!

    anyway....
    dim str as string = "select * from blah";

    is this legal??yup.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I find the best method is to use a string and break it into parts.

      string StringVarriable ForLastName="Pl ater";
      string myq="";

      myq="Select ";
      myq+=" C.* ";
      myq+=" FROM ";
      myq+=" CustomersTable AS C ";
      myq+=" WHERE ";
      myq+=" C.LastName='"+S tringVarriableF orLastName+"' ";
      myq+=" AND C.FirstName LIKE 'A' ";
      myq+=";";

      when you have a big querry, I like to seperate it out on lines so I can comment out or re-arrange them later.

      Comment

      • NiteshR
        New Member
        • Mar 2007
        • 21

        #4
        Originally posted by Plater
        I find the best method is to use a string and break it into parts.

        string StringVarriable ForLastName="Pl ater";
        string myq="";

        myq="Select ";
        myq+=" C.* ";
        myq+=" FROM ";
        myq+=" CustomersTable AS C ";
        myq+=" WHERE ";
        myq+=" C.LastName='"+S tringVarriableF orLastName+"' ";
        myq+=" AND C.FirstName LIKE 'A' ";
        myq+=";";

        when you have a big querry, I like to seperate it out on lines so I can comment out or re-arrange them later.
        Hi, this doesnt seem to work.
        when i output the value from the variable 'myq', it shows me the coding text
        as typed.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Are you actually in the dev-enivironment or in some sort of SQL Query creation zone?

          what I typed would be place in your C# code section, if you were in a query creation zone, just type it out.
          The output of my string should look like this

          Select C.* FROM CustomersTable AS C WHERE C.LastName='Pla ter' AND C.FirstName LIKE 'A' ;

          Comment

          Working...