how to code vb.net
connection string support
to all computers? normally we are puting
connction string like this.
con = New SqlConnection
but the thing is when we are
going to put this application
to another computer we
have a trouble. Now what to do? . How solve this problem. thanks for reading.hope your help.
But what if the database is not on the same machine? That's why it's always better to have the connection string in the app.config. In my opnion, the best way to use connection string is: On app.config: ... And in the code: ... This way multiple developers can use the same config file(in case you use SVN) and also the deploy connection string, without having to change it every time. This works if the DB…
Can you be more specific as to what kind of trouble you are having? If it is a connection problem related to security you may have to set up a database account with a userid and password and change your connection string to use those.
I would also imagine his problem is with security. His current connection string uses trusted security (based on the current logged in user). If the database is on another computer then the current security model will likely not allow the user to connect.
I believe the OP is concerned about getting the proper SQL Express server name on an unknown installation.
The following code works fine with SQL Express running on the machine. I have never tested it to see if it finds a Network server. When I still had SQLExpress 2005 and 2008 installed, it found both instances.
Dim csb As New SqlClient.SqlConnectionStringBuilder
csb.IntegratedSecurity = True
csb.UserInstance = True
' This next line can take a few seconds to complete
Dim dtSQLServers As DataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources()
If dtSQLServers.Rows.Count > 0 Then ' Take 1st entry. Could loop through all entries if needed
Dim ServerName As String = dtSQLServers.Rows.Item(0).Field(Of String)("ServerName")
Dim InstanceName As String = dtSQLServers.Rows.Item(0).Field(Of String)("InstanceName")
csb.DataSource = ServerName & "\" & InstanceName
Catch ex As Exception
End If 'dtSQLServers.Rows.Count > 0
If csb.DataSource IsNot Nothing Then ' List the available database
Dim conn As New SqlClient.SqlConnection(csb.ConnectionString)
Dim cmd As New SqlClient.SqlCommand("Select name From sys.databases", conn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim tables As New DataTable
For Each dr As DataRow In tables.Rows
Hi , AleMontero
Please tell me how to change this line according to your settings so thAT I CAN INSTALL IT ON ANOTHER COMPUTER .i TRIED TO INSTALL MY APPLICATION ON ANOTHER COMPUTER USING WINDOWS 7 64 BIT.tHE APPLICATION WORKS FINE ON MY COMPUTER USING 32 BIT VINDOWS 7 .