Hi,

I have done my vb.net application and ready for deployment, my database is located somewhere in my drive c:, i used this code to get connection string from app.config

 Public Sub main()

        constr = System.Configuration.ConfigurationManager.ConnectionStrings("WindowsApplication7.My.MySettings.nameConnectionString").ToString
        Con = New SqlConnection(constr)
        Try
            Con.Open()
            'MsgBox("Server Connection is Open ! ")
            'frmMain.Connection.Text = "Connecting To serv``er"
            Con.Close()
        Catch ex As Exception
            MsgBox("Sorry Can not open connection ! ", vbCritical, "Database")
        End Try

    End Sub

and my app.config has this code

<?xml version="1.0"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsApplication7.My.MySettings.nameConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DATA\database.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>

On my local machine when i tried to run my application, it cannot find the database save to c:.
The database was embeded on my application folder before, since i will install it to different computers, I decided to remove it and save somewhere in c:, how will i modify my code such that even in different computers they still can access my database?

thank you so much for any help

In order for the other computers on the network to access the DB hosted on your computer, you will need to leave SQLExpress running.

The other machines would then use a connection string something like:
connectionString="Data Source=" & YOUR_COMPUTER_NAME & "\SQLEXPRESS;InitialCatalog=" & YOUR_DATABASE_NAME & ";Integrated Security=True;User Instance=True"

Where YOUR_COMPUTER_NAME is the hostname of your computer
AND
YOUR_DATABASE_NAME is the internal name of the database (the name that appears in Server Explorer on your machine with the db attached)

Hi,
Thanks for your post, But I will publish this and make it executable, how will i change it dynamically such that the first time the users use thisapp, it will try to connect to the database, if not connected, it will prompt something to user then ask to select server and database? Im trying to modify the app.config but seems not updating

I'm a bit confused by your explanation, both here and in the original thread.
Try taking a look at the sample project here in the original thread.

Also, it's considered bad form to open a new thread for the same question... please consider closing one of the discussions by marking it as solved with a link to the remaining discussion.

I didn't mark it solved yet coz im still having trouble with this. Sorry for that
From my orig post im focusing on hard coded form (module) to communicate with the database but with some googling, I found that the app.config can be used and can be modified during run time, that's why on this post I focused on using app.config, but during run time it seems that the app.config is not updating. Reading from forums also said that i might having problem with my sql since its express edition. IM still working on upgrading it, uninstalling it then installing the production version (I think if im not mistaken) sadly cant find my installation cd, the product key is there.

App.Config ConnectionStrings are read-only at run-time when using the ConfigurationManager class introduced in .Net 2.0.

Using SQL Express should not be a problem, as long as you specify the instance name along with the server name in your connection string. Ex: WH306UT\SQLExpress

Did you look at the sample project I posted here? There is some code there for building a connection string at run-time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.