Is there a standard dialog for constructing an ADO

2019-04-17 07:45发布

I want to use a standard dialog to solicit user input of an ADO.net connection string. It is trivial to do for the oledb connection string as described here: MSDN Article on MSDASC.DataLinks().Prompt

I've also found examples that use Microsoft.Data.ConnectionUI.dll and MicrosoftData.ConnectionUI.Dialog.dll from VS (HOWTO: Using the Choose Data Source dialog of Visual Studio 2005 from your own code).

Unfortunately these DLLs are not licensed for redistribution.

Is there a standard dialog for choosing a data source that can be distributed with my application?

5条回答
淡お忘
3楼-- · 2019-04-17 08:04

It appears that such a beast does not exist. I've written my own dialog and can include it in projects as needed.


Update:

The source code for these DLLs are now available as per @code4life's answer.

查看更多
乱世女痞
4楼-- · 2019-04-17 08:06

The source code for these DLLs is now available: http://blogs.msdn.com/b/vsdata/archive/2010/02/02/data-connection-dialog-source-code-is-released-on-code-gallery.aspx

Also you can do this programmatically using the DataLink Properties:

Add the reference to ADODB.DLL (from .NET reference) and Microsoft OLE DB Service Component 1.0 Type Library from the COM tab in your visual studio reference tab.

using ADODB;
using Microsoft.Win32;
public partial class ConnectionStringStep : Form
    {
        private const string MSSQL_PROVIDER = "Provider=SQLOLEDB.1";

        private const string ORACLE_PROVIDER = "Provider=MSDAORA.1";

        private const string MSSQL = "MSSQL";

        public ConnectionStringStep()
        {
            InitializeComponent();
        }


        private static string DataBaseType()
        {
            //get the data from some previous screen or some kind of storage
            return MyStorage.GetProperty("DATABASE_TYPE") ?? "MSSQL";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dataBaseType = DataBaseType();

            var adodbConnection = new Connection
                                      {
                                          ConnectionString = dataBaseType == MSSQL ? MSSQL_PROVIDER : ORACLE_PROVIDER
                                      };

            object connection = (object) adodbConnection;

            var dialog = new MSDASC.DataLinks();

            dialog.PromptEdit(ref connection);

            connectionTextBox.Text = adodbConnection.ConnectionString;
        }
    }

DataLink Properties Reference

查看更多
我只想做你的唯一
5楼-- · 2019-04-17 08:19

There is now a NuGet package by Microsoft providing this dialog:

DataConnectionDialog.

Sample usage:

var dialog = new DataConnectionDialog();
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.ConnectionString = connectionString;

if (DataConnectionDialog.Show(dialog) == System.Windows.Forms.DialogResult.OK)
{
    connectionString = dialog.ConnectionString;
}
查看更多
Explosion°爆炸
6楼-- · 2019-04-17 08:22

It's related, but I'm now sure how you can embed this behavior inside your application.

Every time I need one, I create an empty text file, changed its file extension to ".udl" and double-click it; when I'm done, I close that application, rename that file back to ".txt" and open with Notepad.

查看更多
登录 后发表回答