 |
|
 |
Dim objDataConnectionDialog As DataConnectionDialog
objDataConnectionDialog = New DataConnectionDialog
DataSource.AddStandardDataSources(objDataConnectionDialog)
If DataConnectionDialog.Show(objDataConnectionDialog) = Windows.Forms.DialogResult.OK Then
messagebox.show(objDataConnectionDialog.ConnectionString)
End If
objDataConnectionDialog.Dispose()
|
|
|
|
 |
|
 |
'Update the settings
My.MySettings.Default.Item("UserConnect")=DLG.ConnectionString
should be
'Update the settings
My.MySettings.Default.Item("AppConnect")=DLG.ConnectionString
Anyone know of adding a Choose Data Source Dialog?
|
|
|
|
 |
|
 |
‘User Connect Form for Database connection
‘Settings = AppConnect <connectionString> as application setting
‘Settings = UserConnect <Setting><value>Data Source=</value> as User String
'1. Start out with an empty Class Library project
'2. Create dialog = dlg_UserConnect
'3. Add Button = User Connect
'4. In properties Name Button = UserConnectionButton
'5. Add a dialog Form to the project
'6. Name = SQLConnectionDialog
'7. Start position = CenterScreen
'8. Move the OK Cancel button to the lowest leftmost corner and anchor Bottom, Left
'9. Replace the forms code with the following:
??? In your application, turn off Visual Studio hosting process in Debugging so *.exe.config will be used instead of *.vshost.exe.config which gets overwritten by app.config everytime you compile. ????
I found that enabling “Visual Studio hosting process” the New User Connection String is saved and will remain so until changed
The my.settings connection string should at least have Data Source= in it so the *.exe.config file has the connection string info. If it doesn't exist already, the SaveChange_To_App_Config method will not add it and no changes will be saved.
I created a folder in the project file to store the reference .dll
Eg. C:\Project\......\AppRef
The app or DLL needs to reference Microsoft.Data.ConnectionUI.dll and Microsoft.Data.ConnectionUI.Dialog.dll. Both are found in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\.
‘Add Code:
‘Imports System.Windows.Forms
Public Class dlg_UserConnect
Private Sub OK_Button_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub UserConnection_Button_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles UserConnectionButton.Click
'UserConnect = the name in my.settings to the user string
Dim DLG As New SQL_Connection_Dialog.SQL_Connection_Dialog
DLG.ConnectionString = My.Settings.UserConnect
DLG.Title = "User Connection"
If DLG.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
Dim CS As String = DLG.ConnectionString
DLG.SaveChange_To_App_Config("UserConnect")
'Update the settings
My.MySettings.Default.Item("UserConnect")=DLG.ConnectionString
End Sub
End Class
‘Create Class named = SQL_Connection_Dialog
Imports System.Configuration
Public Class SQL_Connection_Dialog
Public Class SQL_Connection_Dialog
Private _Frm_SQLConnectionDialog As SQLConnectionDialog
Public Sub New()
MyBase.New()
_Frm_SQLConnectionDialog = New SQLConnectionDialog
End Sub
Public Property Title() As String
Get
Return Me._Frm_SQLConnectionDialog.Text
End Get
Set(ByVal value As String)
Me._Frm_SQLConnectionDialog.Text = value
End Set
End Property
Public Property ConnectionString() As String
Get
Return Me._Frm_SQLConnectionDialog.ConnectionString
End Get
Set(ByVal value As String)
Me._Frm_SQLConnectionDialog.ConnectionString = value
End Set
End Property
Public Sub SaveChange_To_App_Config(ByVal connectionName As String)
Dim Config As Configuration
Dim Section As ConnectionStringsSection
Dim Setting As ConnectionStringSettings
Dim ConnectionFullName As String
'There is no inbuilt way to change application
'setting values in the config file.
'So that needs to be done manually by calling config section object.
Try
'Concatenate the full settings name
'This differs from Jakob Lithner. Runtime Connection Wizard
'The ConnectionFullName needs to
'refer to the Assembly calling this DLL
ConnectionFullName = String.Format("{0}.MySettings.{1}", _
System.Reflection.Assembly.GetCallingAssembly. _
EntryPoint.DeclaringType.Namespace, _
connectionName)
'Point out the objects to manipulate
Config = ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
Section = CType(Config.GetSection("connectionStrings"), _
ConnectionStringsSection)
Setting = Section.ConnectionStrings(ConnectionFullName)
'Ensure connection setting is defined
'(Note: A default value must be set to save the
'connection setting!)
If IsNothing(Setting) Then Throw New Exception( _
"There is no connection with this name" + _
" defined in the config file.")
'Set value and save it to the config file
'This differs from Jakob Lithner. Runtime Connection Wizard
'We only want to save the modified portion of the config file
Setting.ConnectionString = Me.ConnectionString
Config.Save(ConfigurationSaveMode.Modified, True)
Catch ex As Exception
End Try
End Sub
Public Function ShowDialog() As System.Windows.Forms.DialogResult
Return Me._Frm_SQLConnectionDialog.ShowDialog
End Function
End Class
End Class
‘Create Dialog named = SQLConnectionDialog
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports Microsoft.Data.ConnectionUI
Imports System.Data.SqlClient
Friend Class SQLConnectionDialog
Private cp As SqlFileConnectionProperties
Private uic As SqlConnectionUIControl
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
cp = New Microsoft.Data.ConnectionUI.SqlFileConnectionProperties
uic = New Microsoft.Data.ConnectionUI.SqlConnectionUIControl
uic.Initialize(cp)
End Sub
'Allows the user to change the title of the dialog
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
'Pass the original connection string or get the
'resulting connection string
Public Property ConnectionString() As String
Get
Return cp.ConnectionStringBuilder.ConnectionString
End Get
Set(ByVal value As String)
cp.ConnectionStringBuilder.ConnectionString = value
End Set
End Property
Private Sub OK_Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub Dialog1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Padding = New Padding(5)
Dim adv As Button = New Button
Dim Tst As Button = New Button
'Size the form and place the uic, Test connection button,
'and advanced button
uic.LoadProperties()
uic.Dock = DockStyle.Top
uic.Parent = Me
Me.ClientSize = Size.Add(uic.MinimumSize, New Size(10, _
(adv.Height + 25)))
Me.MinimumSize = Me.Size
With adv
.Text = "Advanced"
.Dock = DockStyle.None
.Location = New Point((uic.Width - .Width), (uic.Bottom + 10))
.Anchor = (AnchorStyles.Right Or AnchorStyles.Top)
AddHandler .Click, AddressOf Me.Advanced_Click
.Parent = Me
End With
With Tst
.Text = "Test Connection"
.Width = 100
.Dock = DockStyle.None
.Location = _
New Point((uic.Width - .Width) - adv.Width - 10, _
(uic.Bottom + 10))
.Anchor = (AnchorStyles.Right Or AnchorStyles.Top)
AddHandler .Click, AddressOf Me.Test_Click
.Parent = Me
End With
End Sub
Private Sub Advanced_Click(ByVal sender As Object, ByVal e As EventArgs)
'Set up a form to display the advanced connection properties
Dim frm As Form = New Form
Dim pg As PropertyGrid = New PropertyGrid
pg.SelectedObject = cp
pg.Dock = DockStyle.Fill
pg.Parent = frm
frm.ShowDialog()
End Sub
Private Sub Test_Click(ByVal sender As Object, ByVal e As EventArgs)
'Test the connection
Dim conn As New SqlConnection()
conn.ConnectionString = cp.ConnectionStringBuilder.ConnectionString
Try
conn.Open()
MsgBox("Test Connection Succeeded.", MsgBoxStyle.Exclamation)
Catch ex As Exception
MsgBox("Test Connection Failed.", MsgBoxStyle.Critical)
Finally
Try
conn.Close()
Catch ex As Exception
End Try
End Try
End Sub
End Class
|
|
|
|
 |
|
 |
Hi again TWallik,
I deploy with a MS VS2008 Setup an application with your dialog (I modify the code for SQLOLEDB but the code is mostly the same) and in destination PC (W7 Home) I have problems saving config because che app.config is missing.
I start the program, and the dialog read (from where?) the the developer server config, I change the server name and save it, the program access to SQL via SQLOLEDB library but when I close the program the config is still my old config...
Where is my app.config file, the deploy doesn't create anything under AppData or similar.
Alessio Iodice
|
|
|
|
 |
|
 |
After some cleaning of the code I can successfully launch and select the server and database using the dialog.
I have only one problem though when I test connection to the selected database I get a Test Fail dialog. When I Test the sever without the database I get the Test Succeeded.
Is my app.config file not accepting the new information inputed into the dialog and is this the reason the Test Conection Failed msg displayed.
I'm new to VB programming and learning
|
|
|
|
 |
|
 |
Thabks for an EXCELLENT HELP. This dll saved my day. MS has "forgotten" the real life needs....
|
|
|
|
 |
|
|
 |
|
 |
great article well explained ,thanks
|
|
|
|
 |
|
 |
What are SqlFileConnectionProperties and SqlConnectionUIControl? The code implies that they are classes within Microsoft.Data.ConnectionUI. MSDN returns no hits on SqlFileConnectionProperties and a couple of meaningless hits for SqlConnectionUIControl. I'm using .Net 3.5 SP 1 and Visual Studio 2008 with the SDK.
|
|
|
|
 |
|
 |
Hi, I used it and it works fine but...Now I'd like to build something like SQL Connection Dialog but for Oracle....
Any Ideas?
Thanks,
Gianluca
|
|
|
|
 |
|
 |
Hi, TWallick
Thanks, this code has been grate help.
I have found that for some reason the DNS resolver isn't working propertly and even though the Server Name list detects and displays the "server\instance" correctly from client's machine, connection fails. But if I type the "IPAddress\instance", connection succeeds... so i'd better prefere to display IPAddresses in the first place.
Is there any way to display IP Addresses instead Server Name with this control?
Thenks in advance for your help.
Ari
|
|
|
|
 |
|
 |
Hi TWallick,
I try to create a project with the source code you posted here.
1) Create a solution with a project named DBTest (WindowsApplication and Main project) and a project (DLL) named "ConnettiSQL"
2) create a dialog named "SQLConnectionDialog" in ConnettiSQL and Paste the first code in
3) Create a class and named "SQL_Connection_Dialog" in ConnettiSQL and paste the second code in
4) Save
5) Open "references" in DBTest and add "ConnettiSQL" as reference
6) Add a button in DBTest's Form1 with che test part as you posted
7) Changed SQL_Connection_Dialog.SQL_Connection_Dialog in the button code with my ConnettiSQL.SQL_Connection_Dialog
8) save & run.
When I click on button The program stop in Get property at cp.ConnectionStringBuilder.ConnectionString = value and VB say I have to create cp with a "new"
And if I try to create it adding cp=new SqlFileConnectionProperties in the row before the error raise in Dialog1_Load in
uic.LoadProperties()
But if I use your dll everything works!
Any suggestion?
|
|
|
|
 |
|
 |
DOH!!! I found the problem. In my code the new method was in frm_SQL_Connection_Dialog_Designer when it should have been in frm_SQL_Connection_Dialog.
I made the change to the code in this document.
Just add this after the private declares in frm_SQL_Connection_Dialog and it will work just peachy.
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
cp = New Microsoft.Data.ConnectionUI.SqlFileConnectionProperties
uic = New Microsoft.Data.ConnectionUI.SqlConnectionUIControl
uic.Initialize(cp)
End Sub
|
|
|
|
 |
|
 |
Hallo,
I tried to use your SQL Connection Dialog.dll, but when I want to open the file in the toolbox elements dialog, I get the message:
Die Typbibliothek ... SQL Connection Dialog.tlb konnte nicht geladen werden.
(the type library ... SQL Connection Dialog.tlb couldn't be loaded)
What shall I do?
greetings-
Dietrich
|
|
|
|
 |
|
 |
in the Download Compiled_DLL_SQL_Connection_Dialog.zip - 99.3 KB I include...
'Supplied by MS
Microsoft.Data.ConnectionUI.Dialog.dll
Microsoft.Data.ConnectionUI.dll
'My dialog
SQL Connection Dialog.dll
You will not be able to toolbox SQL Connection Dialog.dll or Microsoft.Data.ConnectionUI.dll but you can toolbox Microsoft.Data.ConnectionUI.Dialog.dll.
I tryed to reproduce your error but I can't. The error I get tells me there are no user interface items.
When I added Microsoft.Data.ConnectionUI.Dialog.dll , OOO Ahh , it added all the other Data Connection UI items I have been searching for. like Access, ODBC, OLE, and Oracle.
|
|
|
|
 |
|
 |
From what I can find searching MS website for 'Microsoft.Data.ConnectionUI', you cannot redistribute the .dll. It seems it only comes with Visual Studio.
|
|
|
|
 |
|
|
 |
|
 |
For those that just want to use it. I added the compiled DLL to the article.
I know it can be a pain to actualy get a working version off of some of these articles
|
|
|
|
 |
|
 |
VS 2005 has a tool to do this built in.
Not that it really matters much, as your article is a very nice exercise.
To get to the built in tool:
Go to PROJECT/PROPTERTIES/SETTINGS, select connection string type.
Where there's smoke, there's a Blue Screen of death.
|
|
|
|
 |
|
 |
If your connection was made by creating a Data Source in the Data Source dialog so fields,grids can be dragged to the form, it creates the read only application connection string. Should you change the type to User then the table adapters will no longer use it and tons of compiler errors are created. This can be worked around but it's needlessly time consuming to do it. My attitude is give it what it wants and be done with it.
|
|
|
|
 |
|
 |
Using Data Connection Dialog, can not recreate the test project after download the source code from Data Connection Dialog and recreating the test project, I get the error that "Type SQL_Connection_Dialog is not defined", and I rebuild the class library, any help will be appreciated. Thank you Rambod, software engineer for ABB, Inc.
|
|
|
|
 |
|
 |
Hallo,
when I do your introduction the folowing error occur:
Fehler 1 Typ erwartet. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 31 23 dhSQLDialog
Fehler 2 Der Typ "ConnectionStringsSection" ist nicht definiert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 32 24 dhSQLDialog
Fehler 3 Der Typ "ConnectionStringSettings" ist nicht definiert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 33 24 dhSQLDialog
Fehler 4 Der Name "ConfigurationManager" wurde nicht deklariert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 46 22 dhSQLDialog
Fehler 5 Der Name "ConfigurationUserLevel" wurde nicht deklariert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 46 64 dhSQLDialog
Fehler 6 Der Typ "ConnectionStringsSection" ist nicht definiert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 47 69 dhSQLDialog
Fehler 7 Der Name "ConfigurationSaveMode" wurde nicht deklariert. C:\dhVBnet\SQLConnectionDialog\dhSQLDialog\SQL_Connection_Dialog.vb 57 25 dhSQLDialog
Should I translate the German error descriptions?
Greetings
Dietrich
|
|
|
|
 |
|
 |
Yea, I can't figure this one out.
Did you make reference to Microsoft.Data.ConnectionUI.dll and Microsoft.Data.ConnectionUI.Dialog.dll Both are found in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\
|
|
|
|
 |
|
 |
In the mornung I had found it. I had to set the preference to system.configuration!!
But now there is only one error in the testform code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'TestConn = the name in my.settings to the connection string
Dim DLG As New SQL_Connection_Dialog.SQL_Connection_Dialog
DLG.ConnectionString = My.Settings.TestConn
DLG.Title = "Test Connection"
If DLG.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
Dim CS As String = DLG.ConnectionString
DLG.SaveChange_To_App_Config("TestConn")
'Update the settings
My.MySettings.Default.Item("TestConn") = DLG.ConnectionString
End Sub
Error:
Fehler 1 Der Typ "SQL_Connection_Dialog.SQL_Connection_Dialog" ist nicht definiert. C:\dhVBnet\SQLConnectionDialog\dhTeszSQLConn\Form1.vb 5 24 dhTestSQLConn
I had copied your code from this article here...
Dietrich
|
|
|
|
 |
|
 |
Hallo,
hope you understand some German...
When I start the Testproject the folowing errors occur:
Warnung 1 Fehler beim Laden der Eigenschaft "OutputPath". Der eingegebene Pfad ist kein gültiger Ausgabepfad. SQL Connection Dialog
Fehler 2 Der Typ "SQL_Connection_Dialog.SQL_Connection_Dialog" ist nicht definiert. G:\dhVBnet\Temp\SQL Connection Dialog Test\SQL Connection Dialog Test\Form1.vb 9 24 SQL Connection Dialog Test
Fehler 3 Das Verzeichnis P:\CMNFRMS\DLL\SQL Connection Dialog\ kann nicht erstellt werden. Ein Teil des Pfades P:\CMNFRMS\DLL\SQL Connection Dialog\ konnte nicht gefunden werden. SQL Connection Dialog
What's wrong?
Please, when you can't translate this, tell me and I will do it for you!
Greetings
Dietrich
|
|
|
|
 |