我想让用户在使用时动态的配置数据库连接串,所以想调用“数据链接属性”对话框,如果在VC里,直接调用借口的PromptNew方法就可以了,在C#里,我不想调用Com的接口,.net里面有没有提供相同功能的方法或者对象对象?

解决方案 »

  1.   

    /*
     * (C) DAN MAYER 2004
     * QUESTION OR COMMENTS PLEASE WRITE [email protected]
     * */
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace OdbcConnect
    {
    public class Form1 : System.Windows.Forms.Form
    {
    public System.Windows.Forms.TextBox txtConnectionString;
    private System.Windows.Forms.Label lbConnectionString;
    private System.Windows.Forms.Button ButtonGetConnectionString;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.txtConnectionString = new System.Windows.Forms.TextBox();
    this.lbConnectionString = new System.Windows.Forms.Label();
    this.ButtonGetConnectionString = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // txtConnectionString
    // 
    this.txtConnectionString.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.txtConnectionString.Location = new System.Drawing.Point(16, 32);
    this.txtConnectionString.Name = "txtConnectionString";
    this.txtConnectionString.Size = new System.Drawing.Size(232, 20);
    this.txtConnectionString.TabIndex = 18;
    this.txtConnectionString.Text = "";
    // 
    // lbConnectionString
    // 
    this.lbConnectionString.AutoSize = true;
    this.lbConnectionString.Location = new System.Drawing.Point(16, 16);
    this.lbConnectionString.Name = "lbConnectionString";
    this.lbConnectionString.Size = new System.Drawing.Size(126, 16);
    this.lbConnectionString.TabIndex = 17;
    this.lbConnectionString.Text = "Data source connection:";
    // 
    // ButtonGetConnectionString
    // 
    this.ButtonGetConnectionString.Location = new System.Drawing.Point(248, 32);
    this.ButtonGetConnectionString.Name = "ButtonGetConnectionString";
    this.ButtonGetConnectionString.Size = new System.Drawing.Size(24, 23);
    this.ButtonGetConnectionString.TabIndex = 19;
    this.ButtonGetConnectionString.Text = "...";
    this.ButtonGetConnectionString.Click += new System.EventHandler(this.ButtonGetConnectionString_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 94);
    this.Controls.Add(this.ButtonGetConnectionString);
    this.Controls.Add(this.txtConnectionString);
    this.Controls.Add(this.lbConnectionString);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void ButtonGetConnectionString_Click(object sender, System.EventArgs e)
    {
    /* 
      Reference DataLinks
       NOTE: Reference 
    C:\Program Files\Common Files\System\Ole DB\OLEDB32.DLL
    (Was MSDASC.dll) 
       SEE:
            http://support.microsoft.com:80/support/kb/articles/Q225/1/32.asp
     */
    MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
    //note that a reference to: 
    //  c:\Program Files\Microsoft.NET\Primary Interop Assemblies\adodb.dll
    //is also required to read the ADODB._Connection result
    ADODB._Connection connection;
    //are we editing an existing connect string or getting a new one?
    if(this.txtConnectionString.Text==String.Empty)
    {
    // get a new connection string
    try
    {
    //Prompt user for new connect string
    connection = (ADODB._Connection)dataLinks.PromptNew();
    //read result
    this.txtConnectionString.Text=connection.ConnectionString.ToString();
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }
    else
    {
    // edit connection string
    connection=new ADODB.ConnectionClass();
    connection.ConnectionString=this.txtConnectionString.Text;
    //set local COM compatible data type
    object oConnection=connection;
    try
    {
    //prompt user to edit the given connect string
    if((bool)dataLinks.PromptEdit(ref oConnection))
    {
    this.txtConnectionString.Text=connection.ConnectionString;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }
    }
    }
    }
      

  2.   

    http://www.cnblogs.com/lichdr/archive/2004/10/20/54568.html