如何在c#的消息框中显示用户名?

时间:2014-05-19 06:58:17

标签: c# sql winforms

我想在用户登录后说出用户名(例如)“欢迎Bruce” 或者“欢迎蝙蝠侠”之类的东西。如何获取特定列的值并将其显示在消息框上?我正在使用visualt studio c#2008和ms sql 2005.windows form

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

4 个答案:

答案 0 :(得分:1)

您可以在登录成功时使用下面提到的代码。

MessageBox.Show("welcome "+textBox1.Text);

答案 1 :(得分:1)

您已经可以访问数据(假设它在查询返回的同一行中),因此只需弹出另一个使用该值的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();
    }
}

答案 2 :(得分:0)

您可以尝试:

Messagebox.Show(Context.User.Identity.Name.ToString());

如果您想要这样的用户名的一部分,可以使用拆分方法,

Context.User.Identity.Name.ToString().Split('\\')[1] 

答案 3 :(得分:0)

  1. 将用户名添加到公共静态字符串中。
  2. 将其用于所有子表单。

    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());
        }
    }
    
  3. 在你的方法中:

     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
             }
        }
    }