Results 1 to 10 of 10

Thread: sql query syntax in .net

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    36

    sql query syntax in .net

    i have the following query but it doesnt produe any results:

    Me.OleDbCommand1.CommandText = "Select * From Emp Where Week='WeekString'"

    is this valid syntax when using the WHERE part of a sql query

    WeekString is just a string that takes its value from a txtbox on a form

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: sql query syntax in .net

    Try this:

    Me.OleDbCommand1.CommandText = "Select * From Emp Where Week='" & WeekString & "'"


    BTW, this is the VB6 forum. We're old timers.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    The frog's got the right idea, not that his code is easy to read or anything. You have to enclose a string argument in quotes, which he has done, but you have to look kind of close to count those hash marks. I prefer to surround it with the character code, just for readability, though it is slower to type:

    "SELECT * FROM Emp WHERE Week = " & chr(34) & WeekString & chr(34)

    instead of

    "Select * From Emp Where Week='" & WeekString & "'"

  4. #4
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    I do not think so. According to him WeekString itself is a string and not a variable containing string. So what he is doing is correct.

    "Select * from Blah Where Blah = 'WeekString' " is perfectly fine.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by techyspecy
    I do not think so. According to him WeekString itself is a string and not a variable containing string. So what he is doing is correct.

    "Select * from Blah Where Blah = 'WeekString' " is perfectly fine.
    WeekString is just a string that takes its value from a txtbox on a form

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    OK, so the principle is that if it is a string variable, then it must be enclosed in quotes with a technique like froggies (faster and easier) or mine (slower, but more legible). If it is not a variable, then you don't have to do that, as techyspecy stated.

    That ought to answer the question with due thoroughness.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Good.

    Now let's all live happily ever after.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Not quite EVER after, thanks. I'll get pretty wrinkly eventually.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    36
    thanks guys, both of what u have said makes interessting reading and gives me some stuff to think about.

  10. #10
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    FWIW, IMHO using parameters makes for cleaner and easier to read/maintain code. e.g.:

    VB Code:
    1. cmd.CommandText = "INSERT INTO RadioMessages (Unit, Code, Message, Call, [User]) VALUES (@Unit, @Code, @Message, @Call, @User)"
    2.         cmd.Parameters.Clear()
    3.         cmd.Parameters.Add("@Unit", unitGUID)
    4.         cmd.Parameters.Add("@Code", code)
    5.         cmd.Parameters.Add("@Message", message)
    6.         cmd.Parameters.Add("@Call", callGUID)
    7.         cmd.Parameters.Add("@User", user)
    8.         cmd.ExecuteNonQuery()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width