我試圖將用戶ID存儲在我的配置文件中,然後我想通過我的程序訪問許多原因。我在配置文件中有這樣的appSettings
部分;將值存儲在appSettings
<appSettings>
<add key="userID" value="1" />
</appSettings>
然後我在一個方法中設置值;
private void hrButton_Click(object sender, RoutedEventArgs e)
{
DataAccessor da = new DataAccessor();
DataRowView dataRow = (DataRowView)dataGrid.SelectedItem;
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("userID", dataRow.Row.ItemArray[0].ToString());
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
da.SetUserDetails();
NavigationService.Navigate(new Uri(@"View/HRSystem/HRSystem.xaml", UriKind.Relative));
}
在試圖訪問並在另一箇中使用它之前;
public List<string> SetUserDetails()
{
string userID = ConfigurationManager.AppSettings.Get("userID");
MessageBox.Show("userID: "+userID);
string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
using (OleDbConnection dbfCon = new OleDbConnection(constr))
{
try
{
dbfCon.Open();
OleDbDataReader myReader;
OleDbCommand sqlCmd = new OleDbCommand("SELECT em_pplid FROM employs WHERE em_pplid = "+ userID + "", dbfCon);
myReader = sqlCmd.ExecuteReader();
List<string> listUser = new List<string>();
while (myReader.Read())
{
listUser.Add(myReader[0].ToString());
return listUser;
}
}
catch (MySqlException)
{
throw;
}
}
return null;
}
然而,當我在消息框中它仍然設置爲1。我以前沒有使用過的appSettings
部分顯示userID
,是有一個原因它始終是1?我正在運行Visual Studio 2015,C#WPF。
感謝您的回覆,雖然即使在做這個用戶ID仍然被設置爲1 – CBreeze
更新:使用設置文件方法代替工作,感謝您的幫助。 – CBreeze