class ArrayToDataTable { //例子: string[] xqdw = XQDW.Split(','); string[] xmmc = XMMC.Split(','); DataTable UserDt = Convert("USER_CODE", usercode); string[,] XMXX = new string[xqdw.Length, 2]; //给二维数组进行赋值 for (int i = 0; i < xqdw.Length; i++) { for (int j = 0; j < 2; j++) { if (j==0) { XMXX[i,j] = xqdw[i]; } else { XMXX[i,j] = xmmc[i]; } } } DataTable xmxx = Convert(new string[] { "XQDW", "XMMC" }, XMXX); /// <summary> /// 把一个一维数组转换为DataTable /// </summary> /// <param name="ColumnName">列名</param> /// <param name="Array">一维数组</param> /// <returns>返回DataTable</returns> /// <remarks>柳永法 http://www.yongfa365.com/ </remarks> public static DataTable Convert(string ColumnName, string[] Array) { DataTable dt = new DataTable(); dt.Columns.Add(ColumnName, typeof(string)); for (int i = 0; i < Array.Length; i++) { DataRow dr = dt.NewRow(); dr[ColumnName] = Array[i].ToString(); dt.Rows.Add(dr); } return dt; } /// <summary> /// 反一个M行N列的二维数组转换为DataTable /// </summary> /// <param name="ColumnNames">一维数组,代表列名,不能有重复值</param> /// <param name="Arrays">M行N列的二维数组</param> /// <returns>返回DataTable</returns> /// <remarks>柳永法 http://www.yongfa365.com/ </remarks> public static DataTable Convert(string[] ColumnNames, string[,] Arrays) { DataTable dt = new DataTable(); foreach (string ColumnName in ColumnNames) { dt.Columns.Add(ColumnName, typeof(string)); } for (int i1 = 0; i1 < Arrays.GetLength(0); i1++) { DataRow dr = dt.NewRow(); for (int i = 0; i < ColumnNames.Length; i++) { dr[i] = Arrays[i1, i].ToString(); } dt.Rows.Add(dr); } return dt; } /// <summary> /// 反一个M行N列的二维数组转换为DataTable /// </summary> /// <param name="Arrays">M行N列的二维数组</param> /// <returns>返回DataTable</returns> /// <remarks>柳永法 http://www.yongfa365.com/ </remarks> public static DataTable Convert(string[,] Arrays) { DataTable dt = new DataTable(); int a = Arrays.GetLength(0); for (int i = 0; i < Arrays.GetLength(1); i++) { dt.Columns.Add("col" + i.ToString(), typeof(string)); } for (int i1 = 0; i1 < Arrays.GetLength(0); i1++) { DataRow dr = dt.NewRow(); for (int i = 0; i < Arrays.GetLength(1); i++) { dr[i] = Arrays[i1, i].ToString(); } dt.Rows.Add(dr); } return dt; } }