C# DataTable转数组

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

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

posted @ 2021-06-08 16:52  时光博客  阅读(1069)  评论(0编辑  收藏  举报