string SQL="Select top 10 * from news order by newsdate";
SqlDataAdapter da=new SqlDataAdapter(SQL,conn);
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
DataTable Table=ds.Tables[0];不用command对象,直接用SqlDataAdapter

解决方案 »

  1.   

    请教一下,上面的da.SelectCommand=comm;这句中SelectCommand是如何来的(这代码是我从网上看到的),不明白,还请指点一二,谢谢。
      

  2.   

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(sql,conn);
    da.fill(ds);
      

  3.   

    经过测试,还是不行,提示错误如下:
    Server Error in '/flyshop' Application.
    --------------------------------------------------------------------------------对象名 'news' 无效。 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: 对象名 'news' 无效。Source Error: 
    Line 10:  SqlDataAdapter da=new SqlDataAdapter(SQL,conn);
    Line 11:  DataSet ds=new DataSet();
    Line 12:  da.Fill(ds);
    Line 13:  conn.Close();
    Line 14:  DataTable Table=ds.Tables[0];
     Source File: C:\flyshop\aboutus.aspx    Line: 12 Stack Trace: 
    [SqlException: 对象名 'news' 无效。]
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) +45
       System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +5
       System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
       System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
       System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +38
       ASP.aboutus_aspx.__Render__control1(HtmlTextWriter __output, Control parameterContainer) in C:\flyshop\aboutus.aspx:12
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +27
       System.Web.UI.Control.Render(HtmlTextWriter writer) +7
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +243
       System.Web.UI.Page.ProcessRequestMain() +1929
    还是提示Line 12:  da.Fill(ds);这句错误,我在别的代码中看到是这样的:da.Fill(ds,"注册用户表"),这样SqlDataAdapter对象执行了SQL语句而生成注册用户表,是不是格式错误,但不明白这里的注册用户表到底是哪个表? 
      

  4.   

    我在用da.fill(ds);
    出现:找不到可安装的 ISAM???
      

  5.   


    dattotzy(酋长) 
    请问出现上面的提示是不是系统的问题?
      

  6.   

    da.Fill(ds)与da.Fill(ds,"sdfs")
    是FILL方法的两种不同的形式,第一个系统将自动的SELECTCOMMAND执行的结果作为一个表添加到DATASET中,也就是TABLES[0],第二种形式是把查询结果添加到DATASET指定的表中,表名为"SDFS"你的这个问题是堆栈问题,看上去是DA.FILL(DS)有错,其实是这方法里的一个操作方法的错误,看到提示,是这句话错误System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
    这说明表还没有取出来是还在执行COMMAND.EXECUTEREADER的时候就出错呢,
    对策:请在数据库的查询分析器里先运行这个SQL语句,我估计是在你的数据库里根本就没有NEWS这个表,
      

  7.   

    Exception Details: System.Data.SqlClient.SqlException: 对象名 'news' 无效。是你的SQL语句有问题,放到查询分析器中试试看
      

  8.   

    如果我在数据库中选择的不是含有NEWS表的库就会出现服务器: 消息 208,级别 16,状态 1,行 1
    对象名 'news' 无效。