简体   繁体   中英

Data from Database to Textbox

I am very new to the programming field, like 4 months new, so I think I have a very stupid question for 99% of you.

I am busy creating a very basic booking program to practice and is struggling like a mad pig.

1) I basically want to pull data from MySql to a textbox.
2) My aim for this is that I need to get a loop going for the booking id to be saved in MySql. Not sure if you all would understand what I am saying.

Current Code:

string connection = "datasource = localhost; port = 3306; username = root; password = ****";
string query = "SELECT * FROM databasename.tablename where id='"this.textbox1.Text"' ";
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = new MySqlCommand(query, conn);
MySqlDataReader reader;
conn.Open();
reader = command.ExecuteReader();
while (reader.Read()) 
{

}

Just need to proper use your query, using parameters :

string connection = "datasource = localhost; port = 3306; username = root; password = ****";

string query = "SELECT * FROM databasename.tablename where id=@ID ";

MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = new MySqlCommand(query, conn);
command.Parameters.Add("@ID",textBox1.Text);
MySqlDataReader reader;
conn.Open();

reader = command.ExecuteReader();

while (reader.Read()) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM