在实际的开发中,为了方便软件的部署我们常常需要让客户手动的选择数据源和数据连接信息。当然我们可以将这些信息写在XML的配置文件里.但是客户并不是专业的编程人员,一不小心就会使连接串写错,导致系统无法运行,最好是让用户能够通过一个图形界面来选择,如下图:
这样子,既方便,有不容易出问题。下来我们就看看怎么实现。我的思路就是调用VS自带的工具,由于没有相关的文档,只有通过反汇编了。先添加对Microsoft.Data.ConnectionUI.Dialog.dll的引用,这个assmbly在你的VS2005的安装目录下具体是在D:\Program Files\Microsoft Visual Studio 8\Common7\IDE下边,我的VS安装在D盘,大家可以根据情况去找,调用很简单,下面是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
namespace testConnectionStringUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataConnectionDialog dlg = new DataConnectionDialog();
dlg.DataSources.Add(DataSource.SqlDataSource);
if (DataConnectionDialog.Show(dlg, this) == DialogResult.OK)
{
textBox1.Text = dlg.ConnectionString;
}
}
}
}
解释一下这段很简单的代码,我创建了一个WindowsApplication,其中有一个窗体,在窗体上放置了一个TextBox和一个Button
控件,在Button的点击事件里添加数据源选择对话框的调用。DataConnectionDialog 的调用比较有意思,虽然DataConnectionDialog提供了Show和ShowDialog实例方法,但是你却不能调用,你只能调用Show静态方法,Show方法有两个重载版本:
public static DialogResult Show(DataConnectionDialog dialog);
public static DialogResult Show(DataConnectionDialog dialog, IWin32Window owner);
public static DialogResult Show(DataConnectionDialog dialog)
{
return DataConnectionDialog.Show(dialog, null);
}
下边是第二个方法的实现:
public static DialogResult Show(DataConnectionDialog dialog, IWin32Window owner)
{
DialogResult result2;
if (dialog == null)
{
throw new ArgumentNullException("dialog");
}
if (dialog.DataSources.Count == 0)
{
throw new InvalidOperationException(SR.GetString("DataConnectionDialog_NoDataSourcesAvailable"));
}
foreach (DataSource source1 in dialog.DataSources)
{
if (source1.Providers.Count == 0)
{
throw new InvalidOperationException(SR.GetString("DataConnectionDialog_NoDataProvidersForDataSource", new object[] { source1.DisplayName.Replace("'", "''") }));
}
}
Application.ThreadException += new ThreadExceptionEventHandler(dialog.HandleDialogException);
dialog._showingDialog = true;
try
{
DialogResult result1;
if ((dialog.SelectedDataSource == null) || (dialog.SelectedDataProvider == null))
{
DataConnectionSourceDialog dialog1 = new DataConnectionSourceDialog(dialog);
dialog1.Title = dialog.ChooseDataSourceTitle;
dialog1.HeaderLabel = dialog.ChooseDataSourceHeaderLabel;
(dialog1.AcceptButton as Button).Text = dialog.ChooseDataSourceAcceptText;
if (dialog.Container != null)
{
dialog.Container.Add(dialog1);
}
try
{
if (owner == null)
{
dialog1.StartPosition = FormStartPosition.CenterScreen;
}
dialog1.ShowDialog(owner);
if ((dialog.SelectedDataSource == null) || (dialog.SelectedDataProvider == null))
{
return DialogResult.Cancel;
}
goto Label_015B;
}
finally
{
if (dialog.Container != null)
{
dialog.Container.Remove(dialog1);
}
dialog1.Dispose();
}
}
dialog._saveSelection = false;
Label_015B:
if (owner == null)
{
dialog.StartPosition = FormStartPosition.CenterScreen;
}
Label_0165:
result1 = dialog.ShowDialog(owner);
if (result1 == DialogResult.Ignore)
{
DataConnectionSourceDialog dialog2 = new DataConnectionSourceDialog(dialog);
dialog2.Title = dialog.ChangeDataSourceTitle;
dialog2.HeaderLabel = dialog.ChangeDataSourceHeaderLabel;
if (dialog.Container != null)
{
dialog.Container.Add(dialog2);
}
try
{
if (owner == null)
{
dialog2.StartPosition = FormStartPosition.CenterScreen;
}
result1 = dialog2.ShowDialog(owner);
goto Label_0165;
}
finally
{
if (dialog.Container != null)
{
dialog.Container.Remove(dialog2);
}
dialog2.Dispose();
}
}
result2 = result1;
}
finally
{
dialog._showingDialog = false;
Application.ThreadException -= new ThreadExceptionEventHandler(dialog.HandleDialogException);
}
return result2;
}
在我编写的代码里面只提供了sql server的数据源,你还可以使用别的数据源或者同时使用多个数据源,如果你不会使用反汇编的话,我可以把其余的数据源列出来以供你使用:OdbcDataSource,OracleDataSource,SqlFileDataSource,AccessDataSource,SqlDataSource
运行程序,点击按钮,选择数据,选择相应的provider,填写数据源的相关信息,然后点击OK,现在数据源连接串出现在了TextBox里了。
你可以利用这个来做个小工具,辅助自己平时的日常配置工作。如果你要发布产品,你可以从中学习它的制做思路,来做自己的数据源选择对话框。