我可以让我的程序按用户选择数据库吗?

时间:2009-09-14 11:27:14

标签: c# database

我需要在客户端运行Windows窗体应用程序(C#),在另一端运行数据库。

我希望用户在程序中手动选择数据库(SQL Server 2005)。如果是这样,可以将数据库从一台PC更改为另一台PC。

我需要避免连接字符串中的计算机名称或IP号或有用的东西。

如何实现这一目标?

5 个答案:

答案 0 :(得分:3)

您可以根据用户选择更改计算机名称。我们之前已经这样做了,或者在app.config中有多个连接字符串,并使用用户选择从那里打开相应的连接字符串。

<connectionStrings>
        <add name="Default" connectionString="....
        <add name="Second" connectionString="....
        ...
</connectionStrings>


m_connectionStringCollection = new Hashtable();

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection csSection = config.ConnectionStrings;

for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
{
    ConnectionStringSettings cs = csSection.ConnectionStrings[i];
    string connectionName = cs.Name;
    m_connectionStringCollection.Add(connectionName, cs);
}

答案 1 :(得分:0)

答案 2 :(得分:0)

您可以使用以下代码通过Build connection string对话框提示用户 您可以找到原始文章here

/// <summary>
/// Displays a Connection String Builder (DataLinks) dialog.
/// 
/// Credits:
/// http://www.codeproject.com/cs/database/DataLinks.asp
/// http://www.codeproject.com/cs/database/DataLinks.asp?df=100&forumid=33457&select=1560237#xx1560237xx
/// 
/// Required COM references:
/// %PROGRAMFILES%\Microsoft.NET\Primary Interop Assemblies\adodb.dll
/// %PROGRAMFILES%\Common Files\System\Ole DB\OLEDB32.DLL
/// </summary>
/// <param name="currentConnectionString">Previous database connection string</param>
/// <returns>Selected connection string</returns>
private string PromptForConnectionString(string currentConnectionString)
{
    MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
    ADODB.Connection dialogConnection;
    string generatedConnectionString = string.Empty;

    if (currentConnectionString == String.Empty)
    {
        dialogConnection = (ADODB.Connection)dataLinks.PromptNew();
        generatedConnectionString = dialogConnection.ConnectionString.ToString();
    }
    else
    {
        dialogConnection = new ADODB.Connection();
        dialogConnection.Provider = "SQLOLEDB.1";
        ADODB.Property persistProperty = dialogConnection.Properties["Persist Security Info"];
        persistProperty.Value = true;

        dialogConnection.ConnectionString = currentConnectionString;
        dataLinks = new MSDASC.DataLinks();

        object objConn = dialogConnection;
        if (dataLinks.PromptEdit(ref objConn))
        {
            generatedConnectionString = dialogConnection.ConnectionString.ToString();
        }
    }
    generatedConnectionString = generatedConnectionString.Replace("Provider=SQLOLEDB.1;", string.Empty);
    if (
            !generatedConnectionString.Contains("Integrated Security=SSPI")
            && !generatedConnectionString.Contains("Trusted_Connection=True")
            && !generatedConnectionString.Contains("Password=")
            && !generatedConnectionString.Contains("Pwd=")
        )
        if(dialogConnection.Properties["Password"] != null)
            generatedConnectionString += ";Password=" + dialogConnection.Properties["Password"].Value.ToString();

    return generatedConnectionString;
}

答案 3 :(得分:0)

我建议你使用连接字符串构建器,只需将常量部分存储在配置文件中,然后从用户那里继续使用。

http://msdn.microsoft.com/en-us/library/system.data.common.dbconnectionstringbuilder.aspx

System.Data.Common.DbConnectionStringBuilder builder = 
    new System.Data.Common.DbConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";

答案 4 :(得分:0)

Microsoft已在代码库中发布了数据连接对话框的源代码。

以下是blog post from Yaohai,其中包含更多信息,此处为the home of Data Connection Dialog on Code Gallery