Solved

Referencing SQL Database Location in VB 2008

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

14 Experts available now in Live!

Get 1:1 Help Now