应用配置中的服务器
本文关键字:服务器 配置 应用 | 更新日期: 2023-09-27 18:30:41
<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")
?
在您的
应用程序配置中
<configuration>
<appSettings>
<add key="ServerName" value="PC-1'Instance1" />
</appSettings>
</configuration>
然后您可以将其作为
Server myServer = new Server(ConfigurationManager.AppSettings["ServerName"].ToString());