打造自己的 C# WinForm 应用程序的 SQL Server 连接配置界面

在C# WinForm 应用程序非常需要一个 SQL Server 连接配置界面,许多时候,因 SQL Server 服务器地址变更或 数据库登录账户 变更引起的连接失败等情况,客户就可能打电话“找麻烦”。既然这样,还不如提供一个可视化的配置界面,并在用户手册中说明使用方法,尽可能避免这种小问题带来的烦恼。为此,我将自己无聊时写的连接配置源码贴出来给初学者参考,以备不时之需!
想必大家都很熟悉VS服务器资源管理器中的【添加连接】对话框吧!下面是它的截图:
再看看我模仿这个对话框打造的WinForm程序SQL Server 连接配置界面:
呵呵!有几分相似吧!需要的朋友可以参考下面的源码。这份源码是完整的,粘贴到VS中即可使用。
窗体源码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using System.Data.Sql;
  10. using System.Data.SqlClient;
  11. using System.Configuration;
  12. namespace CodingMouse.CMCSharpSDK.UI.Forms
  13. {
  14.     /// <summary>
  15.     /// 数据库连接配置界面
  16.     /// </summary>
  17.     public partial class frmConnectionConfig : Form
  18.     {
  19.         #region Private Members
  20.         /// <summary>
  21.         /// SQL Server 连接字符串创建者对象
  22.         /// </summary>
  23.         SqlConnectionStringBuilder _connectionStringBuilder = null;
  24.         /// <summary>
  25.         /// 当前应用程序名称
  26.         /// </summary>
  27.         string _applicationName;
  28.         #endregion
  29.         #region Private Methods
  30.         /// <summary>
  31.         /// 获取 本地网络所有 SQL Server 实例(数据源)
  32.         /// </summary>
  33.         private void GetSqlDataSource()
  34.         {
  35.             // 显示提示信息
  36.             string msg = "正在获取本地网络所有 SQL Server 服务器信息 ...";
  37.             this.toolTip.ToolTipIcon = ToolTipIcon.Info;
  38.             this.toolTip.ToolTipTitle = "请稍候...";
  39.             Point showLocation = new Point(
  40.                 this.lblServer.Left + 2,
  41.                 this.lblServer.Top + this.lblServer.Height);
  42.             this.toolTip.Show(msg, this, showLocation, 1000);
  43.             // 创建 提供了一种枚举本地网络内的所有可用 SQL Server 实例的机制 的实例
  44.             SqlDataSourceEnumerator sdsEnum = SqlDataSourceEnumerator.Instance;
  45.             // 调用 检索包含有关所有可见 SQL Server 2000 或 SQL Server 2005 实例的信息的 DataTable 的方法
  46.             DataTable serverDt = sdsEnum.GetDataSources();
  47.             // 创建新列以拼接 ServerName 以及 InstanceName 列的内容(以此构建连接字符串的 Server / Data Source 项)
  48.             DataColumn dcDataSource = new DataColumn("SqlDataSourceName"typeof(string));
  49.             // 将新列添加到 DataTable
  50.             serverDt.Columns.Add(dcDataSource);
  51.             // 创建新列以用中文方式显示 IsClustered 列内容
  52.             DataColumn dcIsClustered = new DataColumn("IsClusteredCHS"typeof(string));
  53.             // 将新列添加到 DataTable
  54.             serverDt.Columns.Add(dcIsClustered);
  55.             // 遍历 DataTable 并给新列赋予拼接后的值
  56.             foreach (DataRow dataRow in serverDt.Rows)
  57.             {
  58.                 if (!string.IsNullOrEmpty(Convert.ToString(dataRow["InstanceName"])))
  59.                     dataRow["SqlDataSourceName"] = string.Format(@"{0}/{1}",
  60.                         Convert.ToString(dataRow["ServerName"]),
  61.                         Convert.ToString(dataRow["InstanceName"]));
  62.                 else
  63.                     dataRow["SqlDataSourceName"] = string.Format(@"{0}",
  64.                         Convert.ToString(dataRow["ServerName"]));
  65.                 dataRow["IsClusteredCHS"] = 
  66.                     (Convert.ToString(dataRow["IsClustered"]).Trim().ToUpper() == "NO")
  67.                     ? "否" : ((Convert.ToString(dataRow["IsClustered"]).Trim().ToUpper() == "YES"
  68.                     ? "是" : dataRow["IsClustered"]);
  69.             }
  70.             // 如果包含数据行
  71.             if (serverDt.Rows.Count > 0)
  72.             {
  73.                 // 创建窗体数据源封装类实例并封装 DataTable
  74.                 BindingSource source = new BindingSource();
  75.                 source.DataSource = serverDt;
  76.                 // 设置 ComboBox 数据源
  77.                 this.cboSqlDataSource.DataSource = source;
  78.                 this.cboSqlDataSource.DisplayMember = "SqlDataSourceName";
  79.                 this.cboSqlDataSource.ValueMember = "SqlDataSourceName";
  80.                 // 设置 DataGridView 数据源
  81.                 this.dgvServerInfo.DataSource = source;
  82.                 // 设置中文列名
  83.                 this.dgvServerInfo.Columns["SqlDataSourceName"].HeaderText = "服务器名";
  84.                 this.dgvServerInfo.Columns["SqlDataSourceName"].DisplayIndex = 0;
  85.                 this.dgvServerInfo.Columns["ServerName"].HeaderText = "服务器物理名称";
  86.                 this.dgvServerInfo.Columns["ServerName"].DisplayIndex = 1;
  87.                 this.dgvServerInfo.Columns["ServerName"].Visible = false;
  88.                 this.dgvServerInfo.Columns["InstanceName"].HeaderText = "实例名";
  89.                 this.dgvServerInfo.Columns["InstanceName"].DisplayIndex = 2;
  90.                 this.dgvServerInfo.Columns["InstanceName"].Visible = false;
  91.                 this.dgvServerInfo.Columns["IsClustered"].HeaderText = "群集信息";
  92.                 this.dgvServerInfo.Columns["IsClustered"].DisplayIndex = 3;
  93.                 this.dgvServerInfo.Columns["IsClustered"].Visible = false;
  94.                 this.dgvServerInfo.Columns["IsClusteredCHS"].HeaderText = "属于群集";
  95.                 this.dgvServerInfo.Columns["IsClusteredCHS"].DisplayIndex = 4;
  96.                 this.dgvServerInfo.Columns["Version"].HeaderText = "版本";
  97.                 this.dgvServerInfo.Columns["Version"].DisplayIndex = 5;
  98.             }
  99.         }
  100.         /// <summary>
  101.         /// 获取当前服务器上所有数据库名称
  102.         /// </summary>
  103.         private void GetDataBaseName()
  104.         {
  105.             // 显示提示信息
  106.             string msg = string.Format("正在获取服务器 [{0}] 上的数据库信息 ...", cboSqlDataSource.Text.Trim());
  107.             this.toolTip.ToolTipIcon = ToolTipIcon.Info;
  108.             this.toolTip.ToolTipTitle = "请稍候...";
  109.             Point showLocation = new Point(
  110.                 this.lblServer.Left + 2,
  111.                 this.lblServer.Top + this.lblServer.Height);
  112.             this.toolTip.Show(msg, this, showLocation, 1000);
  113.             // 查询服务器上所有数据库的 SQL 查询命令
  114.             string sqlTxt = "Select [Name] From [SysDatabases] Order By [Name]";
  115.             // 保存结果的 DataTable
  116.             DataTable dataBaseDt = new DataTable();
  117.             // 创建连接对象
  118.             using (SqlConnection con = new SqlConnection(GetConnectionString()))
  119.             {
  120.                 // 执行查询
  121.                 try
  122.                 {
  123.                     // 创建适配器对象
  124.                     using (SqlDataAdapter adp = new SqlDataAdapter(sqlTxt, con))
  125.                     {
  126.                         // 将查询结果填充到 DataTable
  127.                         adp.Fill(dataBaseDt);
  128.                     }
  129.                 }
  130.                 catch { }  // 不弹出异常消息
  131.             }
  132.             // 如果 DataTable 包含数据行
  133.             if (dataBaseDt.Rows.Count > 0)
  134.             {
  135.                 // 创建窗体数据绑定对象
  136.                 BindingSource source = new BindingSource();
  137.                 source.DataSource = dataBaseDt;
  138.                 // 将结果绑定到数据库列表
  139.                 cboDataBaseName.DataSource = source;
  140.                 cboDataBaseName.DisplayMember = "Name";
  141.                 cboDataBaseName.ValueMember = "Name";
  142.             }
  143.             else
  144.             {
  145.                 // 移除数据库列表
  146.                 cboDataBaseName.DataSource = null;
  147.             }
  148.         }
  149.         /// <summary>
  150.         /// 获取连接字符串
  151.         /// </summary>
  152.         private string GetConnectionString()
  153.         {
  154.             // 重新创建连接字符串创建者
  155.             _connectionStringBuilder = new SqlConnectionStringBuilder();
  156.             // 获取服务器名称
  157.             if (!string.IsNullOrEmpty(cboSqlDataSource.Text.Trim()))
  158.                 _connectionStringBuilder.DataSource = cboSqlDataSource.Text.Trim();
  159.             // 获取登录类型
  160.             if (rdoValidateBySQLServer.Checked)
  161.             {
  162.                 if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
  163.                     _connectionStringBuilder.UserID = txtUserName.Text.Trim();
  164.                 if (!string.IsNullOrEmpty(txtPassword.Text.Trim()))
  165.                     _connectionStringBuilder.Password = txtPassword.Text.Trim();
  166.             }
  167.             // 获取默认数据库
  168.             if (!string.IsNullOrEmpty(cboDataBaseName.Text.Trim())
  169.                 || !string.IsNullOrEmpty(txtDataBaseFilePath.Text.Trim())
  170.                 || !string.IsNullOrEmpty(txtLogicalName.Text.Trim()))
  171.             {
  172.                 // 如果是附加一个数据库文件
  173.                 if (rdoAttachADataBaseFile.Checked)
  174.                 {
  175.                     _connectionStringBuilder.AttachDBFilename = txtDataBaseFilePath.Text.Trim();
  176.                     _connectionStringBuilder.InitialCatalog = txtLogicalName.Text.Trim();
  177.                 }
  178.                 else
  179.                     _connectionStringBuilder.InitialCatalog = cboDataBaseName.Text.Trim();
  180.             }
  181.             // 调整连接字符串
  182.             if (rdoValidateByWindows.Checked)
  183.                 _connectionStringBuilder.IntegratedSecurity = true;
  184.             // 返回连接字符串
  185.             return _connectionStringBuilder.ConnectionString;
  186.         }
  187.         #endregion
  188.         #region Public Methods
  189.         /// <summary>
  190.         /// 无参构造
  191.         /// </summary>
  192.         public frmConnectionConfig(string applicationName)
  193.         {
  194.             // 构建设计器控件
  195.             InitializeComponent();
  196.             // 保存当前应用程序名称
  197.             _applicationName = applicationName;
  198.             // 创建连接字符串创建者
  199.             _connectionStringBuilder = new SqlConnectionStringBuilder();
  200.             _connectionStringBuilder.IntegratedSecurity = true;
  201.             //_connectionStringBuilder.ApplicationName = _applicationName;
  202.             //_connectionStringBuilder.AsynchronousProcessing = true;
  203.             //_connectionStringBuilder.Encrypt = true;
  204.         } 
  205.         #endregion
  206.         #region Event Handlers
  207.         /// <summary>
  208.         /// 数据源 列表下拉事件
  209.         /// </summary>
  210.         /// <param name="sender"></param>
  211.         /// <param name="e"></param>
  212.         private void cboSqlDataSource_DropDown(object sender, EventArgs e)
  213.         {
  214.             // 如果 数据源 列表条目为空
  215.             if (cboSqlDataSource.Items.Count == 0)
  216.             {
  217.                 // 获取 本地网络所有 SQL Server 实例(数据源)
  218.                 GetSqlDataSource();
  219.             }
  220.         }
  221.         /// <summary>
  222.         /// 数据库 列表获得焦点事件
  223.         /// </summary>
  224.         /// <param name="sender"></param>
  225.         /// <param name="e"></param>
  226.         private void cboDataBaseName_Enter(object sender, EventArgs e)
  227.         {
  228.             // 获取当前服务器上所有数据库名称
  229.             GetDataBaseName();
  230.         }
  231.         /// <summary>
  232.         /// 选择选项卡页事件
  233.         /// </summary>
  234.         /// <param name="sender"></param>
  235.         /// <param name="e"></param>
  236.         private void tcServerInfo_Selecting(object sender, TabControlCancelEventArgs e)
  237.         {
  238.             // 如果 数据源 列表条目为空
  239.             if (cboSqlDataSource.Items.Count == 0 && e.TabPage == tpServerInfo)
  240.             {
  241.                 // 获取 本地网络所有 SQL Server 实例(数据源)
  242.                 GetSqlDataSource();
  243.             }
  244.         }
  245.         /// <summary>
  246.         /// [刷新]按钮点击事件
  247.         /// </summary>
  248.         /// <param name="sender"></param>
  249.         /// <param name="e"></param>
  250.         private void btnRefresh_Click(object sender, EventArgs e)
  251.         {
  252.             // 获取 本地网络所有 SQL Server 实例(数据源)
  253.             GetSqlDataSource();
  254.         }
  255.         /// <summary>
  256.         /// [使用 SQL Server 身份验证]单选按钮 Checked 属性更改事件
  257.         /// </summary>
  258.         /// <param name="sender"></param>
  259.         /// <param name="e"></param>
  260.         private void rdoValidateBySQLServer_CheckedChanged(object sender, EventArgs e)
  261.         {
  262.             if (rdoValidateBySQLServer.Checked)
  263.             {
  264.                 _connectionStringBuilder.IntegratedSecurity = false;
  265.                 lblUserName.Enabled = true;
  266.                 txtUserName.Enabled = true;
  267.                 lblPassword.Enabled = true;
  268.                 txtPassword.Enabled = true;
  269.                 if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
  270.                     gbConnectToADataBase.Enabled = true;
  271.                 else
  272.                     gbConnectToADataBase.Enabled = false;
  273.             }
  274.             else
  275.             {
  276.                 _connectionStringBuilder.IntegratedSecurity = true;
  277.                 lblUserName.Enabled = false;
  278.                 txtUserName.Enabled = false;
  279.                 lblPassword.Enabled = false;
  280.                 txtPassword.Enabled = false;
  281.                 if (!string.IsNullOrEmpty(cboSqlDataSource.Text.Trim()))
  282.                     gbConnectToADataBase.Enabled = true;
  283.                 else
  284.                     gbConnectToADataBase.Enabled = false;
  285.             }
  286.         }
  287.         /// <summary>
  288.         /// [附加一个数据库文件]单选按钮 Checked 属性更改事件
  289.         /// </summary>
  290.         /// <param name="sender"></param>
  291.         /// <param name="e"></param>
  292.         private void rdoAttachADataBaseFile_CheckedChanged(object sender, EventArgs e)
  293.         {
  294.             if (rdoAttachADataBaseFile.Checked)
  295.             {
  296.                 cboDataBaseName.Enabled = false;
  297.                 txtDataBaseFilePath.Enabled = true;
  298.                 btnBrowse.Enabled = true;
  299.                 lblLogicalName.Enabled = true;
  300.                 txtLogicalName.Enabled = true;
  301.             }
  302.             else
  303.             {
  304.                 cboDataBaseName.Enabled = true;
  305.                 txtDataBaseFilePath.Enabled = false;
  306.                 btnBrowse.Enabled = false;
  307.                 lblLogicalName.Enabled = false;
  308.                 txtLogicalName.Enabled = false;
  309.             }
  310.         }
  311.         
  312.         /// <summary>
  313.         /// 数据源 列表 Text 值更改事件
  314.         /// </summary>
  315.         /// <param name="sender"></param>
  316.         /// <param name="e"></param>
  317.         private void cboSqlDataSource_TextChanged(object sender, EventArgs e)
  318.         {
  319.             if (!string.IsNullOrEmpty(cboSqlDataSource.Text.Trim())
  320.                 && _connectionStringBuilder.IntegratedSecurity == true
  321.                 || !string.IsNullOrEmpty(txtUserName.Text.Trim())
  322.                 && _connectionStringBuilder.IntegratedSecurity == false)
  323.             {
  324.                 gbConnectToADataBase.Enabled = true;
  325.                 btnOK.Enabled = true;
  326.             }
  327.             else
  328.             {
  329.                 gbConnectToADataBase.Enabled = false;
  330.                 btnOK.Enabled = false;
  331.             }
  332.         }
  333.         /// <summary>
  334.         /// [用户名]文本框文本更改事件
  335.         /// </summary>
  336.         /// <param name="sender"></param>
  337.         /// <param name="e"></param>
  338.         private void txtUserName_TextChanged(object sender, EventArgs e)
  339.         {
  340.             if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
  341.                 gbConnectToADataBase.Enabled = true;
  342.             else
  343.                 gbConnectToADataBase.Enabled = false;
  344.         }
  345.         /// <summary>
  346.         /// [测试连接]按钮点击事件
  347.         /// </summary>
  348.         /// <param name="sender"></param>
  349.         /// <param name="e"></param>
  350.         private void btnTestConnection_Click(object sender, EventArgs e)
  351.         {
  352.             if (!string.IsNullOrEmpty(cboSqlDataSource.Text.Trim()))
  353.             {
  354.                 // 创建连接对象
  355.                 using (SqlConnection con = new SqlConnection(GetConnectionString()))
  356.                 {
  357.                     try
  358.                     {
  359.                         // 打开数据库连接
  360.                         con.Open();
  361.                         // 给出用户提示
  362.                         MessageBox.Show(
  363.                             "测试连接成功。",
  364.                             this.Text,
  365.                             MessageBoxButtons.OK,
  366.                             MessageBoxIcon.Information);
  367.                     }
  368.                     catch (Exception ex)
  369.                     {
  370.                         MessageBox.Show(
  371.                         ex.Message,
  372.                         this.Text,
  373.                         MessageBoxButtons.OK,
  374.                         MessageBoxIcon.Error);
  375.                     }
  376.                     finally
  377.                     {
  378.                         // 关闭数据库连接
  379.                         con.Close();
  380.                     }
  381.                 }
  382.             }
  383.             else
  384.             {
  385.                 MessageBox.Show(
  386.                     "无法测试此连接,因为没有指定服务器名称。",
  387.                     this.Text,
  388.                     MessageBoxButtons.OK,
  389.                     MessageBoxIcon.Error);
  390.             }
  391.         }
  392.         /// <summary>
  393.         /// [确定]按钮点击事件 
  394.         /// </summary>
  395.         /// <param name="sender"></param>
  396.         /// <param name="e"></param>
  397.         private void btnOK_Click(object sender, EventArgs e)
  398.         {
  399.             try
  400.             {
  401.                 // 创建应用程序配置文件
  402.                 string configFilePath = string.Format(@"{0}.config", Application.ExecutablePath);
  403.                 Configuration configuration = null;
  404.                 if (!File.Exists(configFilePath))
  405.                 {
  406.                     FileStream fs = null;
  407.                     try
  408.                     {
  409.                         fs = new FileStream(configFilePath, FileMode.CreateNew);
  410.                         fs.SetLength(0);
  411.                     }
  412.                     catch { }   // 不弹出异常提示
  413.                     finally
  414.                     {
  415.                         fs.Close();
  416.                     }
  417.                 }
  418.                 configuration = ConfigurationManager.OpenExeConfiguration(configFilePath);
  419.                 // configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  420.                 // 遍历检验 ConnectionType 项是否存在
  421.                 bool appSettingsExist = false;
  422.                 foreach (KeyValueConfigurationElement element in configuration.AppSettings.Settings)
  423.                 {
  424.                     // 如果存在
  425.                     if (element.Key == "ConnectionType")
  426.                     {
  427.                         // 标识存在
  428.                         appSettingsExist = true;
  429.                         break;
  430.                     }
  431.                 }
  432.                 // 如果存在
  433.                 if (appSettingsExist)
  434.                     // 修改配置节点
  435.                     configuration.AppSettings.Settings["ConnectionType"].Value = "SQLSERVER";
  436.                 else
  437.                     // 添加配置节点
  438.                     configuration.AppSettings.Settings.Add("ConnectionType""SQLSERVER");
  439.                 // 遍历检验 SQLSERVER 项是否存在
  440.                 bool connectionStringExist = false;
  441.                 foreach (ConnectionStringSettings setting in configuration.ConnectionStrings.ConnectionStrings)
  442.                 {
  443.                     // 如果存在
  444.                     if (setting.Name == "SQLSERVER")
  445.                     {
  446.                         // 标识存在
  447.                         connectionStringExist = true;
  448.                     }
  449.                 }
  450.                 // 如果存在
  451.                 if (connectionStringExist)
  452.                 {
  453.                     // 修改配置节点
  454.                     configuration.ConnectionStrings.ConnectionStrings["SQLSERVER"].ConnectionString =
  455.                         GetConnectionString();
  456.                     configuration.ConnectionStrings.ConnectionStrings["SQLSERVER"].ProviderName =
  457.                         "System.Data.SqlClient";
  458.                 }
  459.                 else
  460.                 {
  461.                     // 添加连接字符串节点
  462.                     configuration.ConnectionStrings.ConnectionStrings.Add(
  463.                         new ConnectionStringSettings(
  464.                         "SQLSERVER",
  465.                         GetConnectionString(),
  466.                         "System.Data.SqlClient"));
  467.                 }
  468.                 // 保存配置
  469.                 configuration.SaveAs(
  470.                     string.Format(@"{0}.config", Application.ExecutablePath.Replace(".EXE"".exe")),
  471.                     ConfigurationSaveMode.Minimal);
  472.                 // 关闭窗体
  473.                 this.Close();
  474.             }
  475.             catch (Exception ex)
  476.             {
  477.                 MessageBox.Show(
  478.                     string.Format("保存数据库连接配置时发生以下错误:/r/n/r/n{0}", ex.Message),
  479.                     this.Text,
  480.                     MessageBoxButtons.OK,
  481.                     MessageBoxIcon.Error);
  482.             }
  483.         }
  484.         /// <summary>
  485.         /// [取消]按钮点击事件
  486.         /// </summary>
  487.         /// <param name="sender"></param>
  488.         /// <param name="e"></param>
  489.         private void btnCancel_Click(object sender, EventArgs e)
  490.         {
  491.             this.Close();
  492.         }
  493.         /// <summary>
  494.         /// [浏览]按钮点击事件
  495.         /// </summary>
  496.         /// <param name="sender"></param>
  497.         /// <param name="e"></param>
  498.         private void btnBrowse_Click(object sender, EventArgs e)
  499.         {
  500.             OpenFileDialog ofDlg = new OpenFileDialog();
  501.             ofDlg.Title = "选择 SQL Server 数据库文件";
  502.             ofDlg.Filter = "Microsoft SQL Server 数据库(*.mdf)|*.mdf|所有文件(*.*)|*.*";
  503.             ofDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  504.             DialogResult result = ofDlg.ShowDialog(this);
  505.             if (result == DialogResult.OK)
  506.                 txtDataBaseFilePath.Text = ofDlg.FileName;
  507.         }
  508.         /// <summary>
  509.         /// [获取连接字符串]按钮点击事件
  510.         /// </summary>
  511.         /// <param name="sender"></param>
  512.         /// <param name="e"></param>
  513.         private void btnGetConnectionString_Click(object sender, EventArgs e)
  514.         {
  515.             this.txtConnectionString.Text = GetConnectionString();
  516.         }
  517.         #endregion
  518.     }
  519. }

设计器源码:

  1. namespace CodingMouse.CMCSharpSDK.UI.Forms
  2. {
  3.     partial class frmConnectionConfig
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.components = new System.ComponentModel.Container();
  29.             this.cboSqlDataSource = new System.Windows.Forms.ComboBox();
  30.             this.tcServerInfo = new System.Windows.Forms.TabControl();
  31.             this.tpConnectionInfo = new System.Windows.Forms.TabPage();
  32.             this.gbConnectToADataBase = new System.Windows.Forms.GroupBox();
  33.             this.txtLogicalName = new System.Windows.Forms.TextBox();
  34.             this.lblLogicalName = new System.Windows.Forms.Label();
  35.             this.btnBrowse = new System.Windows.Forms.Button();
  36.             this.cboDataBaseName = new System.Windows.Forms.ComboBox();
  37.             this.txtDataBaseFilePath = new System.Windows.Forms.TextBox();
  38.             this.rdoAttachADataBaseFile = new System.Windows.Forms.RadioButton();
  39.             this.rdoSelectOrEnterADataBaseName = new System.Windows.Forms.RadioButton();
  40.             this.gbLogOnToTheServer = new System.Windows.Forms.GroupBox();
  41.             this.txtPassword = new System.Windows.Forms.TextBox();
  42.             this.txtUserName = new System.Windows.Forms.TextBox();
  43.             this.lblPassword = new System.Windows.Forms.Label();
  44.             this.lblUserName = new System.Windows.Forms.Label();
  45.             this.rdoValidateBySQLServer = new System.Windows.Forms.RadioButton();
  46.             this.rdoValidateByWindows = new System.Windows.Forms.RadioButton();
  47.             this.tpServerInfo = new System.Windows.Forms.TabPage();
  48.             this.dgvServerInfo = new System.Windows.Forms.DataGridView();
  49.             this.toolTip = new System.Windows.Forms.ToolTip(this.components);
  50.             this.btnRefresh = new System.Windows.Forms.Button();
  51.             this.lblServer = new System.Windows.Forms.Label();
  52.             this.btnGetConnectionString = new System.Windows.Forms.Button();
  53.             this.btnCancel = new System.Windows.Forms.Button();
  54.             this.btnOK = new System.Windows.Forms.Button();
  55.             this.btnTestConnection = new System.Windows.Forms.Button();
  56.             this.lblSplit = new System.Windows.Forms.Label();
  57.             this.txtConnectionString = new System.Windows.Forms.TextBox();
  58.             this.tcServerInfo.SuspendLayout();
  59.             this.tpConnectionInfo.SuspendLayout();
  60.             this.gbConnectToADataBase.SuspendLayout();
  61.             this.gbLogOnToTheServer.SuspendLayout();
  62.             this.tpServerInfo.SuspendLayout();
  63.             ((System.ComponentModel.ISupportInitialize)(this.dgvServerInfo)).BeginInit();
  64.             this.SuspendLayout();
  65.             // 
  66.             // cboSqlDataSource
  67.             // 
  68.             this.cboSqlDataSource.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  69.                         | System.Windows.Forms.AnchorStyles.Right)));
  70.             this.cboSqlDataSource.FormattingEnabled = true;
  71.             this.cboSqlDataSource.Location = new System.Drawing.Point(12, 24);
  72.             this.cboSqlDataSource.Name = "cboSqlDataSource";
  73.             this.cboSqlDataSource.Size = new System.Drawing.Size(269, 20);
  74.             this.cboSqlDataSource.TabIndex = 1;
  75.             this.cboSqlDataSource.Leave += new System.EventHandler(this.cboSqlDataSource_Leave);
  76.             this.cboSqlDataSource.TextChanged += new System.EventHandler(this.cboSqlDataSource_TextChanged);
  77.             this.cboSqlDataSource.DropDown += new System.EventHandler(this.cboSqlDataSource_DropDown);
  78.             // 
  79.             // tcServerInfo
  80.             // 
  81.             this.tcServerInfo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  82.                         | System.Windows.Forms.AnchorStyles.Right)));
  83.             this.tcServerInfo.Controls.Add(this.tpConnectionInfo);
  84.             this.tcServerInfo.Controls.Add(this.tpServerInfo);
  85.             this.tcServerInfo.HotTrack = true;
  86.             this.tcServerInfo.Location = new System.Drawing.Point(12, 50);
  87.             this.tcServerInfo.Name = "tcServerInfo";
  88.             this.tcServerInfo.SelectedIndex = 0;
  89.             this.tcServerInfo.ShowToolTips = true;
  90.             this.tcServerInfo.Size = new System.Drawing.Size(350, 304);
  91.             this.tcServerInfo.TabIndex = 3;
  92.             this.tcServerInfo.Selecting += new System.Windows.Forms.TabControlCancelEventHandler(this.tcServerInfo_Selecting);
  93.             // 
  94.             // tpConnectionInfo
  95.             // 
  96.             this.tpConnectionInfo.Controls.Add(this.gbConnectToADataBase);
  97.             this.tpConnectionInfo.Controls.Add(this.gbLogOnToTheServer);
  98.             this.tpConnectionInfo.Location = new System.Drawing.Point(4, 21);
  99.             this.tpConnectionInfo.Name = "tpConnectionInfo";
  100.             this.tpConnectionInfo.Padding = new System.Windows.Forms.Padding(3);
  101.             this.tpConnectionInfo.Size = new System.Drawing.Size(342, 279);
  102.             this.tpConnectionInfo.TabIndex = 0;
  103.             this.tpConnectionInfo.Text = "数据库连接信息";
  104.             this.tpConnectionInfo.UseVisualStyleBackColor = true;
  105.             // 
  106.             // gbConnectToADataBase
  107.             // 
  108.             this.gbConnectToADataBase.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  109.                         | System.Windows.Forms.AnchorStyles.Right)));
  110.             this.gbConnectToADataBase.Controls.Add(this.txtLogicalName);
  111.             this.gbConnectToADataBase.Controls.Add(this.lblLogicalName);
  112.             this.gbConnectToADataBase.Controls.Add(this.btnBrowse);
  113.             this.gbConnectToADataBase.Controls.Add(this.cboDataBaseName);
  114.             this.gbConnectToADataBase.Controls.Add(this.txtDataBaseFilePath);
  115.             this.gbConnectToADataBase.Controls.Add(this.rdoAttachADataBaseFile);
  116.             this.gbConnectToADataBase.Controls.Add(this.rdoSelectOrEnterADataBaseName);
  117.             this.gbConnectToADataBase.Enabled = false;
  118.             this.gbConnectToADataBase.Location = new System.Drawing.Point(6, 129);
  119.             this.gbConnectToADataBase.Name = "gbConnectToADataBase";
  120.             this.gbConnectToADataBase.Size = new System.Drawing.Size(330, 144);
  121.             this.gbConnectToADataBase.TabIndex = 1;
  122.             this.gbConnectToADataBase.TabStop = false;
  123.             this.gbConnectToADataBase.Text = "连接到一个数据库";
  124.             // 
  125.             // txtLogicalName
  126.             // 
  127.             this.txtLogicalName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  128.                         | System.Windows.Forms.AnchorStyles.Right)));
  129.             this.txtLogicalName.Enabled = false;
  130.             this.txtLogicalName.Location = new System.Drawing.Point(6, 117);
  131.             this.txtLogicalName.Name = "txtLogicalName";
  132.             this.txtLogicalName.Size = new System.Drawing.Size(318, 21);
  133.             this.txtLogicalName.TabIndex = 6;
  134.             // 
  135.             // lblLogicalName
  136.             // 
  137.             this.lblLogicalName.AutoSize = true;
  138.             this.lblLogicalName.Enabled = false;
  139.             this.lblLogicalName.Location = new System.Drawing.Point(4, 102);
  140.             this.lblLogicalName.Name = "lblLogicalName";
  141.             this.lblLogicalName.Size = new System.Drawing.Size(65, 12);
  142.             this.lblLogicalName.TabIndex = 5;
  143.             this.lblLogicalName.Text = "逻辑名(&L):";
  144.             // 
  145.             // btnBrowse
  146.             // 
  147.             this.btnBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  148.             this.btnBrowse.Enabled = false;
  149.             this.btnBrowse.Location = new System.Drawing.Point(244, 78);
  150.             this.btnBrowse.Name = "btnBrowse";
  151.             this.btnBrowse.Size = new System.Drawing.Size(80, 21);
  152.             this.btnBrowse.TabIndex = 4;
  153.             this.btnBrowse.Text = "浏览(&B)...";
  154.             this.btnBrowse.UseVisualStyleBackColor = true;
  155.             this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
  156.             // 
  157.             // cboDataBaseName
  158.             // 
  159.             this.cboDataBaseName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  160.                         | System.Windows.Forms.AnchorStyles.Right)));
  161.             this.cboDataBaseName.FormattingEnabled = true;
  162.             this.cboDataBaseName.Location = new System.Drawing.Point(6, 36);
  163.             this.cboDataBaseName.Name = "cboDataBaseName";
  164.             this.cboDataBaseName.Size = new System.Drawing.Size(318, 20);
  165.             this.cboDataBaseName.TabIndex = 1;
  166.             this.cboDataBaseName.Enter += new System.EventHandler(this.cboDataBaseName_Enter);
  167.             // 
  168.             // txtDataBaseFilePath
  169.             // 
  170.             this.txtDataBaseFilePath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  171.                         | System.Windows.Forms.AnchorStyles.Right)));
  172.             this.txtDataBaseFilePath.Enabled = false;
  173.             this.txtDataBaseFilePath.Location = new System.Drawing.Point(6, 78);
  174.             this.txtDataBaseFilePath.Name = "txtDataBaseFilePath";
  175.             this.txtDataBaseFilePath.Size = new System.Drawing.Size(232, 21);
  176.             this.txtDataBaseFilePath.TabIndex = 3;
  177.             // 
  178.             // rdoAttachADataBaseFile
  179.             // 
  180.             this.rdoAttachADataBaseFile.AutoSize = true;
  181.             this.rdoAttachADataBaseFile.Location = new System.Drawing.Point(6, 62);
  182.             this.rdoAttachADataBaseFile.Name = "rdoAttachADataBaseFile";
  183.             this.rdoAttachADataBaseFile.Size = new System.Drawing.Size(155, 16);
  184.             this.rdoAttachADataBaseFile.TabIndex = 2;
  185.             this.rdoAttachADataBaseFile.Text = "附加一个数据库文件(&H):";
  186.             this.rdoAttachADataBaseFile.UseVisualStyleBackColor = true;
  187.             this.rdoAttachADataBaseFile.CheckedChanged += new System.EventHandler(this.rdoAttachADataBaseFile_CheckedChanged);
  188.             // 
  189.             // rdoSelectOrEnterADataBaseName
  190.             // 
  191.             this.rdoSelectOrEnterADataBaseName.AutoSize = true;
  192.             this.rdoSelectOrEnterADataBaseName.Checked = true;
  193.             this.rdoSelectOrEnterADataBaseName.Location = new System.Drawing.Point(6, 20);
  194.             this.rdoSelectOrEnterADataBaseName.Name = "rdoSelectOrEnterADataBaseName";
  195.             this.rdoSelectOrEnterADataBaseName.Size = new System.Drawing.Size(179, 16);
  196.             this.rdoSelectOrEnterADataBaseName.TabIndex = 0;
  197.             this.rdoSelectOrEnterADataBaseName.TabStop = true;
  198.             this.rdoSelectOrEnterADataBaseName.Text = "选择或输入一个数据库名(&D):";
  199.             this.rdoSelectOrEnterADataBaseName.UseVisualStyleBackColor = true;
  200.             // 
  201.             // gbLogOnToTheServer
  202.             // 
  203.             this.gbLogOnToTheServer.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  204.                         | System.Windows.Forms.AnchorStyles.Right)));
  205.             this.gbLogOnToTheServer.Controls.Add(this.txtPassword);
  206.             this.gbLogOnToTheServer.Controls.Add(this.txtUserName);
  207.             this.gbLogOnToTheServer.Controls.Add(this.lblPassword);
  208.             this.gbLogOnToTheServer.Controls.Add(this.lblUserName);
  209.             this.gbLogOnToTheServer.Controls.Add(this.rdoValidateBySQLServer);
  210.             this.gbLogOnToTheServer.Controls.Add(this.rdoValidateByWindows);
  211.             this.gbLogOnToTheServer.Location = new System.Drawing.Point(6, 6);
  212.             this.gbLogOnToTheServer.Name = "gbLogOnToTheServer";
  213.             this.gbLogOnToTheServer.Size = new System.Drawing.Size(330, 117);
  214.             this.gbLogOnToTheServer.TabIndex = 0;
  215.             this.gbLogOnToTheServer.TabStop = false;
  216.             this.gbLogOnToTheServer.Text = "登录到服务器";
  217.             // 
  218.             // txtPassword
  219.             // 
  220.             this.txtPassword.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  221.                         | System.Windows.Forms.AnchorStyles.Right)));
  222.             this.txtPassword.Enabled = false;
  223.             this.txtPassword.Location = new System.Drawing.Point(93, 88);
  224.             this.txtPassword.Name = "txtPassword";
  225.             this.txtPassword.PasswordChar = '*';
  226.             this.txtPassword.Size = new System.Drawing.Size(231, 21);
  227.             this.txtPassword.TabIndex = 5;
  228.             this.txtPassword.TextChanged += new System.EventHandler(this.txtPassword_TextChanged);
  229.             // 
  230.             // txtUserName
  231.             // 
  232.             this.txtUserName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  233.                         | System.Windows.Forms.AnchorStyles.Right)));
  234.             this.txtUserName.Enabled = false;
  235.             this.txtUserName.Location = new System.Drawing.Point(93, 61);
  236.             this.txtUserName.Name = "txtUserName";
  237.             this.txtUserName.Size = new System.Drawing.Size(231, 21);
  238.             this.txtUserName.TabIndex = 3;
  239.             this.txtUserName.TextChanged += new System.EventHandler(this.txtUserName_TextChanged);
  240.             // 
  241.             // lblPassword
  242.             // 
  243.             this.lblPassword.AutoSize = true;
  244.             this.lblPassword.Enabled = false;
  245.             this.lblPassword.Location = new System.Drawing.Point(22, 91);
  246.             this.lblPassword.Name = "lblPassword";
  247.             this.lblPassword.Size = new System.Drawing.Size(65, 12);
  248.             this.lblPassword.TabIndex = 4;
  249.             this.lblPassword.Text = "密  码(&P):";
  250.             // 
  251.             // lblUserName
  252.             // 
  253.             this.lblUserName.AutoSize = true;
  254.             this.lblUserName.Enabled = false;
  255.             this.lblUserName.Location = new System.Drawing.Point(22, 64);
  256.             this.lblUserName.Name = "lblUserName";
  257.             this.lblUserName.Size = new System.Drawing.Size(65, 12);
  258.             this.lblUserName.TabIndex = 2;
  259.             this.lblUserName.Text = "用户名(&U):";
  260.             // 
  261.             // rdoValidateBySQLServer
  262.             // 
  263.             this.rdoValidateBySQLServer.AutoSize = true;
  264.             this.rdoValidateBySQLServer.Location = new System.Drawing.Point(6, 42);
  265.             this.rdoValidateBySQLServer.Name = "rdoValidateBySQLServer";
  266.             this.rdoValidateBySQLServer.Size = new System.Drawing.Size(185, 16);
  267.             this.rdoValidateBySQLServer.TabIndex = 1;
  268.             this.rdoValidateBySQLServer.Text = "使用 SQL Server 身份验证(&Q)";
  269.             this.rdoValidateBySQLServer.UseVisualStyleBackColor = true;
  270.             this.rdoValidateBySQLServer.CheckedChanged += new System.EventHandler(this.rdoValidateBySQLServer_CheckedChanged);
  271.             // 
  272.             // rdoValidateByWindows
  273.             // 
  274.             this.rdoValidateByWindows.AutoSize = true;
  275.             this.rdoValidateByWindows.Checked = true;
  276.             this.rdoValidateByWindows.Location = new System.Drawing.Point(6, 20);
  277.             this.rdoValidateByWindows.Name = "rdoValidateByWindows";
  278.             this.rdoValidateByWindows.Size = new System.Drawing.Size(167, 16);
  279.             this.rdoValidateByWindows.TabIndex = 0;
  280.             this.rdoValidateByWindows.TabStop = true;
  281.             this.rdoValidateByWindows.Text = "使用 Windows 身份验证(&W)";
  282.             this.rdoValidateByWindows.UseVisualStyleBackColor = true;
  283.             // 
  284.             // tpServerInfo
  285.             // 
  286.             this.tpServerInfo.Controls.Add(this.dgvServerInfo);
  287.             this.tpServerInfo.Location = new System.Drawing.Point(4, 21);
  288.             this.tpServerInfo.Name = "tpServerInfo";
  289.             this.tpServerInfo.Padding = new System.Windows.Forms.Padding(3);
  290.             this.tpServerInfo.Size = new System.Drawing.Size(342, 279);
  291.             this.tpServerInfo.TabIndex = 1;
  292.             this.tpServerInfo.Text = "服务器详细信息";
  293.             this.tpServerInfo.UseVisualStyleBackColor = true;
  294.             // 
  295.             // dgvServerInfo
  296.             // 
  297.             this.dgvServerInfo.AllowUserToAddRows = false;
  298.             this.dgvServerInfo.AllowUserToDeleteRows = false;
  299.             this.dgvServerInfo.AllowUserToResizeRows = false;
  300.             this.dgvServerInfo.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
  301.             this.dgvServerInfo.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  302.             this.dgvServerInfo.Dock = System.Windows.Forms.DockStyle.Fill;
  303.             this.dgvServerInfo.Location = new System.Drawing.Point(3, 3);
  304.             this.dgvServerInfo.MultiSelect = false;
  305.             this.dgvServerInfo.Name = "dgvServerInfo";
  306.             this.dgvServerInfo.ReadOnly = true;
  307.             this.dgvServerInfo.RowHeadersVisible = false;
  308.             this.dgvServerInfo.RowTemplate.Height = 23;
  309.             this.dgvServerInfo.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
  310.             this.dgvServerInfo.Size = new System.Drawing.Size(336, 273);
  311.             this.dgvServerInfo.TabIndex = 0;
  312.             // 
  313.             // toolTip
  314.             // 
  315.             this.toolTip.ShowAlways = true;
  316.             // 
  317.             // btnRefresh
  318.             // 
  319.             this.btnRefresh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  320.             this.btnRefresh.Location = new System.Drawing.Point(287, 23);
  321.             this.btnRefresh.Name = "btnRefresh";
  322.             this.btnRefresh.Size = new System.Drawing.Size(75, 21);
  323.             this.btnRefresh.TabIndex = 2;
  324.             this.btnRefresh.Text = "刷新(&R)";
  325.             this.btnRefresh.UseVisualStyleBackColor = true;
  326.             this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
  327.             // 
  328.             // lblServer
  329.             // 
  330.             this.lblServer.AutoSize = true;
  331.             this.lblServer.Location = new System.Drawing.Point(10, 9);
  332.             this.lblServer.Name = "lblServer";
  333.             this.lblServer.Size = new System.Drawing.Size(77, 12);
  334.             this.lblServer.TabIndex = 0;
  335.             this.lblServer.Text = "服务器名(&E):";
  336.             // 
  337.             // btnGetConnectionString
  338.             // 
  339.             this.btnGetConnectionString.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  340.             this.btnGetConnectionString.Enabled = false;
  341.             this.btnGetConnectionString.Location = new System.Drawing.Point(242, 360);
  342.             this.btnGetConnectionString.Name = "btnGetConnectionString";
  343.             this.btnGetConnectionString.Size = new System.Drawing.Size(120, 21);
  344.             this.btnGetConnectionString.TabIndex = 5;
  345.             this.btnGetConnectionString.Text = "获取连接字符串(&G)";
  346.             this.btnGetConnectionString.UseVisualStyleBackColor = true;
  347.             this.btnGetConnectionString.Click += new System.EventHandler(this.btnGetConnectionString_Click);
  348.             // 
  349.             // btnCancel
  350.             // 
  351.             this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  352.             this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  353.             this.btnCancel.Location = new System.Drawing.Point(287, 395);
  354.             this.btnCancel.Name = "btnCancel";
  355.             this.btnCancel.Size = new System.Drawing.Size(75, 21);
  356.             this.btnCancel.TabIndex = 9;
  357.             this.btnCancel.Text = "取消";
  358.             this.btnCancel.UseVisualStyleBackColor = true;
  359.             this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  360.             // 
  361.             // btnOK
  362.             // 
  363.             this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  364.             this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
  365.             this.btnOK.Enabled = false;
  366.             this.btnOK.Location = new System.Drawing.Point(206, 395);
  367.             this.btnOK.Name = "btnOK";
  368.             this.btnOK.Size = new System.Drawing.Size(75, 21);
  369.             this.btnOK.TabIndex = 8;
  370.             this.btnOK.Text = "确定";
  371.             this.btnOK.UseVisualStyleBackColor = true;
  372.             this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
  373.             // 
  374.             // btnTestConnection
  375.             // 
  376.             this.btnTestConnection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  377.             this.btnTestConnection.Location = new System.Drawing.Point(12, 395);
  378.             this.btnTestConnection.Name = "btnTestConnection";
  379.             this.btnTestConnection.Size = new System.Drawing.Size(100, 21);
  380.             this.btnTestConnection.TabIndex = 7;
  381.             this.btnTestConnection.Text = "测试连接(&T)";
  382.             this.btnTestConnection.UseVisualStyleBackColor = true;
  383.             this.btnTestConnection.Click += new System.EventHandler(this.btnTestConnection_Click);
  384.             // 
  385.             // lblSplit
  386.             // 
  387.             this.lblSplit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  388.                         | System.Windows.Forms.AnchorStyles.Right)));
  389.             this.lblSplit.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  390.             this.lblSplit.Location = new System.Drawing.Point(12, 387);
  391.             this.lblSplit.Name = "lblSplit";
  392.             this.lblSplit.Size = new System.Drawing.Size(350, 2);
  393.             this.lblSplit.TabIndex = 6;
  394.             // 
  395.             // txtConnectionString
  396.             // 
  397.             this.txtConnectionString.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  398.                         | System.Windows.Forms.AnchorStyles.Right)));
  399.             this.txtConnectionString.Location = new System.Drawing.Point(12, 360);
  400.             this.txtConnectionString.Name = "txtConnectionString";
  401.             this.txtConnectionString.ReadOnly = true;
  402.             this.txtConnectionString.Size = new System.Drawing.Size(224, 21);
  403.             this.txtConnectionString.TabIndex = 4;
  404.             // 
  405.             // frmConnectionConfig
  406.             // 
  407.             this.AcceptButton = this.btnOK;
  408.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  409.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  410.             this.CancelButton = this.btnCancel;
  411.             this.ClientSize = new System.Drawing.Size(374, 428);
  412.             this.Controls.Add(this.txtConnectionString);
  413.             this.Controls.Add(this.lblSplit);
  414.             this.Controls.Add(this.btnTestConnection);
  415.             this.Controls.Add(this.btnOK);
  416.             this.Controls.Add(this.btnCancel);
  417.             this.Controls.Add(this.btnGetConnectionString);
  418.             this.Controls.Add(this.btnRefresh);
  419.             this.Controls.Add(this.lblServer);
  420.             this.Controls.Add(this.tcServerInfo);
  421.             this.Controls.Add(this.cboSqlDataSource);
  422.             this.HelpButton = true;
  423.             this.MaximizeBox = false;
  424.             this.MinimizeBox = false;
  425.             this.MinimumSize = new System.Drawing.Size(382, 455);
  426.             this.Name = "frmConnectionConfig";
  427.             this.ShowIcon = false;
  428.             this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
  429.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  430.             this.Text = "数据库连接配置";
  431.             this.tcServerInfo.ResumeLayout(false);
  432.             this.tpConnectionInfo.ResumeLayout(false);
  433.             this.gbConnectToADataBase.ResumeLayout(false);
  434.             this.gbConnectToADataBase.PerformLayout();
  435.             this.gbLogOnToTheServer.ResumeLayout(false);
  436.             this.gbLogOnToTheServer.PerformLayout();
  437.             this.tpServerInfo.ResumeLayout(false);
  438.             ((System.ComponentModel.ISupportInitialize)(this.dgvServerInfo)).EndInit();
  439.             this.ResumeLayout(false);
  440.             this.PerformLayout();
  441.         }
  442.         #endregion
  443.         private System.Windows.Forms.ComboBox cboSqlDataSource;
  444.         private System.Windows.Forms.TabControl tcServerInfo;
  445.         private System.Windows.Forms.TabPage tpConnectionInfo;
  446.         private System.Windows.Forms.TabPage tpServerInfo;
  447.         private System.Windows.Forms.DataGridView dgvServerInfo;
  448.         private System.Windows.Forms.ToolTip toolTip;
  449.         private System.Windows.Forms.Label lblServer;
  450.         private System.Windows.Forms.Button btnRefresh;
  451.         private System.Windows.Forms.GroupBox gbLogOnToTheServer;
  452.         private System.Windows.Forms.TextBox txtPassword;
  453.         private System.Windows.Forms.TextBox txtUserName;
  454.         private System.Windows.Forms.Label lblPassword;
  455.         private System.Windows.Forms.Label lblUserName;
  456.         private System.Windows.Forms.RadioButton rdoValidateBySQLServer;
  457.         private System.Windows.Forms.RadioButton rdoValidateByWindows;
  458.         private System.Windows.Forms.GroupBox gbConnectToADataBase;
  459.         private System.Windows.Forms.RadioButton rdoAttachADataBaseFile;
  460.         private System.Windows.Forms.RadioButton rdoSelectOrEnterADataBaseName;
  461.         private System.Windows.Forms.Button btnGetConnectionString;
  462.         private System.Windows.Forms.Button btnCancel;
  463.         private System.Windows.Forms.Button btnOK;
  464.         private System.Windows.Forms.Button btnTestConnection;
  465.         private System.Windows.Forms.ComboBox cboDataBaseName;
  466.         private System.Windows.Forms.TextBox txtDataBaseFilePath;
  467.         private System.Windows.Forms.Button btnBrowse;
  468.         private System.Windows.Forms.TextBox txtLogicalName;
  469.         private System.Windows.Forms.Label lblLogicalName;
  470.         private System.Windows.Forms.Label lblSplit;
  471.         private System.Windows.Forms.TextBox txtConnectionString;
  472.     }
  473. }
  474. 上面是自己写的界面,也可以自己去调用系统的界面

一、添加引用
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Microsoft.Data.ConnectionUI.Dialog.dll

二、引入名称空间

C# code
using Microsoft.Data.ConnectionUI;



三、代码

C# code
DataConnectionDialog dlg =new DataConnectionDialog(); dlg.DataSources.Add(DataSource.SqlDataSource); dlg.SelectedDataProvider = DataProvider.SqlDataProvider; DataConnectionDialog.Show(dlg, this);


比如说放在一个按钮的Click事件里

C# code
privatevoid button1_Click(object sender, EventArgs e) { DataConnectionDialog dlg =new DataConnectionDialog(); dlg.DataSources.Add(DataSource.SqlDataSource); dlg.SelectedDataProvider = DataProvider.SqlDataProvider; DataConnectionDialog.Show(dlg, this); }



四、运行
界面出来了,是不是相当的专业!

DataConnectionDialog 还有很多属性,配置这些属性可以改变窗口外观,满足不同需求。

原文地址:https://www.cnblogs.com/lvfeilong/p/khhjhjhgjhgjhj.html