本文介绍了如何在文本框中显示来自mysql的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySqlDataReader myReader = null;

MySqlCommand myCommand = new MySqlCommand(select * from users where user_name ='+ Session [user] +',con);



myReader = myCommand.ExecuteReader();



while(myReader.Read())

{

TextBox4.Text =(myReader [user_name]。ToString());

}

MySqlDataReader myReader = null;
MySqlCommand myCommand = new MySqlCommand("select * from users where user_name='" + Session["user"] + "'", con);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
TextBox4.Text = (myReader["user_name"].ToString());
}




i希望将数据从数据库显示到文本框中,但这段代码不起作用请帮助我


i want to display data from db into textboxes but this code dont work help me please

推荐答案

MySqlCommand myCommand = new MySqlCommand("SELECT lastlogin FROM users WHERE user_name=@userName", con);
myCommand.Parameters.AddWithValue("userName", Session["user"].ToString());

object result = myCommand.ExecuteScalar();

if (result == null)
{
  TextBox4.Text = "User not found";
}
else
{
  TextBox4.Text = result.ToString();
}


SqlCommand cmd = new SqlCommand(select <column_1>,<column_2> from <table_name> where <any_condition>, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Label4.Text = dr[0].ToString(); // [0] for <column_1>
                    Label5.Text = dr[1].ToString(); // [1] for <column_2>
                }





在您的情况下:





in your case:

MySqlCommand myCommand = new MySqlCommand("select <column_name> from users where user_name='" + Session["user"] + "'", con);
con.open();
Mysqldatareader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
TextBox4.Text = myReader[0].ToString();
}
conn.close();





享受! :)



enjoy! :)



这篇关于如何在文本框中显示来自mysql的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 14:09