如何通过登录的个人资料ID在标签中显示名字和姓氏?

huangapple go评论68阅读模式
英文:

How to display the First Name and Last Name in the Lable by the ID of the profile you are logged into?

问题

以下是您要翻译的部分:

我正在创建一个带有MySQL数据库的WPF应用程序,我编写了以下代码,以便用户名将显示在标签中,但出于某种原因,它只给我显示了数据库中的最后一个用户。我该如何做才能登录到帐户并只看到与之相关的信息?也就是说,我登录到用户1,然后在字段中只看到用户1,例如不是用户101

我尝试添加一个ID字段,但在这样做之后,我的所有标签都开始填充表中的最后一个ID

为了能够显示姓和名,您需要:

创建一个类,其中将存储变量:
用户ID
用户名
```csharp
public class global
{
    public static int userid;
    public static string username;
}

接下来,在检查字符串是否已填充时,您需要声明变量并将其转换为字符串

DB db = new DB();
string userName = TB_Name.Text;
string userPass = PB_Password.Password;

DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();

MySqlCommand command = new MySqlCommand("SELECT * FROM Login WHERE Login = @uL AND Password = @uP AND Role = 2", db.GetConnection());

command.Parameters.Add("@uL", MySqlDbType.VarChar).Value = userName;
command.Parameters.Add("@uP", MySqlDbType.VarChar).Value = userPass;

adapter.SelectCommand = command;
adapter.Fill(table);
if (table.Rows.Count > 0)
{
    global.userid = Convert.ToInt32(table.Rows[0]["ID"].ToString());
    global.username = table.Rows[0]["Login"].ToString();
    UserPanel();
}

之后,在用户行中,您需要将用户ID的值分配给带有ID的标签

string query = $"SELECT FirstName, LastName, ID, img FROM Login WHERE ID = {ID_Label.Content = global.userid}";
MySqlCommand cmd = new MySqlCommand(query, db.GetConnection());
db.openConnection();
MySqlDataReader myReader = cmd.ExecuteReader();

try
{
    while (myReader.Read())
    {
        FN_Label.Content = myReader.GetString("FirstName");
        LN_Label.Content = myReader.GetString("LastName");
    }
}

<details>
<summary>英文:</summary>

I am creating a WPF application with a MySQL database, I wrote the following code so that the username would be displayed in the Lable, but for some reason it gives me only the last user in my database. How can I make it so that I would log into the account and see information only on it? Those. I go under user 1 and I will see user 1 in the field, and for example not user 101

```   string conn =
                &quot;server=; username=; password=; database=u1831430_excurtion&quot;;
            MySqlConnection connectionString = new MySqlConnection(conn);
            MySqlCommand command = connectionString.CreateCommand();
            command.CommandText = $&quot;SELECT `FirstName` FROM `Login` WHERE 1&quot;;
            command.CommandText = $&quot;SELECT `LastName` FROM `Login` WHERE 1&quot;;
            //command.CommandText = $&quot;SELECT `ID` FROM `Login` WHERE 1&quot;;
            MySqlDataReader myReader;

            try
            {
                connectionString.Open();
                myReader = command.ExecuteReader();

                while (myReader.Read())
                {
                    //ID_Label.Content = myReader[0].ToString();
                    FN_Label.Content = myReader[0].ToString();
                    LN_Label.Content = myReader[0].ToString();
                }
            }

I tried to add a field with an ID, but after I did that, all my Labels began to fill in with the last ID from the table

To make it possible to display the last name and first name, you need:

Create a class where variables will be stored:
user ID
Username

public class global
    {
        public static int userid;
        public static string username;
    }

Next, when checking strings for fullness, you need to declare variables and translate them into a string

 DB db = new DB();
                string userName = TB_Name.Text;
                string userPass = PB_Password.Password;

                DataTable table = new DataTable();
                MySqlDataAdapter adapter = new MySqlDataAdapter();

                MySqlCommand command = new MySqlCommand(&quot;SELECT * FROM Login WHERE Login = @uL AND Password = @uP AND Role = 2&quot; , db.GetConnection());

                command.Parameters.Add(&quot;@uL&quot;, MySqlDbType.VarChar).Value = userName;
                command.Parameters.Add(&quot;@uP&quot;, MySqlDbType.VarChar).Value = userPass;

                adapter.SelectCommand = command;
                adapter.Fill(table);
                if (table.Rows.Count &gt; 0)
                {
                    global.userid = Convert.ToInt32(table.Rows[0][&quot;ID&quot;].ToString());
                    global.username = table.Rows[0][&quot;Login&quot;].ToString();
                   UserPanel();
                }

After that, in the user line, you need to assign the value of the user ID to your label with the ID

 string query = $&quot;SELECT FirstName, LastName, ID, img FROM Login WHERE ID = {ID_Label.Content = global.userid}&quot;;
            MySqlCommand cmd = new MySqlCommand(query, db.GetConnection());
            db.openConnection();
            MySqlDataReader myReader = cmd.ExecuteReader();

            try
            {
                while (myReader.Read())
                {
                    FN_Label.Content = myReader.GetString(&quot;FirstName&quot;);
                    LN_Label.Content = myReader.GetString(&quot;LastName&quot;);
                }
            }

答案1

得分: 0

为了能够显示姓和名,需要执行以下步骤:

创建一个类来存储变量:
用户ID
用户名

public class global
{
    public static int userid;
    public static string username;
}

接下来,在检查字符串是否为空时,需要声明变量并将它们转换为字符串:

DB db = new DB();
string userName = TB_Name.Text;
string userPass = PB_Password.Password;

DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();

MySqlCommand command = new MySqlCommand("SELECT * FROM Login WHERE Login = @uL AND Password = @uP AND Role = 2", db.GetConnection());

command.Parameters.Add("@uL", MySqlDbType.VarChar).Value = userName;
command.Parameters.Add("@uP", MySqlDbType.VarChar).Value = userPass;

adapter.SelectCommand = command;
adapter.Fill(table);
if (table.Rows.Count > 0)
{
    global.userid = Convert.ToInt32(table.Rows[0]["ID"].ToString());
    global.username = table.Rows[0]["Login"].ToString();
    UserPanel();
}

然后,在用户行中,需要将用户ID的值分配给带有ID的标签:

string query = $"SELECT FirstName, LastName, ID, img FROM Login WHERE ID = {ID_Label.Content = global.userid}";
MySqlCommand cmd = new MySqlCommand(query, db.GetConnection());
db.openConnection();
MySqlDataReader myReader = cmd.ExecuteReader();

try
{
    while (myReader.Read())
    {
        FN_Label.Content = myReader.GetString("FirstName");
        LN_Label.Content = myReader.GetString("LastName");
    }
}
英文:

To make it possible to display the last name and first name, you need:

Create a class where variables will be stored:
user ID
Username

public class global
    {
        public static int userid;
        public static string username;
    }

Next, when checking strings for fullness, you need to declare variables and translate them into a string

 DB db = new DB();
                string userName = TB_Name.Text;
                string userPass = PB_Password.Password;

                DataTable table = new DataTable();
                MySqlDataAdapter adapter = new MySqlDataAdapter();

                MySqlCommand command = new MySqlCommand(&quot;SELECT * FROM Login WHERE Login = @uL AND Password = @uP AND Role = 2&quot; , db.GetConnection());

                command.Parameters.Add(&quot;@uL&quot;, MySqlDbType.VarChar).Value = userName;
                command.Parameters.Add(&quot;@uP&quot;, MySqlDbType.VarChar).Value = userPass;

                adapter.SelectCommand = command;
                adapter.Fill(table);
                if (table.Rows.Count &gt; 0)
                {
                    global.userid = Convert.ToInt32(table.Rows[0][&quot;ID&quot;].ToString());
                    global.username = table.Rows[0][&quot;Login&quot;].ToString();
                   UserPanel();
                }

After that, in the user line, you need to assign the value of the user ID to your label with the ID

 string query = $&quot;SELECT FirstName, LastName, ID, img FROM Login WHERE ID = {ID_Label.Content = global.userid}&quot;;
            MySqlCommand cmd = new MySqlCommand(query, db.GetConnection());
            db.openConnection();
            MySqlDataReader myReader = cmd.ExecuteReader();

            try
            {
                while (myReader.Read())
                {
                    FN_Label.Content = myReader.GetString(&quot;FirstName&quot;);
                    LN_Label.Content = myReader.GetString(&quot;LastName&quot;);
                }
            }

huangapple
  • 本文由 发表于 2023年2月16日 03:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定