[英]How to display the user's name on a messagebox in c#?
我想在用户登录后说出用户名(例如)“欢迎布鲁斯”或“欢迎蝙蝠侠”之类的东西。 如何获取特定列的值并将其显示在消息框上? 我正在使用visualt studio c#2008和ms sql 2005. windows窗体
using (SqlConnection conn = new SqlConnection("Data Source=MJ-PC\\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM tblUsers WHERE U_Name=@U_Name AND U_Pass=@U_Pass", conn);
cmd.Parameters.Add("@U_Name", SqlDbType.VarChar).Value = textBox1.Text;
cmd.Parameters.Add("@U_Pass", SqlDbType.VarChar).Value = textBox2.Text;
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
dr.Read();
int userType = Convert.ToInt32(dr["U_Type"]);
if (userType == 1)
{
MessageBox.Show("Login Successful");
MDIParent1 settingsForm = new MDIParent1();
settingsForm.Show();
this.Hide();
}
else if (userType == 2)
{
MessageBox.Show("Login Successful");
MDIParent2 settingsForm = new MDIParent2();
settingsForm.Show();
this.Hide();
}
}
else
{
MessageBox.Show("Login Failed");
Outcome = Convert.ToInt32(lblOutcome.Text);
Outcome = Outcome - 1;
textBox1.Clear();
textBox2.Clear();
lblOutcome.Text = Outcome.ToString();
if (Outcome == 0)
{
MessageBox.Show("You have reached the maximum number of trial");
this.Close();
}
}
这是我的代码
在我的数据库中我有一个U_Name和F_Name,我想显示F_Name
您可以在Login Successful上使用下面提到的代码。
MessageBox.Show("welcome "+textBox1.Text);
您已经可以访问数据(假设它位于查询返回的同一行中),因此只需弹出另一个使用该值的MessageBox
:
if (dr.HasRows)
{
dr.Read();
int userType = Convert.ToInt32(dr["U_Type"]);
MessageBox.Show("Welcome, " + dr["F_Name"].ToString())
if (userType == 1)
{
MessageBox.Show("Login Successful");
MDIParent1 settingsForm = new MDIParent1();
settingsForm.Show();
this.Hide();
}
else if (userType == 2)
{
MessageBox.Show("Login Successful");
MDIParent2 settingsForm = new MDIParent2();
settingsForm.Show();
this.Hide();
}
}
你可以试试:
Messagebox.Show(Context.User.Identity.Name.ToString());
如果你想要一个像这样的用户名的一部分,你可以像这样使用split方法,
Context.User.Identity.Name.ToString().Split('\\')[1]
将它用于所有子表单。
static class Program { public static string sCurrentUserName; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
在你的方法中:
if (dr.HasRows)
{
dr.Read();
int userType = Convert.ToInt32(dr["U_Type"]);
if (userType == 1)
{
MessageBox.Show("Login Successful");
MDIParent1 settingsForm = new MDIParent1();
settingsForm.Show();
this.Hide();
}
else if (userType == 2)
{
MessageBox.Show("Login Successful");
MDIParent2 settingsForm = new MDIParent2();
settingsForm.Show();
this.Hide();
}
Program.sCurrentUserName = dr["F_Name"].ToString();
}
else
{
Program.sCurrentUserName = string.Empty;
//other code
}
如何在子表单中使用它 :
public partial class Form2: Form
{
public Form2()
{
}
private void Form2_Load(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(Program.sCurrentUserName) == false)
MessageBox.Show("Welcome " + Program.sCurrentUserName);
else
{
//do something
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.