[英]How to assign datareader value string array
I am getting these records from database MySql version 8.0.17
我从数据库
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
读取这些记录如下
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
但是我需要在数组中分配这些
string [] arpp_pro
的值,以便在新表上为TABLE_NAME
中的每个值执行单个查询 INSERT INTO
How to solve this problem.如何解决这个问题呢。
How can I get all records in array from TABLE_NAME
?如何从
TABLE_NAME
获取数组中的所有记录?
Thanks in advance for any help提前感谢您的帮助
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.我还建议在您的阅读器中
using
关键字,以确保在您完成后关闭/处理它。 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();
如果你真的需要它作为一个数组,你可以调用
string[] arpp_pro_array = arpp_pro.ToArray();
to convert the list to an array.将列表转换为数组。 You will need
using System.Linq;
您将需要
using System.Linq;
at the top of your code file for this to work, as ToArray
is a LINQ extension method.在代码文件的顶部,因为
ToArray
是 LINQ 扩展方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.