写了 string SQL = "select count(*) from Lesson";
SqlConnection Conn;
Conn = new SqlConnection("...");
SqlCommand Comm = new SqlCommand(SQL,Conn);
SqlDataReader dr = Comm.ExecuteReader();
如何把表列数显示出来啊?
SqlConnection Conn;
Conn = new SqlConnection("...");
SqlCommand Comm = new SqlCommand(SQL,Conn);
SqlDataReader dr = Comm.ExecuteReader();
如何把表列数显示出来啊?
也就是说,他在读这当前这一条记录的时候,是不知道还有没有下一条记录的,等读到没有记录的时候,dr.Read()返回false,这时候你才能知道记录的列数是多少
{
//行数
int count=Convert.ToInt32(sdr[0]);
}当然也可以用Scalar:
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (sdr.Read())
{
}
而不是用循环读取
while(sdr.Read())
{
}再由于是第一列,所以就直接sdr[0]就OK了...
还有个问题,如果把字段名在 DropDownList 显示出来,该如何实现啊?
{
bt.Columns[i].ColumnName; //就是字段名了,直接一项一项添加到DropDownList中
//就ok了
}
this.DropDownList1.Items.Add("字段名2");....这样?
SqlDataAdapter sda=new SqlDataAdapter("select * from studentDetails",con);
DataSet ds=new DataSet();
sda.Fill(ds,"student");
this.Dropdownlist1.DataSource=ds.Tables["student"];
//显示姓名
this.Dropdownlist1.DisplayMember="sname";
DropDownList1.DataBind();
不写这句,什么都白搭
{
if (!this.IsPostBack)
{
BindData();
}
}
public void BindData()
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda = new SqlDataAdapter("select * from studentInfor", con);
DataSet ds = new DataSet();
sda.Fill(ds, "student");
this.DropDownList1.DataSource = ds.Tables["student"];
this.DropDownList1.DataTextField = "sname";
this.DropDownList1.DataBind();
}
http://blog.csdn.net/zhzuo/archive/2004/07/03/33273.aspx