Solved

Referencing SQL Database Location in VB 2008

Posted on 2009-07-01
4
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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