Help with dynamic connection string
-
I am working on this program where i need to supply the name of the server at runtime so as to dynamically build the connection string.. what I am saying is that..the program will have a initial setup screen, so the user can supply the name of the server... how do i go about doing that..presently i am using local and at times i use the name of my server..but i need to use the program on different computer on a network, which i am not sure of the server name at this moment...can anyone help.. I hope this was clear.. i am using sql server 2000 and vb.net 2003 as my front end...
Nab
-
I am working on this program where i need to supply the name of the server at runtime so as to dynamically build the connection string.. what I am saying is that..the program will have a initial setup screen, so the user can supply the name of the server... how do i go about doing that..presently i am using local and at times i use the name of my server..but i need to use the program on different computer on a network, which i am not sure of the server name at this moment...can anyone help.. I hope this was clear.. i am using sql server 2000 and vb.net 2003 as my front end...
Nab
-
I am working on this program where i need to supply the name of the server at runtime so as to dynamically build the connection string.. what I am saying is that..the program will have a initial setup screen, so the user can supply the name of the server... how do i go about doing that..presently i am using local and at times i use the name of my server..but i need to use the program on different computer on a network, which i am not sure of the server name at this moment...can anyone help.. I hope this was clear.. i am using sql server 2000 and vb.net 2003 as my front end...
Nab
There's nothing special about building a connection string. It's a normal string just like any other and can be built using the exact same methods.
Dim connStr As String = String.Format("Data Source={0};Initial Catalog.....", serverName)
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Not sure what you are into but here is a pretty handy website to get the connection strings link Are you using the application blocks from MS or are you using your own.
Ok thank you guys. But can any one help me with this? My program would have a initial setup screen where the system admin will suply all the necessary informatoin such as the server name, pasword, username and stuff like that... all this information would be written to a text file somewhere on the hard drive. The program should then take the server name, user name and password from the text file to create the connection string to connect to the database (sql 2000) How do i go about using the information from the text file to create my connection string so that my program can use it? I can't program the connection string in the project because i don't know the name of the server they will be using and they might need to put it on other servers.. Please help!!
Nab
-
Ok thank you guys. But can any one help me with this? My program would have a initial setup screen where the system admin will suply all the necessary informatoin such as the server name, pasword, username and stuff like that... all this information would be written to a text file somewhere on the hard drive. The program should then take the server name, user name and password from the text file to create the connection string to connect to the database (sql 2000) How do i go about using the information from the text file to create my connection string so that my program can use it? I can't program the connection string in the project because i don't know the name of the server they will be using and they might need to put it on other servers.. Please help!!
Nab
Ok here you go but let me tell you up front that I would never do this in any production code Ever and here is why. If you persist the connection to a local file you can/will get hacked and you now have the responsiblity of maintaining or validating the contents of the file. So based on this don't use this code in production applications it is to dangerous and you don't need to there are better options. If the user has to enter the server username and password just hold it in a member variable then all of the problems go away. This is a test form to prove the concept Public Class Form1 ' Member sqlConnection variable Private mConnString As Data.SqlClient.SqlConnection Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Gives you the path of the file to write Dim csExample As New connectionstringexample("C:\connectString.txt") ' Builds the structure and stores the data to a file csExample.PersistConnectionString(New ConnectionStringStuct("myServerAddress;myDataBase;myUsername;myPassword;")) Try ' Instantiates the new connection string using the data retrieved from the file mConnString = New System.Data.SqlClient.SqlConnection(csExample.GetConnectionString()) Catch ex As Exception Throw New ApplicationException(ex.Message) End Try End Sub End Class I used a structure to hold the data because it keeps everything by value not by reference. It has two fields one with a formatted connectionstring and one that is the raw delimited data which should be encrypted. Public Structure ConnectionStringStuct ' Real work is done here Sub New(ByVal RawStringIn As String) Dim lStringParts As String() = RawStringIn.Split(";") ' Put any validation code here ' Buids a complete connection string mConnectionString = "Data Source=" + lStringParts(0) + ";" _ + "Initial Catalog=" + lStringParts(1) + ";" _ + "User=" + lStringParts(2) + ";" _ + "Password=" + lStringParts(3) + ";" mRawString = lStringParts(0) + ";" _ + lStringParts(1) + ";" _ + lStringParts(2) + ";" _ + lStringParts(3) End Sub Private mConnectionString As String Private mRawString As String Public ReadOnly Property ConnectionString() Get Return mConnectionString End Get End Property Pub