Results 1 to 2 of 2
  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007

    Proper Way of Opening and Closing SQL Server connection

    I wish to make an Multi User (Over LAN Or WAN) application. Presently for single User/Instance application i am connecting to the SQL Database as an when required, in the following manner

    vb Code:
    1. Using Conn as New SQLConnection(My.Settings.MyConnectionString)
    2.     Using Cmd as New SqlCommand
    3.           Try
    4.               ...
    5.          Catch
    6.   End Using
    7. End Using

    I Wish to Clarify, if this is the Proper Way of connecting to DB in case of Single User/Instance, and Can it be used in case of multi user. (As it will require connecting and disconnecting to DB multiple Times)

    Or Should I Open the Connection When the Program Starts and Close it When the Program Closes.

    Plz Suggest


  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Proper Way of Opening and Closing SQL Server connection

    Whether or not it is multi-user makes absolutely no difference to how you connect to a database. The only difference is that you need to consider concurrency issues. ADO.NET uses optimistic concurrency by default and you should make use of that unless you have a specific reason not to. I suggest that you read up on optimistic concurrency in general and in relation to ADO.NET specifically.

    By the way, there's no need to nest Using blocks unless you want to execute some other code between the Using statements. A single Using statement can be used to create multiple objects, e.g.
    vb.net Code:
    1. Using Conn as New SQLConnection(My.Settings.MyConnectionString),
    2.       Cmd as New SqlCommand
    3.     Try
    4.         '...
    5.     Catch
    6. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.