下边就是我的代码,就是取得数据库中所有的内容,放到一个ListBox中,可是我怎么取得记录数呢?string select="select * from test";
SqlConnection conn=new SqlConnection(source);
conn.Open();
SqlCommand cmd=new SqlCommand(select,conn);
SqlDataReader reader=cmd.ExecuteReader();
ListBox1.Items.Clear();
while (reader.Read())
{
ListBox1.Items.Add(reader[0] + "  " + reader[1]);
}

解决方案 »

  1.   

    1. 用 适配器 填充  数据集 中的数据表,表的纪录数2. 用 Cmd.ExecuteNonQuery() 就会返回影响纪录的行数
      

  2.   

    如果用SqlDataReader 好象只能用RecordsAffected。
    以前用的很少。不好意思。
      

  3.   

    使用DataSet
    string select="select * from test";
    SqlConnection conn=new SqlConnection(source);
    conn.Open();
    SqlCommand cmd=new SqlCommand(select,conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet()
    da.Fill(ds);
    ListBox1.DataSource = ds.Tables[0].DefaultView;
    ListBox1.DataTextField = "字段名";
    ListBox1.DataValueField = "字段名";
    ListBox1.DataBind();记录数量 ds.Tables[0].Rows.Count 如果你全部记录都加入了ListBox,可以通过listbox的Rows就是记录数了