如何在文本框中显示来自mysql的数据 [英] how to display data from mysql in textboxes

查看:79
本文介绍了如何在文本框中显示来自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

推荐答案

只是制作它真的很简单。如果您只想显示1列。不要使用SELECT *,而只选择你需要的列。

Just make it really simple. If you only want 1 column to be display. Do not use "SELECT *", but select only does columns that you need.
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();
}


for SQL:



for SQL:

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! :)


MySqlConnection con = new MySqlConnection();



MySqlCommand com = new MySqlCommand();



MySqlDataReader dr;



con = new MySqlConnection(ConfigurationManager.ConnectionStrings [netConnectionString]。ToString());

con.Open();

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

dr = com.ExecuteReader();

if(dr.Read())

{

Label6.Text = dr [0] .ToString();

TextBox4.Text = dr [0] .ToString();

}
MySqlConnection con = new MySqlConnection();

MySqlCommand com = new MySqlCommand();

MySqlDataReader dr;

con = new MySqlConnection(ConfigurationManager.ConnectionStrings["netConnectionString"].ToString());
con.Open();
com = new MySqlCommand("select password from users where user_name='" + Session["user"] + "'", con);
dr = com.ExecuteReader();
if (dr.Read())
{
Label6.Text = dr[0].ToString();
TextBox4.Text = dr[0].ToString();
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆