我有一个访问数据库实例的WPF应用程序。我在我的个人计算机上开发,应用程序将在另一台机器和数据库实例上运行。有没有办法或最佳实践将实例名称存储在WPF应用程序之外,我可以在app启动时阅读?目前,当我从一台机器移动到另一台机器时,我对数据库实例名称进行了注释和取消注释。我想在本地机器上存储一些我可以在应用程序启动时读取的东西。
以下是我目前的工作:
try
{
if (sqlConn != null)
{
sqlConn.Close();
}
strConnection = "Data Source=" + strDBInstance + "; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
//strConnection = "Data Source=CASS-LAPTOP\\SQLEXPRESS; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
//strConnection = "Data Source=KENTS-WORK-PC\\CASS_SQLEXPRESS; Initial Catalog=NBFoodPantry;Integrated Security=true; MultipleActiveResultSets = True";
sqlConn = new SqlConnection(@strConnection);
try
{
sqlConn.Open();
}
catch (Exception ex)
{
string strMsg;
WriteErrorLog(strErrorLogFile, "ConnectToDatabase", ex.Message, false);
strMsg = "ConnectToDatabase: SQL Open failed. Please make sure that you database ";
strMsg += "instance and name are correct. Also make sure that the SQL engine is running.";
System.Windows.MessageBox.Show(strMsg);
}
}
catch (Exception ex)
{
string strMsg;
WriteErrorLog(strErrorLogFile, "ConnectToDatabase", ex.Message, false);
strMsg = " ConnectToDatabase: failed with error, " + ex.Message + ".";
System.Windows.MessageBox.Show(strMsg);
}