Solved

Referencing SQL Database Location in VB 2008

Posted on 2009-07-01
4
156 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now