Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i am creating a application in vb2010 and use sql-server2005 as backend.
i want to install it other's computer . for this connection string is a problam becouse i have to change it


i allready create the setup bt it wont work .

my connection string is :
Public sqlc As New SqlClient.SqlConnection("Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;multipleactiveresultsets = true")


so now i want to create a startuo form where i get all the name instance of sql-server2005. so i can replase it with Source=LOCALHOST\SQLEXPRESS;

but i dont know how to do it ?

plz anyone can help me , any type of ex. sampal code or project ?
any sugetions ? plz help . thanks in advance ...........
Posted
Updated 24-May-18 19:59pm
Comments
cocis48 20-Oct-12 2:11am    
Create a new Form with a Combo Box in it and the code here is in VB .NET, to read the servers names that allowed/exposed its name to be published, the SQLBrowser must be running on your pc in order to read the names exposed, assuming that your Combo box name is cmbServers, you need to import this:
Imports Microsoft.SqlServer.Management.Common and maybe other reference, it will tell you when you copy the code. Keep in mind that if you don't see all, means that the ones you don't see are not exposing their names, you call the method below:

Private Sub GetServerList()

Dim Server As String = String.Empty
Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
Dim table As System.Data.DataTable = instance.GetDataSources()

For Each row As System.Data.DataRow In table.Rows
Server = String.Empty
Server = row("ServerName")
If row("InstanceName").ToString.Length > 0 Then
Server = Server & "\" & row("InstanceName")
End If
cmbServers.Items.Add(Server)
Next

cmbServers.SelectedIndex = cmbServers.FindStringExact(Environment.MachineName)


End Sub
 
Share this answer
 
Comments
Vipin Sharma 20-Oct-12 9:51am    
thanks i will try this. but i have a que. is it work when i dont run sql-server ? mean i install bt dont run it when i run my app.
coz it is a app so it is necessary to connecct with db without run sql .
Vipin Sharma 21-Oct-12 0:03am    
Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance

vb give error for Dim instance As SqlDataSourceEnumerator . why ? what refrance i have to use ?
or import ?
cocis48 21-Oct-12 16:13pm    
You must install at least SqlExpress on the local machine in order to find others SQL server instances, click on the word and the little red underscore will let you know what import you are missing. These are references that you need, and they are in this folder for SQL 2008, C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies.
1-Micosoft.SqlServer.ConnectionInfo
2-Micosoft.SqlServer.ConnectionInfoExtended
3-Micosoft.SqlServer.Smo
4-Micosoft.SqlServer.SmoExtended
5-Micosoft.SqlServer.SqlEnum
try on at a time, for what you want you may no need all of them, but you must install Sql express first and the SDK that is where these dll's are.
regards.
Vipin Sharma 22-Oct-12 6:21am    
thanks a lotsss... its works and i am so happy for that...
thanks for help me :)
all i need to just add refrance Imports System.Data.Sql
it really helpfull for me . thank you ....
cocis48 22-Oct-12 23:51pm    
You are welcome!
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
cmbserver.Items.Add(My.Computer.Name.ToString & "\SQLExpress")
cmbserver.Items.Add("Localhost\SQLExpress")
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub GetServerList()

        Dim Server As String = String.Empty
        Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
        Dim table As System.Data.DataTable = instance.GetDataSources()

        For Each row As System.Data.DataRow In table.Rows
            Server = String.Empty
            Server = row("ServerName")
            If row("InstanceName").ToString.Length > 0 Then
                Server = Server & "\" & row("InstanceName")
            End If
            cmbserver.Items.Add(Server)
        Next

        cmbserver.SelectedIndex = cmbserver.FindStringExact(Environment.MachineName)


    End Sub
 
Share this answer
 
Comments
Maciej Los 25-May-18 2:07am    
Why to post a "solution" to already answered question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900