C# 把data的值装换成string型

本文介绍了一种将DataTable转换为字符串的方法及如何实现dataset的分页显示。通过具体的C#代码示例,展示了如何遍历DataTable并将其内容拼接成字符串,以及如何根据指定的页大小和页索引从dataset中抽取数据进行分页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 public string DataTableToString(string str)
        {
            string str1 = "";
            DBClass db = new DBClass();
            DataTable dt = db.GetDataSet(str).Tables[0];

            StringBuilder strData = new StringBuilder();
            int r = dt.Rows.Count;
            int c = dt.Columns.Count;
            string[,] obj = new string[r, c];
            for (int a = 0; a < r; a++)
            {
                for (int b = 0; b < c; b++)
                {
                    obj[a, b] = dt.Rows[a][b].ToString();
                    str1 += obj[a, b] += " ";
                }
            }
            return str1;

        }

dataset分页显示
         public  DataSet MyFenYe(string sql,int pageSize,int pageIndex)
      {
          string str1 = ""; 
             DBClass db=new DBClass();
             DataSet ds=db.GetDataSet(sql);
             DataSet vds = new DataSet();
      vds = ds.Clone();
     int fromIndex = pageSize*(pageIndex-1);
     int toIndex = pageSize*pageIndex-1;
     for(int i=fromIndex;i<=toIndex;i++)
      {
      if(i>=ds.Tables[0].Rows.Count) 
        break;
       vds.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
       }
     ds.Dispose();
     str1 += vds;
    return vds;
  }


dataset分页显示转换成string  纯自创做法,制作人:唐帅
        public string MySecondFenYe(string sql, int pageSize, int pageIndex)
        {
            string str1 = "";
            DBClass db = new DBClass();
            // DataSet vds = db.GetDataSet(sql);
            DataSet ds = db.GetDataSet(sql);
            DataSet vds = new DataSet();
            vds = ds.Clone();
            int fromIndex = pageSize * (pageIndex - 1);
            int toIndex = pageSize * pageIndex - 1;
            for (int i = fromIndex; i <= toIndex; i++)
            {
                if (i >= ds.Tables[0].Rows.Count)
                    break;
            vds.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
            }
            ds.Dispose();
            DataTable dt = vds.Tables[0];

            StringBuilder strData = new StringBuilder();
            int r = dt.Rows.Count;
            int c = dt.Columns.Count;
            string[,] obj = new string[r, c];
            for (int a = 0; a < r; a++)
            {
                for (int b = 0; b < c; b++)
                {
                    obj[a, b] = dt.Rows[a][b].ToString();
                    str1 += obj[a, b] += " ";
                }
            }
            return str1;

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值