How to Display Data in C# form mysql table?

Related searches

I am new on C# . I create a C# Console program and insert some data to MySql by following code .

string connection = "Server=localhost;Database=user_table;Uid=root;Pwd=";
MySqlConnection dbcon = new MySqlConnection(connection);
MySqlCommand cmd;
dbcon.Open();

cmd = dbcon.CreateCommand();
cmd.CommandText = "INSERt INTO user_table(user_name,amount)   VALUES(@user_name,@amount)";
cmd.Parameters.AddWithValue("@user_name","Niloy");
cmd.Parameters.AddWithValue("@amount", "456");
cmd.ExecuteNonQuery();

Now I want to retrieve this data and display in console application . like This

Niloy 234

Joy 500

Minal 230

how can i do this ?

You have to do the opposite of that you have already done for the insertion of data.

// You sql command
MySqlCommand selectData;

// Create the sql command
selectData = dbcon.CreateCommand();

// Declare the sript of sql command
selectData.CommandText = "SELECT user_name, amount, FROM user_table";

// Declare a reader, through which we will read the data.
MySqlDataReader rdr = selectData.ExecuteReader();

// Read the data
while(rdr.Read())
{
    string userName = (string)rdr["user_name"];
    string amount = (string)rdr["amount"];

    // Print the data.
    Console.WriteLine(username+" "+amount);
}

rdr.Close();

Insert, Update, Delete, Display Data in MySQL Using C#, This article shows how to insert, update, delete and display data in MySQL. is my connection string i have assigned the database file address path query in which i am taking input from the user through windows forms� MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. We can use MySQL with C#, Java, and many other languages. Here we will use C#.

using (dbcon)
{
dbcon.Open();
cmd = dbcon.CreateCommand();
cmd.CommandText = "Select user_name,amount from   user_table";

 MySqlReader  sqlreader = cmd.ExecuteReader();

              while (sqlreader.Read())
              {
                 Console.WriteLine(sqlreader[0].ToString()+ " "+(sqlreader[1].ToString());
              }
              sqlreader.Close();

}

C#, #To. C# - How To Connect C# To MySQL Database And Display Data [ with source code Duration: 7:48 Posted: Aug 7, 2016 welcome to another c# tutorial in this c# tuturial we will create a connection between csharp and mysql database and diplay data in a datagridview you should have xampp or wampp or lampp

You can use gridview to display your data, Using the similar way you used to insert data into your Table.

string connection = "Server=localhost;Database=user_table;Uid=root;Pwd=";
MySqlConnection dbcon = new MySqlConnection(connection);
DataTable dt = new DataTable();

MySqlCommand cmd;
dbcon.Open();

cmd = dbcon.CreateCommand();
cmd.CommandText = "SELECT * from user_table";
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
Gridview1.DataSource=dt;
Gridview1.DataBind();

C# MySQL tutorial - programming MySQL in C#, If we need more extensive processing of data, or we want to bind a Winforms control to a database table, the DataSet is preferred. C# MySQL� You're missing a "FROM" and table name in your SQL statement - "SELECT * FROM tablename". Also, it's best practice to avoid concatenating SQL strings like that - look into using parameterised queries instead to help avoid SQL injection attacks. – Diado Nov 2 '17 at 12:11

How to display data from a table (MySQL) using Visual Studio C# , Originally Answered: How do I display Data from a table(MySql) using visual studios c#. First thing you need is to connect your form with MySql database . Solution 1. Accept Solution Reject Solution. 1) Create a connection to your database. 2) Query for the data you want to show. 3) Bind that data to a datagridview control that you have placed on your form. 4) When something code related in this process doesn't work, ask that specific question here so we can help you.

How To Search Data In MySQL And Display It On DataGridView , C# Tutorial - How To Find Data In MySQL Database And Display The Result In C# Code Duration: 8:32 Posted: Nov 1, 2015 I added the mysql.data.mysqlclient reference. I created a database using phpMyAdmin, and in my table I have id name and surname where id is primary and on auto increment with 11 digits and name surname are type of varchar with the limitation of 255 chars and the rest of the code are above.

Very often you will need to use a MySQL table to store data inside it and then output that data by using a PHP script. To display the table data it is best to use HTML, which upon filling in some data on the page invokes a PHP script which will update the MySQL table. To populate a new database table with data you will first need an HTML page which will collect that data from the user. The following HTML code that and passes the information to a PHP script:

Comments
  • This is your insert code. Did you try anything about display?
  • Thanks a lot for giving your valuable time. When I create MySqlReader rdr = selectData.ExecuteReader(); Object , MySqlReader get Red sign , I think its cant recognize MySqlReader . What whould I do ? @Christos
  • I didn't See MySqlReader in suggestion list when i m writing (Drop down menu )
  • @Niloy yeah, you are right ! I am sorry. I just updated my post. Please let me know, if that's ok now. Thanks !
  • Thanks a lot for giving your valuable time. When I create MySqlReader rdr = selectData.ExecuteReader(); Object , MySqlReader get Red sign , I think its cant recognize MySqlReader . What whould I do ? @apomene