有的时候需要用到数组数据转换到为DataTable
写了个静态类供需要的人使用:
Code
/**//// <summary>
/// 把Array数组转换为DataTable
/// </summary>
/// <param name="Colomnname">列名</param>
/// <param name="arr">数组</param>
/// <returns></returns>
public static DataTable ArrayToDataTable(string Colomnname,string[] arr)
{
DataTable dt = new DataTable();
dt.Columns.Add(Colomnname, typeof(string));
for (int i = 0; i < arr.Length; i++)
{
DataRow dr = dt.NewRow();
dr[Colomnname]= arr[i].ToString();
dt.Rows.Add(dr);
}
return dt;
}
/**//// <summary>
/// 把Array数组转换为DataTable
/// </summary>
/// <param name="Colomnname">列名</param>
/// <param name="arr">数组</param>
/// <returns></returns>
public static DataTable ArrayToDataTable(string Colomnname,string[] arr)
{
DataTable dt = new DataTable();
dt.Columns.Add(Colomnname, typeof(string));
for (int i = 0; i < arr.Length; i++)
{
DataRow dr = dt.NewRow();
dr[Colomnname]= arr[i].ToString();
dt.Rows.Add(dr);
}
return dt;
}