Sunday, April 11, 2010

Using the ADO.NET Data Connection dialog in your own application – and limiting the choice of data sources

In version 3.0 of the ExportSqlCE SSMS add-in, I have used the newly released Visual Studio ADO.NET Add Data Connection dialog to prompt the user for connection information to the server. This dialog is now available in source code here.

This project consists of this well-know dialog (and others) – used in the Visual Studio Server Explorer:

image

For the purpose of the add-in, I was interested in on the Microsoft SQL Server data source, as this was the target of the scripting code.

This was achieved in the following way:

using Microsoft.Data.ConnectionUI;


DataSource sqlDataSource = new DataSource("MicrosoftSqlServer", "Microsoft SQL Server");
sqlDataSource.Providers.Add(DataProvider.SqlDataProvider);
DataConnectionDialog dcd = new DataConnectionDialog();
dcd.DataSources.Add(sqlDataSource);
dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
dcd.SelectedDataSource = sqlDataSource;
if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
{
string connectionString = dcd.ConnectionString;



 



The result is the following dialog, notice that the Change… button is disabled:



image

No comments: