窗体控件:1、Label 2、Button
数据库:testmdb.mdb 表:user 字段:id(主键) 、name
要求:单击Button,把数据库第一条记录中的name显示在Label上。 

解决方案 »

  1.   

    Button单击事件方法:
    OleDbConnection cn=new OleDbConnection("连接字符串");
    OleDbCommand cmd=new OleDbCommand("select top 1 [name] from [user] ordey by [id]",cn);
    cn.Open();
    Label1.Text=cmd.ExecuteScalar().ToString();
    cn.Close();累死
      

  2.   

    OleDbConnection cn=new OleDbConnection("连接字符串");//根据连接字符串创建一个数据连接
    OleDbCommand cmd=new OleDbCommand("select top 1 [name] from [user] ordey by [id]",cn);//根据sql语句和数据连接创建一个命令对象
    cn.Open();//打开数据连接
    Label1.Text=cmd.ExecuteScalar().ToString();//执行命令,返回第一个name,并给Label
    cn.Close();//关闭数据连接结帖吧~~
      

  3.   

    OleDbCommand cmd=new OleDbCommand("select top 1 [name] from [user] ordey by [id]",cn);//根据sql语句和数据连接创建一个命令对象
    关键是这括号里面不懂   特别是  ordey by [id]  是什么意思
      

  4.   

    去掉  ordey by [id]  才能正常运行
    不去掉的话会出现异常
    这是什么原因啊??
      

  5.   

    打字时敲错了,应为order by [id]括号里的是sql语句啊。