C#通过Rows.Count属性获取总行数,然后如何读取每条记录各个字段的值

        在C#中的Datatable数据变量的操作过程中,有时候我们需要知道DataTable中是否含有数据行或者DataTable的数据总行数,此时我们就可以先拿到DataTable中的Rows属性对象,通过Rows属性对象的Count属性即可获取总行数。

      例如我们我们有个DataTable变量为dt,需要通过C#获取该DataTable的总行数可使用下列语句:

int rowCount = dt.Rows.Count;

一、C#获取该DataTable的总行数完整的源程序

   protected void Page_Load(object sender, EventArgs e)
        {
            string conStr = "server=localhost;port=3306;user Id=root;password=331; database=timing ;Allow User Variables=True"; //连接字符串
            MySqlConnection conn = null;

            try
            {
                conn = new MySqlConnection(conStr);
                conn.Open();
                string sql1 = "select * from table1";
                MySqlDataAdapter sda1 = new MySqlDataAdapter(sql1, conn);
                DataTable dt = new DataTable();
                //使用 SqlDataAdapter 对象 sda 将查询结果填充到 DataSet 对象 dt 中
                sda1.Fill(dt);
                int rowCount = dt.Rows.Count;  
            }
            catch (Exception ex)
            {
                //MessageBox.Show("查询失败!" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

二、读取每条记录各个字段的值

int array11 = Convert.ToInt32(dt.Rows[0]["id"].ToString()); //读取第一条记录id字段的值
int array12 = Convert.ToInt32(dt.Rows[0]["ts"].ToString()); //读取第一条记录ts字段的值
int array21 = Convert.ToInt32(dt.Rows[1]["id"].ToString()); //读取第二条记录id字段的值
int array22 = Convert.ToInt32(dt.Rows[1]["ts"].ToString()); //读取第二条记录ts字段的值

读取每条记录各个字段的值完整的源程序

   protected void Page_Load(object sender, EventArgs e)
        {
            string conStr = "server=localhost;port=3306;user Id=root;password=331; database=timing ;Allow User Variables=True"; //连接字符串
            MySqlConnection conn = null;

            try
            {
                conn = new MySqlConnection(conStr);
                conn.Open();
                string sql1 = "select * from table1";
                MySqlDataAdapter sda1 = new MySqlDataAdapter(sql1, conn);
                DataTable dt = new DataTable();
                //使用 SqlDataAdapter 对象 sda 将查询结果填充到 DataSet 对象 dt 中
                sda1.Fill(dt);
                int rowCount = dt.Rows.Count; 
                
                int array11 = Convert.ToInt32(dt.Rows[0]["id"].ToString()); //读取第一条记录id字段的值
                int array12 = Convert.ToInt32(dt.Rows[0]["ts"].ToString()); //读取第一条记录ts字段的值
                int array21 = Convert.ToInt32(dt.Rows[1]["id"].ToString()); //读取第二条记录id字段的值
                int array22 = Convert.ToInt32(dt.Rows[1]["ts"].ToString()); //读取第二条记录ts字段的值
            }
            catch (Exception ex)
            {
                //MessageBox.Show("查询失败!" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值