1
<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")在应用程序配置中添加它?

4

1 回答 1

2

在您的应用配置中

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

然后你可以访问它

Server myServer = new Server(ConfigurationManager.AppSettings["ServerName"].ToString());
于 2013-09-10T04:18:12.820 回答