• 将数组转换成datatable


            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;  
          
                }  
          
            }  
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    调查问卷
    SQL 基础学习(1):下载DB Browser for SQLite. 下载graphviz(为了使用Rails ERD的前提)出现❌,已debug.
    路由完整实例代码
    如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法
    CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器
    SpringMVC的解释与搭建Maven私有代理服务器
    单调队列 bzoj3126 [Usaco2013 Open]Photo
    二分图 crf的军训
    单调队列 JC loves Mkk
    测试开发CICD——Git——window上安装git——配置基本信息
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667848.html
Copyright © 2020-2023  润新知