Results 1 to 7 of 7

Thread: How do you creating SQL field names dynamically

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    How do you creating SQL field names dynamically

    I have a table with fields player1Name,player2Name..etc. How does one create the field names dynamically. I tried this..doesn't work obviously.

    Dim sql As String = "SELECT * FROM myTable WHERE tableName = '" + thisTableName + "'"
    Dim cmd As SqlCommand = New SqlCommand(sql, thisConnection)

    Dim dr As SqlDataReader
    dr = cmd.ExecuteReader

    While dr.Read()
    For i As Integer = 1 To 7
    tablePlayerNames(i) = "dr!player" + Str(i) + "Name"
    Next i
    End While

    Thanks

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: How do you creating SQL field names dynamically

    What is the error message when you try executing the command statement?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: How do you creating SQL field names dynamically

    I don't get an error but the line player!Name = "dr!player"+str(i)+"Name" doesn't work.
    Thanks

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: How do you creating SQL field names dynamically

    sorry line is

    tablePlayerNames(i) = "dr!player" + Str(i) + "Name"

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: How do you creating SQL field names dynamically

    Quote Originally Posted by mouseChaser View Post
    sorry line is

    tablePlayerNames(i) = "dr!player" + Str(i) + "Name"
    I do not have VS open but it would seem you want this, assuming you want the value of dr!player and not what you have in the quotes.

    Code:
    tablePlayerNames(i) = CStr(dr!player) & Str(i) & "Name"

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How do you creating SQL field names dynamically

    @Kevin - that's not what the OP is looking to do... what the OP is trying to do is dynamically access the field by name.... player1 player2 player3 etc.... but they are going about it the wrong way.

    @Mouse - Maybe you should read up on the data reader and how it works and what it can do.
    http://msdn.microsoft.com/en-us/libr...atareader.aspx
    http://msdn.microsoft.com/en-us/libr...r_members.aspx

    Because then you would know there is a GetString method that you can pass the index of the column to and get the value back.
    http://msdn.microsoft.com/en-us/libr...sqlstring.aspx
    Code:
    tablePlayerNames(i) = dr.GetString(i)
    There is also an Item collection, where the field name is the key.
    http://msdn.microsoft.com/en-us/library/f01t4cfy.aspx
    Code:
    tablePlayerNames(i) = dr.Item(i).ToString 
    ' -or-
    tablePlayerNames(i) = dr.Item("Player" & i.ToString).ToString
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: How do you creating SQL field names dynamically

    techgnome thanks so much. This worked just wonderfully

    tablePlayerNames(i) = dr.Item("player" & i.ToString & "Name").ToString

    mouseChaser

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