Solved

Referencing SQL Database Location in VB 2008

Posted on 2009-07-01
4
157 Views
Last Modified: 2012-05-07
How can I replace the C: reference line below?
I have a procedure that works, but needs to be adjusted in order to handle future use when a user has the program and database installed in a different location on another computer.
This part below in Code sample
                              =C:\CompanySetup.mdf
is the part I need to be a variable since it will not always be on the C:\  folder.  My VB Database reference are:
        ' CompanyInfoBindingSource
        ' CompanyInfoTableAdapter
        ' CompanySetupDataSet.CompanyInfo
        ' TableAdapterManager1
Thank You
Private Sub StoredProcedureCallDelete()
 
        ' PROJECT_DATA -    Delete all records with a * belonging to current Project Name
        '
        Dim myConnectionString As String = _
            "Data Source=.\SQLExpress;AttachDbFilename=C:\CompanySetup.mdf;Integrated Security=True;User Instance=True;Trusted_Connection=Yes;"
              Using connection As New SqlClient.SqlConnection(myConnectionString)
            connection.Open()
 
            Using command As New SqlClient.SqlCommand("StoredProcedure1", connection)
                command.CommandType = CommandType.StoredProcedure
                ' pass parameter - All records matching this project name will be deleted
                command.Parameters.Add("@Proj_NameLookUp", SqlDbType.Text).Value = PDF_ML_Form.PDF_Proj_Name_lbl.Text
                command.ExecuteNonQuery()
            End Using
        End Using
 
    End Sub

Open in new window

0
Comment
Question by:GarySB
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:shahprabal
ID: 24757544
in your project properties, go to the settings tab and enter a key name (eg: connString) and the value (Data Source=.\SQLExpress;AttachDbFilename=C:\CompanySetup.mdf;Integrated Security=True;User Instance=True;Trusted_Connection=Yes;")
 Then all you have to do to get the value is :
My.Settings.connString
 If you need to change it then you can do so in the config file and won't have to recompile the application.
If you need more info, check out:
http://visualbasic.about.com/od/usingvbnet/a/appsettings.htm
0
 

Author Comment

by:GarySB
ID: 24757799
I placed the new variable into the settings tab and changed the line to:
      Dim myConnectionString As String = My.Settings.connString

Question Please: You said 'If I need to change it'
I want the program to change it during the installation process.  Will the new variable connString  change automatically now depending where the program installs it?  If not it is the same fixed location string in a different place.
0
 
LVL 14

Accepted Solution

by:
shahprabal earned 125 total points
ID: 24758015
Here is how you can create a custom action:
http://social.msdn.microsoft.com/Forums/en-US/winformssetup/thread/2fe842d2-35d1-4878-95a8-dbcbe1851158
Once you have the install location you can just set the settings value the same way you access it.
0
 

Author Closing Comment

by:GarySB
ID: 31598917
Great!!!
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question