打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Showing a Connection String prompt in a WinForm application
Showing a Connection String prompt in a WinForm application

When I was putting together the Data Dictionary Creator program, I needed to allow users to input a connection string. A lot of winform applications that require data connections give you a textbox and tell you to figure it out yourself, but I really wanted to show a prompt that let you test your connections, select databases on a server, etc.

I was surprised that I couldn‘t find a nice, free, winform control to build a connection string. I found something on CodeProject with a smart solution - it pops up the standard Data Link Properties dialog. The code mostly worked, but had some problems (passwords were lost if user didn‘t check "Allow Saving Password", problems with integrated security). I found some general suggestions in the comments and newsgroup postings and threw in some exception handling, and came up with something that worked pretty well for me. It‘s a little trickier than it looks, since setting certain properties in just the right order shifts the dialog into modes which hide tabs and stuff.

This requires references to a few COM objects:

%PROGRAMFILES%\Microsoft.NET\Primary Interop Assemblies\adodb.dll
%PROGRAMFILES%\Common Files\System\Ole DB\OLEDB32.DLL

I found an article about using CreateObject or GetTypeByProgID to avoid the interop references, I think I‘d rather ship the interop stubs than worry about dynamic object calls failing when I‘m shipping an application, but it‘s interesting.

The next step for this would be bundle this up in a user control (textbox with an ellipses button to launch the dialog, maybe a balloon tip to show connection errors), but I‘m not sure I‘ll get to that any time soon. Here‘s the code as a simple function that pops the dialog and returns the selected connection string: 

/// <summary>/// Displays a Connection String Builder (DataLinks) dialog.////// Credits:/// http://www.codeproject.com/cs/database/DataLinks.asp/// http://www.codeproject.com/cs/database/DataLinks.asp?df=100&forumid=33457&select=1560237#xx1560237xx////// Required COM references:/// %PROGRAMFILES%\Microsoft.NET\Primary Interop Assemblies\adodb.dll/// %PROGRAMFILES%\Common Files\System\Ole DB\OLEDB32.DLL/// </summary>/// <param name="currentConnectionString">Previous database connection string</param>/// <returns>Selected connection string</returns>private string PromptForConnectionString(string currentConnectionString){MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();ADODB.Connection dialogConnection;string generatedConnectionString = string.Empty;if (currentConnectionString == String.Empty){dialogConnection = (ADODB.Connection)dataLinks.PromptNew();generatedConnectionString = dialogConnection.ConnectionString.ToString();}else{dialogConnection = new ADODB.Connection();dialogConnection.Provider = "SQLOLEDB.1";ADODB.Property persistProperty = dialogConnection.Properties["Persist Security Info"];persistProperty.Value = true;dialogConnection.ConnectionString = currentConnectionString;dataLinks = new MSDASC.DataLinks();object objConn = dialogConnection;if (dataLinks.PromptEdit(ref objConn)){generatedConnectionString = dialogConnection.ConnectionString.ToString();}}generatedConnectionString = generatedConnectionString.Replace("Provider=SQLOLEDB.1;", string.Empty);if (!generatedConnectionString.Contains("Integrated Security=SSPI")&& !generatedConnectionString.Contains("Trusted_Connection=True")&& !generatedConnectionString.Contains("Password=")&& !generatedConnectionString.Contains("Pwd="))if(dialogConnection.Properties["Password"] != null)generatedConnectionString += ";Password=" + dialogConnection.Properties["Password"].Value.ToString();return generatedConnectionString;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
ADODB.Connection 错误 '800a0e7a'
VB 实现excel数据导入ACCESS示例
向ACCESS大批量快速插入数据的方法
创建adodb.connection方法
winform实现重新启动程序的方法
ADODB.Connection Automation Error
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服