我正在尝试将userID存储在我的配置文件中,然后由于多种原因我想在整个程序中访问该文件。我在配置文件中有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;
}
但是,当我在消息框中显示userID
时,它仍然设置为1.我之前没有使用过appSettings
部分,是否有理由始终为1?我正在运行Visual Studio 2015,C#WPF。
答案 0 :(得分:2)
在class A
{
A(int a = 0)
{
std::cout << a;
}
};
config.AppSettings.Settings.Remove("userID");
但保存此类信息的正确方法是使用commit add00ba
设置:
config.AppSettings.Settings.Add();
Retreive:
Properties.Settings.Default.userID = dataRow.Row.ItemArray[0].ToString();
Properties.Settings.Default.Save();
答案 1 :(得分:0)
试
config.AppSettings.Settings.Remove('userID');
config.AppSettings.Settings.Add('userID', dataRow.Row.ItemArray[0].ToString());
但是您不应该将用户ID存储在配置文件中。您应该将其保存在会话或Cookie
中