Solved

Referencing SQL Database Location in VB 2008

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Footer for each row on Gridview 2 30
VB.Net Tranfer data between to VB.Net apps 8 38
Sending receiving text messages in vb.net 15 47
vb.net class 3 21
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

792 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