0

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 ?

1
  • This is your insert code. Did you try anything about display? Commented Jun 20, 2014 at 11:57

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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 !
0
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();

}

1 Comment

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
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.