Fundamental help required with SQL connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Fundamental help required with SQL connection

    I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
    server.

    When I create and instance of the server, it has a the format
    machinename/instancename

    This is fine for the developement machine but how does my code connect to
    the server when it is on another machine ? When an instance of MSDE is run
    on the target, it will produce...
    anotherPCname/instancename

    Thanks for any help




  • Dan Guzman

    #2
    Re: Fundamental help required with SQL connection

    The 'Data Source' keyword in the connection string specifies the SQL Server
    instance you want to connect to. A best practice is to store the connection
    string externally (e.g. config file) rather than hard-code it in your app so
    that you can connect to different servers without changing code. Connection
    string examples:

    Local default instance:
    Data Source=ThisServ er;Initial Catalog=MyDatab ase;Integrated Security=SSPI

    Local named instance:
    Data Source=ThisServ er\ThisInstance ;Initial Catalog=MyDatab ase;Integrated
    Security=SSPI

    Remote default instance:
    Data Source=OtherSer ver;Initial Catalog=MyDatab ase;Integrated Security=SSPI

    Remote named instance:
    Data Source=OtherSer ver\OtherInstan ce;Initial Catalog=MyDatab ase;Integrated
    Security=SSPI

    --
    Hope this helps.

    Dan Guzman
    SQL Server MVP

    "David" <david@orbitcom s.com> wrote in message
    news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=blue]
    > I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
    > server.
    >
    > When I create and instance of the server, it has a the format
    > machinename/instancename
    >
    > This is fine for the developement machine but how does my code connect to
    > the server when it is on another machine ? When an instance of MSDE is run
    > on the target, it will produce...
    > anotherPCname/instancename
    >
    > Thanks for any help
    >
    >
    >
    >[/color]


    Comment

    • David

      #3
      Re: Fundamental help required with SQL connection

      Dan,

      Thanks for the reply.

      So, I just need to create a text file with the connection string with say

      the local machine ID and database name. The application loads this at boot

      and

      connects to the named server.

      When I deploy the application, I have the client alter the text file by

      finding

      the machine name and modifying the text to suit ??

      I suppose I could use a Try Catch and have try with the local settings and

      if it fails

      it loads the file in the catch sub (or maybe the string is stored in

      registry).

      One thing I am also unable to find out is. How does MSDE know where the file

      is located on the third party machine ? Should the Initial Catalog =

      Path+dasename ?

      Thanks

      "Dan Guzman" <danguzman@nosp am-earthlink.net> wrote in message
      news:FayEc.70$o D3.17@newsread1 .news.pas.earth link.net...[color=blue]
      > The 'Data Source' keyword in the connection string specifies the SQL[/color]
      Server[color=blue]
      > instance you want to connect to. A best practice is to store the[/color]
      connection[color=blue]
      > string externally (e.g. config file) rather than hard-code it in your app[/color]
      so[color=blue]
      > that you can connect to different servers without changing code.[/color]
      Connection[color=blue]
      > string examples:
      >
      > Local default instance:
      > Data Source=ThisServ er;Initial Catalog=MyDatab ase;Integrated Security=SSPI
      >
      > Local named instance:
      > Data Source=ThisServ er\ThisInstance ;Initial Catalog=MyDatab ase;Integrated
      > Security=SSPI
      >
      > Remote default instance:
      > Data Source=OtherSer ver;Initial Catalog=MyDatab ase;Integrated[/color]
      Security=SSPI[color=blue]
      >
      > Remote named instance:
      > Data Source=OtherSer ver\OtherInstan ce;Initial[/color]
      Catalog=MyDatab ase;Integrated[color=blue]
      > Security=SSPI
      >
      > --
      > Hope this helps.
      >
      > Dan Guzman
      > SQL Server MVP
      >
      > "David" <david@orbitcom s.com> wrote in message
      > news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=green]
      > > I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
      > > server.
      > >
      > > When I create and instance of the server, it has a the format
      > > machinename/instancename
      > >
      > > This is fine for the developement machine but how does my code connect[/color][/color]
      to[color=blue][color=green]
      > > the server when it is on another machine ? When an instance of MSDE is[/color][/color]
      run[color=blue][color=green]
      > > on the target, it will produce...
      > > anotherPCname/instancename
      > >
      > > Thanks for any help
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • John Bell

        #4
        Re: Fundamental help required with SQL connection

        Hi

        To connect to a different instance/server that is running your database then
        the connection string will need to be changed. Quite often this sort of
        thing is held in the registry and you configure it on installation.
        Alternatively you may want to provide a small application to configure the
        settings.

        Get the flexibility you need to use integrated solutions, apps, and innovations in technology with your data, wherever it lives—in the cloud, on-premises, or at the edge.


        John

        "David" <david@orbitcom s.com> wrote in message
        news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=blue]
        > I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
        > server.
        >
        > When I create and instance of the server, it has a the format
        > machinename/instancename
        >
        > This is fine for the developement machine but how does my code connect to
        > the server when it is on another machine ? When an instance of MSDE is run
        > on the target, it will produce...
        > anotherPCname/instancename
        >
        > Thanks for any help
        >
        >
        >
        >[/color]


        Comment

        • David

          #5
          Re: Fundamental help required with SQL connection

          Thanks again for the replies.

          I have tried a hard-coded option of connection and it worked.

          ie.

          TRY
          sqlconnectionst ring = ........ originalmachine name.......pass word=..."
          CATCH
          sqlconnectionst ring = ........ newmachinename. ......password= ..."
          END TRY

          I tried to hook this into a config file but I had trouble(I used the wizard
          in VB.NET to make the connection string and edited it but the area the code
          was placed did not seem to like me playing with the code adding file open
          command etc.

          I will try by completely coding the connection in the main form load
          routine.
          Like this

          TRY
          Load my original instance for development and test
          CATCH
          read registry and see if validconnect string exists
          if so then try to connect else
          prompt user for PC name
          save name to registry
          try to connect
          FINALLY
          give up
          END TRY

          would this approach seem reasonable ?

          "John Bell" <jbellnewsposts @hotmail.com> wrote in message
          news:_FyEc.6675 $%N6.73110644@n ews-text.cableinet. net...[color=blue]
          > Hi
          >
          > To connect to a different instance/server that is running your database[/color]
          then[color=blue]
          > the connection string will need to be changed. Quite often this sort of
          > thing is held in the registry and you configure it on installation.
          > Alternatively you may want to provide a small application to configure the
          > settings.
          >
          > http://www.microsoft.com/sql/msde/te...ntegration.asp
          >
          > John
          >
          > "David" <david@orbitcom s.com> wrote in message
          > news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=green]
          > > I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
          > > server.
          > >
          > > When I create and instance of the server, it has a the format
          > > machinename/instancename
          > >
          > > This is fine for the developement machine but how does my code connect[/color][/color]
          to[color=blue][color=green]
          > > the server when it is on another machine ? When an instance of MSDE is[/color][/color]
          run[color=blue][color=green]
          > > on the target, it will produce...
          > > anotherPCname/instancename
          > >
          > > Thanks for any help
          > >
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • John Bell

            #6
            Re: Fundamental help required with SQL connection

            Hi

            This may help:



            John

            "David" <david@orbitcom s.com> wrote in message
            news:qazEc.7156 2$sj4.10264@new s-server.bigpond. net.au...[color=blue]
            > Thanks again for the replies.
            >
            > I have tried a hard-coded option of connection and it worked.
            >
            > ie.
            >
            > TRY
            > sqlconnectionst ring = ........[/color]
            originalmachine name.......pass word=..."[color=blue]
            > CATCH
            > sqlconnectionst ring = ........ newmachinename. ......password= ..."
            > END TRY
            >
            > I tried to hook this into a config file but I had trouble(I used the[/color]
            wizard[color=blue]
            > in VB.NET to make the connection string and edited it but the area the[/color]
            code[color=blue]
            > was placed did not seem to like me playing with the code adding file open
            > command etc.
            >
            > I will try by completely coding the connection in the main form load
            > routine.
            > Like this
            >
            > TRY
            > Load my original instance for development and test
            > CATCH
            > read registry and see if validconnect string exists
            > if so then try to connect else
            > prompt user for PC name
            > save name to registry
            > try to connect
            > FINALLY
            > give up
            > END TRY
            >
            > would this approach seem reasonable ?
            >
            > "John Bell" <jbellnewsposts @hotmail.com> wrote in message
            > news:_FyEc.6675 $%N6.73110644@n ews-text.cableinet. net...[color=green]
            > > Hi
            > >
            > > To connect to a different instance/server that is running your database[/color]
            > then[color=green]
            > > the connection string will need to be changed. Quite often this sort of
            > > thing is held in the registry and you configure it on installation.
            > > Alternatively you may want to provide a small application to configure[/color][/color]
            the[color=blue][color=green]
            > > settings.
            > >
            > > http://www.microsoft.com/sql/msde/te...ntegration.asp
            > >
            > > John
            > >
            > > "David" <david@orbitcom s.com> wrote in message
            > > news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=darkred]
            > > > I am writing SQL data apps using VB.NET 2003, MSDE is being used as[/color][/color][/color]
            the[color=blue][color=green][color=darkred]
            > > > server.
            > > >
            > > > When I create and instance of the server, it has a the format
            > > > machinename/instancename
            > > >
            > > > This is fine for the developement machine but how does my code connect[/color][/color]
            > to[color=green][color=darkred]
            > > > the server when it is on another machine ? When an instance of MSDE is[/color][/color]
            > run[color=green][color=darkred]
            > > > on the target, it will produce...
            > > > anotherPCname/instancename
            > > >
            > > > Thanks for any help
            > > >
            > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • David

              #7
              Re: Fundamental help required with SQL connection

              I finally have the problem figured out. I needed to ATTACH the database to
              sql server (MSDE2000) using the osql utility.

              Now, would someone be able to explain how I can automate the attachment at
              the customer end.
              Preferably, I would like my VN.NET application install to copy the
              unattached database files and attach them to the MSDE server.

              Thanks


              "David" <david@orbitcom s.com> wrote in message
              news:YNvEc.7121 4$sj4.25491@new s-server.bigpond. net.au...[color=blue]
              > I am writing SQL data apps using VB.NET 2003, MSDE is being used as the
              > server.
              >
              > When I create and instance of the server, it has a the format
              > machinename/instancename
              >
              > This is fine for the developement machine but how does my code connect to
              > the server when it is on another machine ? When an instance of MSDE is run
              > on the target, it will produce...
              > anotherPCname/instancename
              >
              > Thanks for any help
              >
              >
              >
              >[/color]


              Comment

              • chi-soon_x_chang@raytheon.com

                #8
                Re: Fundamental help required with SQL connection

                Hi
                I am totally new to the VB.net, only learn this about two weeks. I
                used codes from MS to create a file upload class then add function to
                store the uploaded filename to a table in SQL server. However I kept
                getting the error message about "cannot connect to database" No matter
                what format I used. I have tried at least two dozen connection stringe
                formats either in Sqlconnection or OleDBConnection . Here is the basic
                formats that I used:
                strCon = "Data Source =localhost;Pass word=;User ID=sa;Initial
                Catalog=Northwi nd" for SqlConnection
                strCon = "Provider=SQLOL EDB.1;User ID=sa;Password= ;Persist Security
                Info=True;Initi al Catalog=Northwi nd;Data Source=FAL-L6388" for
                OleDbConnection .
                I use SQL 7, which was installed by default, i,e only have user ID =
                sa, no password, use the default Northwind database. That's all. My
                Server is local named FAL-L688.
                I have replaced the Data Source with Server= but still give me same
                error. I even used an udl to create the OleDB coonnection and put
                into my string, still NO. I have exhauseted all alternatives. Does
                anyone have clue? Is anyway to debug the trasaction? so i can find out
                what went wrong?

                Comment

                • chi-soon_x_chang@raytheon.com

                  #9
                  Re: Fundamental help required with SQL connection

                  Here is my codes:
                  Imports System.Data.Ole Db
                  Imports System.Configur ation
                  ....
                  Dim oCon As New OleDbConnection
                  Dim oCmd As New OleDbCommand
                  oCon = New OleDbConnection (ConfigurationS ettings.AppSett ings("strCon"))
                  oCon.Open()

                  oCmd.CommandTyp e = CommandType.Sto redProcedure
                  oCmd.CommandTex t = "set_order_atta chments"
                  oCmd.Parameters .Add("@cart_id" , OleDbType.VarCh ar, 15, cart_id)
                  oCmd.Parameters .Add("@attachme nt", OleDbType.VarCh ar, 255,
                  strFilenamesCol lection)
                  oCmd.Connection = oCon
                  Try
                  oCon.Open()
                  oCmd.ExecuteNon Query()
                  ....
                  And I have fololowing setting in the Web.config file under the
                  <configuratio n>
                  <appSettings>
                  <add key="strCon"
                  value="Provider =SQLOLEDB.1;Use r ID=sa;Password= ;Initial
                  Catalog=Northwi nd;Server=(loca l);" />
                  </appSettings>
                  Also tried to use "Data Source=.." and enter my local machine name
                  there.
                  None of them work , came back the same "Non DB connection"

                  C Chang

                  Comment

                  • chi-soon_x_chang@raytheon.com

                    #10
                    Re: Fundamental help required with SQL connection

                    There was a duplicated "oCon.Open( )" in my codes, but removal it does
                    not help. However, I tried to use the OracleCommand with the SQL query
                    script directly and IT WORKS!. Does anyone know why it does not work
                    with the Oracle procedure. I hate to change all my procedure calls to
                    in-line scripts. Besides if there is a complicated procedure then the
                    query becomes a problem.

                    Comment

                    • Erland Sommarskog

                      #11
                      Re: Fundamental help required with SQL connection

                      (chi-soon_x_chang@ra ytheon.com) writes:[color=blue]
                      > There was a duplicated "oCon.Open( )" in my codes, but removal it does
                      > not help. However, I tried to use the OracleCommand with the SQL query
                      > script directly and IT WORKS!. Does anyone know why it does not work
                      > with the Oracle procedure. I hate to change all my procedure calls to
                      > in-line scripts. Besides if there is a complicated procedure then the
                      > query becomes a problem.[/color]

                      I'm getting confused, are you connecting to SQL Server or to Oracle?

                      To go back to your original code, the error message seems like it
                      would from your own code. That is, I cannot really recall any error
                      that says "Cannot connect to database". It would help, if you could
                      get hold of the actual message from the client library. To that end,
                      it would help if you posted more parts of the code, and also if you
                      found out on exactly which statement, things go wrong.

                      Stupid check: you have SQL Server running, haven't you?



                      --
                      Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

                      Books Online for SQL Server SP3 at
                      Get the flexibility you need to use integrated solutions, apps, and innovations in technology with your data, wherever it lives—in the cloud, on-premises, or at the edge.

                      Comment

                      • John Bell

                        #12
                        Re: Fundamental help required with SQL connection

                        Hi

                        In addition to Erlands comments, you may want to move the try block earlier
                        to catch the connection error. Also check out
                        http://www.connectionstrings.com/ for the connection string, I think you
                        should be using Data Source instead of Server. Try using the IP address
                        instead of the name if necessary.

                        John

                        <chi-soon_x_chang@ra ytheon.com> wrote in message
                        news:1103838243 .185277.162240@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                        > There was a duplicated "oCon.Open( )" in my codes, but removal it does
                        > not help. However, I tried to use the OracleCommand with the SQL query
                        > script directly and IT WORKS!. Does anyone know why it does not work
                        > with the Oracle procedure. I hate to change all my procedure calls to
                        > in-line scripts. Besides if there is a complicated procedure then the
                        > query becomes a problem.
                        >[/color]


                        Comment

                        Working...