【问题标题】:Server in App Config应用配置中的服务器
【发布时间】:2023-04-02 09:20:02
【问题描述】:
<connectionStrings>    
<add name="System1.Properties.Settings.ConnectionString" connectionString="Data Source=PC-1\Instance1;Initial Catalog=System1;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

我在我的应用配置中有这个,然后在我的应用中使用它,如下所示:

ConnectionStringSettings myConnectionString = ConfigurationManager.ConnectionStrings["System1.Properties.Settings.ConnectionString"];
    private void button1_Click(object sender, EventArgs e)
    {
            using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
            {
                myDatabaseConnection.Open();

                using (SqlCommand mySqlCommand1 = new SqlCommand("someCommand")
{
mySqlCommand1.ExecuteNonQuery();
}}}

我也有备份数据库的功能

        Server myServer = new Server(@"PC-1\Instance1");
        Backup bkpDBFull = new Backup();
        bkpDBFull.Action = BackupActionType.Database;
        bkpDBFull.Database = "System1";
        bkpDBFull.Devices.AddDevice(@"D:\Sample.bak", DeviceType.File);
        bkpDBFull.BackupSetName = "Sample";
        bkpDBFull.BackupSetDescription = "Sample";
        bkpDBFull.ExpirationDate = DateTime.Today.AddDays(5);
        bkpDBFull.Initialize = false;
        bkpDBFull.SqlBackup(myServer);

我的问题是如何在应用配置中添加这个Server myServer = new Server(@"PC-1\Instance1")

【问题讨论】:

    标签: c# winforms sql-server-2008-r2 app-config smo


    【解决方案1】:

    在您的应用配置中

    <configuration>
       <appSettings>
           <add key="ServerName" value="PC-1\Instance1" />
       </appSettings>
    </configuration>
    

    然后你可以访问它

    Server myServer = new Server(ConfigurationManager.AppSettings["ServerName"].ToString());
    

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2013-12-24
      相关资源
      最近更新 更多