C# DataTable转数组

2021-09-08 15:03:19 浏览数 (1)

代码语言:javascript复制
string strsql = $"select name from 表名";
DataTable dt = base.GetDataTable(strsql);
string[] result = new string[dt.Rows.Count];
for (int i = 0; i < result.Length; i  )
{
    foreach (DataColumn item in dt.Columns)
    {
        result[i] = dt.Rows[i][item].ToString();
    }
}
return result;

or

代码语言:javascript复制
string strsql = $"select * from 表名";
DataTable dt = base.GetDataTable(strsql);
string[] result = new string[dt.Rows.Count];
for (int i = 0; i < result.Length; i  )
{
    foreach (DataColumn item in dt.Columns)
    {
        result[i] = dt.Rows[i]["name"].ToString();
    }
}
return result;

我的博客即将同步至腾讯云 社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3mcfkt8o4f408

0 人点赞