带有 SqlConnection 外部文件的 ConnectionString

ConnectionString with external file for SqlConnection

VisualStudio 2017 上的 C#。Windows 表单应用程序。

大家好。我在网上读到无法使用 .udl 文件,其中为 SqlConnection 写入 ConnectionString 。今天是这样吗?而且,如果是,还有另一种方法可以在 SqlConnetion?

中为 ConnectionString 使用外部文件

我必须 运行 在 5 台具有不同连接字符串的 PC 上进行投影,例如:

PC1) Data Source=PCNAME\SQLEXPRESS;Initial Catalog=DBNEW;User ID=sa;Password=123;

PC2) Data Source=SERVER\SQLEXPRESS;Initial Catalog=DB;User ID=sa;Password=999;

[...]

目前我在项目中使用的是字符串

string connSQL = "Data Source=.\SQLEXPRESS;Initial Catalog=DBNEW;Persist Security Info=True;User ID=sa;Password=123;";

五台电脑不同的连接方式,我要改五次。

无论如何我都尝试过连接 .udl 文件

string connSQL = "Data Source=.\SQLEXPRESS;AttachDbFile=C:\connstring.udl";

包含这个

[oledb]
; Everything after this line is an OLE DB initstring
Data Source=PCNAME\SQLEXPRESS;Initial Catalog=DBNEW;Persist Security Info=True;User ID=sa;Password=123;

当然不行。

有替代解决方案的想法吗?

终于找到解决办法了。还要感谢 MethodMan 的评论我后来意识到你可以使用外部文件来编译 connectionString,即 MyProjectName.exe.config,与软件exe保存在同一个目录下,与App.config.

所以我所做的是创建一个新的 FormConn,您可以在其中手动输入 connectionString 的数据,然后在 [=28= 中覆盖它们]MyProjectName.exe.config和link这个文件到Form1用于SQL数据库管理。代码下方。

Form1.cs:

using System.Configuration;

public Form1()
{
    //Check if a connectionString already exists
    //To the first slot there is a system configuration so 1 = no custom connectionString
    if (ConfigurationManager.ConnectionStrings.Count == 1)
        {
            FormConn frmConn = new FormConn();
            frmConn.ShowDialog();
            try
            {
                //Restart Form1 in the case connectionString has changed
                Application.Restart();
            }
            catch(Exception ex)
            {
                 MessageBox.Show(ex.Message);
            }
        }
        //Associates the custom connectionString to the string I will use in the code for operations that are connected to the DB.
        StringSQL = ConfigurationManager.ConnectionStrings[1].ConnectionString;
}

FormConn.cs:

using System.Configuration;
using System.Reflections;

string appPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        string appName = Environment.GetCommandLineArgs()[0];
        string configFile = System.IO.Path.Combine(appPath, appName + ".config");
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
        configFileMap.ExeConfigFilename = configFile;
        System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
        var sezione = (ConnectionStringsSection)config.GetSection("connectionStrings");<br>sezione.ConnectionStrings["MyProjectName.Properties.Settings.MyDataSetConnectionString"].ConnectionString = "Data Source=" + txtDataSource.Text + ";Initial Catalog=" + txtInitialCatalog.Text + ";Persist Security Info=True;User ID=" + txtUserID.Text + ";Password=" + txtPassword.Text + ";";
        config.Save();
        ConfigurationManager.RefreshSection("connectionStrings");
        this.Close();

这就是我的 MyProjectName.exe.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
<connectionStrings>
    <add name="MyProjectName.Properties.Settings.MyDataSetConnectionString"
        connectionString="Data Source=MyPcName\SQLEXPRESS;Initial Catalog=DB-1;Persist Security Info=True;User ID=sa;Password=123psw321"
        providerName="System.Data.SqlClient" />
</connectionStrings>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

这对我有用!