显示一个对话框的ConnectionString [英] Display a ConnectionString dialog

查看:165
本文介绍了显示一个对话框的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C#程序,应该能够创建,备份和还原SQL Server数据库。



对于这一点,用户需要能够设置一个连接字符串到所需的SQL Server(和数据库)。



我想使用相同的对话框,例如Visual Studio中创建连接字符串



这可能吗?


解决方案

注意:下文提及的对话框组件不再提供下载。除非你已经在过去的检索,你可能不会得到这个答案的示例代码工作。



备选:现在有一个不同 DataConnectionDialog 可在
的NuGet
。请参见详情这个答案







<击> MSDN上归档数据连接对话框图库 (碎截至2015年9月1日)



数据连接对话框与Visual Studio发布的一个数据库工具组件。它允许用户建立连接字符串和连接到特定的数据源。试试这个..



C#示例:

 静态无效的主要(字串[] args)
{
DataConnectionDialog DCD =新DataConnectionDialog();
DataConnectionConfiguration DCS =新DataConnectionConfiguration(NULL);
dcs.LoadConfiguration(DCD);如果(DataConnectionDialog.Show(DCD)== DialogResult.OK)
{
//负载表
使用(SqlConnection的连接=新的SqlConnection(

dcd.ConnectionString ))
{
connection.Open();
的SqlCommand CMD =新的SqlCommand(SELECT * FROM SYS.TABLES连接);使用
(SqlDataReader的读卡器= cmd.ExecuteReader())
{
,而(reader.Read())
{
Console.WriteLine(reader.HasRows);
}
}
}
}
dcs.SaveConfiguration(DCD);
}

下面的源代码也可以。我们可以整合和根据许可我们的应用程序重新分发源代码。




I'm trying to create a program in C# that should be able to create, backup and restore a SQL Server database.

For this, the user needs to be able to setup a connection string to the desired SQL Server (and database).

I would like to use the same dialog as for example Visual Studio for creating the connection string.

Is this possible?

解决方案

Note: The dialog component referred to below is no longer available for download. Unless you have retrieved it in the past, you will probably not get this answer's sample code to work.

Alternative: There is now a different DataConnectionDialog available on NuGet. See this answer for details.


"Data Connection Dialog" on MSDN Archive Gallery (broken as of 1 Sept. 2015)

The data connection dialog is a database tool component released with Visual Studio. It allows users to build connection strings and to connect to specific data sources. try this..

C# Sample:

static void Main(string[] args)
{
    DataConnectionDialog dcd = new DataConnectionDialog();
    DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
    dcs.LoadConfiguration(dcd);

    if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
    {
        // load tables
        using (SqlConnection connection = new SqlConnection(dcd.ConnectionString))
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM sys.Tables", connection);
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.HasRows);
                }
            }
        }
    }
    dcs.SaveConfiguration(dcd);
}

Here source code also available. we can integrate and redistribute the source code with our application according to license.

这篇关于显示一个对话框的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆