如何在 C# 中获取 class 值
How to get class values in C#
我已经创建了一个 class UserInfo
并且我已经设置了用户登录响应的值,现在我不知道如何在我的应用程序的另一部分(另一种形式)访问这些值.
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MYCUSTOMAPP
{
class UserInfo
{
public string userId;
public string userType;
public string userName;
public string userUserName;
public string userPhone;
public string userIdCard;
}
}
Login
(将值设为 class 个)
//....
SqlDataReader result = cmd.ExecuteReader();
if (result.HasRows)
{
while (result.Read())
{
UserInfo loggedUser = new UserInfo();
loggedUser.userId = result.GetValue(result.GetOrdinal("Id")).ToString();
loggedUser.userName = result.GetValue(result.GetOrdinal("Name")).ToString();
loggedUser.userType = result.GetValue(result.GetOrdinal("UserType")).ToString();
loggedUser.userUserName = result.GetValue(result.GetOrdinal("UserName")).ToString();
loggedUser.userPhone = result.GetValue(result.GetOrdinal("Phone")).ToString();
loggedUser.userIdCard = result.GetValue(result.GetOrdinal("IdCard")).ToString();
}
}
// the rest...
问题
现在假设我处于 MainWindow
形式,我怎样才能获得 userId
的值?
更新
Logic
- 用户登录
- 将用户数据存储在 class(或您可能建议的任何其他内容)
- 在我的应用程序中全局获取这些用户数据,这样我就不需要在每次需要用户信息时都调用数据库(它会已经显示)
更新 2
我的登录功能
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCUSTOMAPPDatabaseString"].ConnectionString))
{
if (cn.State == ConnectionState.Closed)
cn.Open();
using (DataTable dt = new DataTable())
{
using (SqlCommand cmd = new SqlCommand("dbo.LoginUser", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", usernameBox.Text);
cmd.Parameters.AddWithValue("@Password", passwordBox.Text);
SqlDataReader result = cmd.ExecuteReader();
if (result.HasRows)
{
while (result.Read())
{
// Name of logged user from database (this is database response)
Console.WriteLine(result.GetValue(result.GetOrdinal("Name")));
// Add logged user info to `UserInfo` class
UserInfo loggedUser = new UserInfo();
loggedUser.userId = result.GetValue(result.GetOrdinal("Id")).ToString();
loggedUser.userName = result.GetValue(result.GetOrdinal("Name")).ToString();
loggedUser.userType = result.GetValue(result.GetOrdinal("UserType")).ToString();
loggedUser.userUserName = result.GetValue(result.GetOrdinal("UserName")).ToString();
loggedUser.userPhone = result.GetValue(result.GetOrdinal("Phone")).ToString();
loggedUser.userIdCard = result.GetValue(result.GetOrdinal("IdCard")).ToString();
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
Show();
}
}
else
{
cn.Close();
MessageBox.Show("Your credentials are not match!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
我想你可以在调用另一个窗体的构造函数时将它作为参数传递,如果你直接调用那个窗体。就像在这种情况下:Communicate between two windows forms in C#
但是,如果您以后需要加载它,您可以将其存储为用户设置:https://www.youtube.com/watch?v=P432z8q9iVE。或者更结构化的方法是将数据存储在数据库中,例如 MySQL、MSSQL 或您可以在用户单击号召性用语按钮时放置的其他数据库。
已解决
我已将我的 class 更改为以下
class UserInfo
{
private string userId1;
private static string userType1;
private static string userName1;
private static string userUserName1;
private static string userPhone1;
private static string userIdCard1;
public string GetuserId()
{
return userId1;
}
public void SetuserId(string value)
{
userId1 = value;
}
public string GetuserType()
{
return userType1;
}
public void SetuserType(string value)
{
userType1 = value;
}
public string GetuserName()
{
return userName1;
}
public void SetuserName(string value)
{
userName1 = value;
}
public string GetuserUserName()
{
return userUserName1;
}
public void SetuserUserName(string value)
{
userUserName1 = value;
}
public string GetuserPhone()
{
return userPhone1;
}
public void SetuserPhone(string value)
{
userPhone1 = value;
}
public string GetuserIdCard()
{
return userIdCard1;
}
public void SetuserIdCard(string value)
{
userIdCard1 = value;
}
}
并将我的登录响应代码更改为:
while (result.Read())
{
UserInfo loggedUser = new UserInfo();
loggedUser.SetuserId(result.GetValue(result.GetOrdinal("Id")).ToString());
loggedUser.SetuserName(result.GetValue(result.GetOrdinal("Name")).ToString());
loggedUser.SetuserType(result.GetValue(result.GetOrdinal("UserType")).ToString());
loggedUser.SetuserUserName(result.GetValue(result.GetOrdinal("UserName")).ToString());
loggedUser.SetuserPhone(result.GetValue(result.GetOrdinal("Phone")).ToString());
loggedUser.SetuserIdCard(result.GetValue(result.GetOrdinal("IdCard")).ToString());
}
现在我可以通过以下任何形式获取我的值:
UserInfo loggedUser = new UserInfo();
loggedUser.GetuserName(); // will return logged user name
我已经创建了一个 class UserInfo
并且我已经设置了用户登录响应的值,现在我不知道如何在我的应用程序的另一部分(另一种形式)访问这些值.
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MYCUSTOMAPP
{
class UserInfo
{
public string userId;
public string userType;
public string userName;
public string userUserName;
public string userPhone;
public string userIdCard;
}
}
Login
(将值设为 class 个)
//....
SqlDataReader result = cmd.ExecuteReader();
if (result.HasRows)
{
while (result.Read())
{
UserInfo loggedUser = new UserInfo();
loggedUser.userId = result.GetValue(result.GetOrdinal("Id")).ToString();
loggedUser.userName = result.GetValue(result.GetOrdinal("Name")).ToString();
loggedUser.userType = result.GetValue(result.GetOrdinal("UserType")).ToString();
loggedUser.userUserName = result.GetValue(result.GetOrdinal("UserName")).ToString();
loggedUser.userPhone = result.GetValue(result.GetOrdinal("Phone")).ToString();
loggedUser.userIdCard = result.GetValue(result.GetOrdinal("IdCard")).ToString();
}
}
// the rest...
问题
现在假设我处于 MainWindow
形式,我怎样才能获得 userId
的值?
更新
Logic
- 用户登录
- 将用户数据存储在 class(或您可能建议的任何其他内容)
- 在我的应用程序中全局获取这些用户数据,这样我就不需要在每次需要用户信息时都调用数据库(它会已经显示)
更新 2
我的登录功能
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCUSTOMAPPDatabaseString"].ConnectionString))
{
if (cn.State == ConnectionState.Closed)
cn.Open();
using (DataTable dt = new DataTable())
{
using (SqlCommand cmd = new SqlCommand("dbo.LoginUser", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserName", usernameBox.Text);
cmd.Parameters.AddWithValue("@Password", passwordBox.Text);
SqlDataReader result = cmd.ExecuteReader();
if (result.HasRows)
{
while (result.Read())
{
// Name of logged user from database (this is database response)
Console.WriteLine(result.GetValue(result.GetOrdinal("Name")));
// Add logged user info to `UserInfo` class
UserInfo loggedUser = new UserInfo();
loggedUser.userId = result.GetValue(result.GetOrdinal("Id")).ToString();
loggedUser.userName = result.GetValue(result.GetOrdinal("Name")).ToString();
loggedUser.userType = result.GetValue(result.GetOrdinal("UserType")).ToString();
loggedUser.userUserName = result.GetValue(result.GetOrdinal("UserName")).ToString();
loggedUser.userPhone = result.GetValue(result.GetOrdinal("Phone")).ToString();
loggedUser.userIdCard = result.GetValue(result.GetOrdinal("IdCard")).ToString();
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
Show();
}
}
else
{
cn.Close();
MessageBox.Show("Your credentials are not match!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
我想你可以在调用另一个窗体的构造函数时将它作为参数传递,如果你直接调用那个窗体。就像在这种情况下:Communicate between two windows forms in C#
但是,如果您以后需要加载它,您可以将其存储为用户设置:https://www.youtube.com/watch?v=P432z8q9iVE。或者更结构化的方法是将数据存储在数据库中,例如 MySQL、MSSQL 或您可以在用户单击号召性用语按钮时放置的其他数据库。
已解决
我已将我的 class 更改为以下
class UserInfo
{
private string userId1;
private static string userType1;
private static string userName1;
private static string userUserName1;
private static string userPhone1;
private static string userIdCard1;
public string GetuserId()
{
return userId1;
}
public void SetuserId(string value)
{
userId1 = value;
}
public string GetuserType()
{
return userType1;
}
public void SetuserType(string value)
{
userType1 = value;
}
public string GetuserName()
{
return userName1;
}
public void SetuserName(string value)
{
userName1 = value;
}
public string GetuserUserName()
{
return userUserName1;
}
public void SetuserUserName(string value)
{
userUserName1 = value;
}
public string GetuserPhone()
{
return userPhone1;
}
public void SetuserPhone(string value)
{
userPhone1 = value;
}
public string GetuserIdCard()
{
return userIdCard1;
}
public void SetuserIdCard(string value)
{
userIdCard1 = value;
}
}
并将我的登录响应代码更改为:
while (result.Read())
{
UserInfo loggedUser = new UserInfo();
loggedUser.SetuserId(result.GetValue(result.GetOrdinal("Id")).ToString());
loggedUser.SetuserName(result.GetValue(result.GetOrdinal("Name")).ToString());
loggedUser.SetuserType(result.GetValue(result.GetOrdinal("UserType")).ToString());
loggedUser.SetuserUserName(result.GetValue(result.GetOrdinal("UserName")).ToString());
loggedUser.SetuserPhone(result.GetValue(result.GetOrdinal("Phone")).ToString());
loggedUser.SetuserIdCard(result.GetValue(result.GetOrdinal("IdCard")).ToString());
}
现在我可以通过以下任何形式获取我的值:
UserInfo loggedUser = new UserInfo();
loggedUser.GetuserName(); // will return logged user name