Insert variables for Server info

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWlrZVM=?=

    #1

    Insert variables for Server info

    Can anyone tell me how to insert variables for server info in a SQL
    connection statement.

    For example, in the following statement:
    ("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" & "Password=xyz;" )

    I tried using the following variables, but it doesn't work.
    '("Server='varS erver';" & "Database='varD atabase';" & "User
    ID='varUserName ';" & "Password='varP assword';")

    In debug mode, I can hover over the variables and it shows the correct
    information but I still get an error.

    Thanks in advance,

    Mike

  • Jack Jackson

    #2
    Re: Insert variables for Server info

    On Mon, 1 Oct 2007 10:11:06 -0700, MikeS
    <MikeS@discussi ons.microsoft.c omwrote:
    >Can anyone tell me how to insert variables for server info in a SQL
    >connection statement.
    >
    >For example, in the following statement:
    >("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" & "Password=xyz;" )
    >
    >I tried using the following variables, but it doesn't work.
    >'("Server='var Server';" & "Database='varD atabase';" & "User
    >ID='varUserNam e';" & "Password='varP assword';")
    >
    >In debug mode, I can hover over the variables and it shows the correct
    >information but I still get an error.
    Anything in a text string is just text. There is no way to embed a
    variable name in a text string and have the variable name evaluated.

    I think the clearest way is to use String.Format:

    String.Format(" Server={0};Data base={1};User ID={2};Password ={3}", _
    New Object() {varServer, varDatabase, varUserName, varPassword})

    You can also use StringBuilder, or just concatenate strings:
    "Server=" + varServer + ";Database= " + varDatabase ...

    Comment

    • rowe_newsgroups

      #3
      Re: Insert variables for Server info

      On Oct 1, 1:11 pm, MikeS <Mi...@discussi ons.microsoft.c omwrote:
      Can anyone tell me how to insert variables for server info in a SQL
      connection statement.
      >
      For example, in the following statement:
      ("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" & "Password=xyz;" )
      >
      I tried using the following variables, but it doesn't work.
      '("Server='varS erver';" & "Database='varD atabase';" & "User
      ID='varUserName ';" & "Password='varP assword';")
      >
      In debug mode, I can hover over the variables and it shows the correct
      information but I still get an error.
      >
      Thanks in advance,
      >
      Mike
      I suspect your trouble is in your string concatenation. Try:

      Dim connectString As String =
      String.Format(" Server={0};Data base={1};User ID={2};Password ={3};",
      varServer, varDatabase, varUserName, varPassword)

      Thanks,

      Seth Rowe

      Comment

      • =?Utf-8?B?TWlrZVM=?=

        #4
        Re: Insert variables for Server info

        Actually that didn't work but I made a couple of changes that did work.

        ("Server=" & varServer & ";" & "Database=" & varDatabase & ";" & "User
        ID=" & varUserName & ";" & "Password=" & varPassword & ";")

        This way I can get the values from the registry and not "hard code" it in
        the program.

        Thanks for your input.

        Mike

        "rowe_newsgroup s" wrote:
        On Oct 1, 1:11 pm, MikeS <Mi...@discussi ons.microsoft.c omwrote:
        Can anyone tell me how to insert variables for server info in a SQL
        connection statement.

        For example, in the following statement:
        ("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" & "Password=xyz;" )

        I tried using the following variables, but it doesn't work.
        '("Server='varS erver';" & "Database='varD atabase';" & "User
        ID='varUserName ';" & "Password='varP assword';")

        In debug mode, I can hover over the variables and it shows the correct
        information but I still get an error.

        Thanks in advance,

        Mike
        >
        I suspect your trouble is in your string concatenation. Try:
        >
        Dim connectString As String =
        String.Format(" Server={0};Data base={1};User ID={2};Password ={3};",
        varServer, varDatabase, varUserName, varPassword)
        >
        Thanks,
        >
        Seth Rowe
        >
        >

        Comment

        • Jack Jackson

          #5
          Re: Insert variables for Server info

          On Mon, 01 Oct 2007 11:11:22 -0700, rowe_newsgroups
          <rowe_email@yah oo.comwrote:
          >On Oct 1, 1:11 pm, MikeS <Mi...@discussi ons.microsoft.c omwrote:
          >Can anyone tell me how to insert variables for server info in a SQL
          >connection statement.
          >>
          >For example, in the following statement:
          >("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" & "Password=xyz;" )
          >>
          >I tried using the following variables, but it doesn't work.
          >'("Server='var Server';" & "Database='varD atabase';" & "User
          >ID='varUserNam e';" & "Password='varP assword';")
          >>
          >In debug mode, I can hover over the variables and it shows the correct
          >information but I still get an error.
          >>
          >Thanks in advance,
          >>
          >Mike
          >
          >I suspect your trouble is in your string concatenation. Try:
          >
          >Dim connectString As String =
          >String.Format( "Server={0};Dat abase={1};User ID={2};Password ={3};",
          >varServer, varDatabase, varUserName, varPassword)
          String.Format takes a maximum of 3 separate replacement arguments. For
          more you have to pass an array.

          Comment

          • rowe_newsgroups

            #6
            Re: Insert variables for Server info

            String.Format takes a maximum of 3 separate replacement arguments. For
            more you have to pass an array.
            Umm... not quite. It takes 3 separate named arguments before you pass
            the values as a ParamArray. Which acts just the same as the "normal"
            String.Format with 3 or less arguments.

            For example:

            ////////////////////
            Module Module1

            Sub Main()

            Dim s As String = String.Format(" {0} {1} {2} {3} {4} {5}",
            "one", "two", "three", "four", "five", "six")
            Console.WriteLi ne(s)

            Console.Read()

            End Sub

            End Module
            ///////////////////

            That passes six arguments in the exact same format as if it were used
            with 3 or less arguments - there is no need to declare a separate
            array of strings and pass it in.

            Thanks,

            Seth Rowe


            Comment

            • Jack Jackson

              #7
              Re: Insert variables for Server info

              On Tue, 02 Oct 2007 11:35:33 -0700, rowe_newsgroups
              <rowe_email@yah oo.comwrote:
              >String.Forma t takes a maximum of 3 separate replacement arguments. For
              >more you have to pass an array.
              >
              >Umm... not quite. It takes 3 separate named arguments before you pass
              >the values as a ParamArray. Which acts just the same as the "normal"
              >String.Forma t with 3 or less arguments.
              >
              >For example:
              >
              >////////////////////
              >Module Module1
              >
              Sub Main()
              >
              Dim s As String = String.Format(" {0} {1} {2} {3} {4} {5}",
              >"one", "two", "three", "four", "five", "six")
              Console.WriteLi ne(s)
              >
              Console.Read()
              >
              End Sub
              >
              >End Module
              >///////////////////
              >
              >That passes six arguments in the exact same format as if it were used
              >with 3 or less arguments - there is no need to declare a separate
              >array of strings and pass it in.
              I see that the code you posted works, but I don't understand why. Can
              you explain? What is ParmArray? I Googled for it and didn't find any
              interesting results.

              Comment

              • Jack Jackson

                #8
                Re: Insert variables for Server info

                On Wed, 03 Oct 2007 03:25:39 -0700, rowe_newsgroups
                <rowe_email@yah oo.comwrote:
                Thank you very much. That is very helpful.

                Comment

                • Jack Jackson

                  #9
                  Re: Insert variables for Server info

                  On Wed, 03 Oct 2007 09:46:51 -0700, Jack Jackson
                  <jacknospam@peb bleridge.comwro te:
                  >On Wed, 03 Oct 2007 03:25:39 -0700, rowe_newsgroups
                  ><rowe_email@ya hoo.comwrote:
                  >>
                  >Thank you very much. That is very helpful.
                  Any idea why String.Format has three overloads for one, two and three
                  replacement arguments? It doesn't seem like those serve any real
                  purpose with the ParamArray overload.

                  Comment

                  • rowe_newsgroups

                    #10
                    Re: Insert variables for Server info

                    Any idea why String.Format has three overloads for one, two and three
                    replacement arguments? It doesn't seem like those serve any real
                    purpose with the ParamArray overload.
                    Fortunately for me, I recalled seeing a question similar to this in
                    the C# newsgroup:



                    Basically, it's either for readability or compatibility with
                    other .Net languages.

                    Thanks,

                    Seth Rowe


                    Comment

                    Working...