1

I am getting these records from database MySql version 8.0.17

+-------------------------+
| TABLE_NAME              |
+-------------------------+
| t_contents_s300_1_2021  |
| t_contents_s34d_1_2021  |
| t_contents_s34g_1_2021  |
| t_contents_s3sv_1_2021  |
+-------------------------+

and I used MySqlDataReader to read those records as follows

MySqlDataReader reader = cmd.ExecuteReader();    
// in reader, I have records which comes from database.

while(reader.Read())    
{
   string [] arpp_pro = new string[] {reader["TABLE_NAME"].ToString()};
}

everything works fine...

But I need assigning these values of string [] arpp_pro in array for execute single query INSERT INTO on new table for each values from TABLE_NAME

How to solve this problem.

How can I get all records in array from TABLE_NAME?

Thanks in advance for any help

1
  • I've removed references to ASP.NET as nothing in this question is unique to making web applications. Commented Feb 18, 2021 at 5:39

1 Answer 1

2

I think you want to construct a list:

MySqlDataReader reader = cmd.ExecuteReader();    

List<string> arpp_pro = new List<string>(); // define a list outside of the loop
while(reader.Read())    
{
   // for each row from the database, add the retrieved table name to the list
   arpp_pro.Add(reader["TABLE_NAME"].ToString()); 
}

// code to dos something with arpp_pro here.

I also recommend using the using keyword with your reader to ensure that it's closed/disposed when you are done with it. Example:

List<string> arpp_pro = new List<string>(); // define a list outside of the loop
using(MySqlDataReader reader = cmd.ExecuteReader())
{
    while(reader.Read())    
    {
       // for each row from the database, add the retrieved table name to the list
       arpp_pro.Add(reader["TABLE_NAME"].ToString());
    }
}

If you really need it as an array, you can call string[] arpp_pro_array = arpp_pro.ToArray(); to convert the list to an array. You will need using System.Linq; at the top of your code file for this to work, as ToArray is a LINQ extension method.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for help, really appreciated. Plse see update in the question. I don't understand how to get the single value (table name) from the list
@Edward Why are you reading multiple values if you only need one? Only you know the logic of which table name you want, and what tells you that it's that one.
@John: a little note - since reader will return string values, cast will be faster than calling ToString, that is, (string)reader["TABLE_NAME"] instead of reader["TABLE_NAME"].ToString().
@John pls see logic in the question. For each value from the list arpp_pro I need insert value on the table t_contents_table_name_insert
@Edward You specifically asked for "How can I get all records in array from TABLE_NAME?", which I have answered. Your new edit is something else and should be its own question.
|

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.