簡體   English   中英

如何在c#的消息框中顯示用戶名?

[英]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] 
  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()); } } 

在你的方法中:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM