照书写例子private void Button1_Click(object sender, System.EventArgs e)
{
string SQstr = "";
switch(DropDownList1.SelectedIndex.ToString())
{
case "完美": SQstr = " 成绩 = 100"; break;
case "较好": SQstr = " 成绩>=80 and 成绩<=90"; break;
case "一般": SQstr = " 成绩>=60 and 成绩<=80"; break;
default: Console.WriteLine("123"); break;

}
myconn.Open();

string SQst = "select * from TABLE3 where" + SQstr;
SqlDataAdapter mycommand = new SqlDataAdapter(SQst, myconn);
DataSet ds = new DataSet();
                          mycommand.Fill(ds, "scores");
        DataGrid1.DataSource = ds.Tables["scores"].DefaultView;
DataGrid1.DataBind();

}问题在这两行中 mycommand.Fill(ds, "scores");               
 DataGrid1.DataSource = ds.Tables["scores"].DefaultView;   .Fill方法中第2个参数"scores"和["scores"]是什么?? 代表什么?麻烦说的详细些,谢谢了

解决方案 »

  1.   

    "scores"是ds中一个表的名字,这个名字就是你通过mycommand.Fill(ds,"scores")给的名字
    调用时就用ds.Tables["scores"]调用,ds里可以放n个这样的Table
    你的报错信息是什么?
      

  2.   

    这个全部代码using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace Web
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;
    protected SqlConnection myconn = new SqlConnection();


    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    string ss = "server=localhost;uid=sa;pwd=40109547;database=实验1";
    myconn.ConnectionString = ss;
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    string SQstr = "";
    switch(DropDownList1.SelectedIndex.ToString())
    {
    case "完美": SQstr = " 成绩 = 100"; break;
    case "较好": SQstr = " 成绩>=80 and 成绩<=90"; break;
    case "一般": SQstr = " 成绩>=60 and 成绩<=80"; break;
    default: Console.WriteLine("123"); break;

    }
    myconn.Open();

    string SQst = "select * from TABLE3 where" + SQstr;
    SqlDataAdapter mycommand = new SqlDataAdapter(SQst, myconn);
    DataSet ds = new DataSet();
                mycommand.Fill(ds, "scores");
    DataGrid1.DataSource = ds.Tables["scores"].DefaultView;
    DataGrid1.DataBind();

    }
    }
    }
    错误提示是“/Web/Web”应用程序中的服务器错误。
    --------------------------------------------------------------------------------第 1 行: 'where' 附近有语法错误。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: 'where' 附近有语法错误。源错误: 
    行 71:  SqlDataAdapter mycommand = new SqlDataAdapter(SQst, myconn);
    行 72:  DataSet ds = new DataSet();
    行 73:             mycommand.Fill(ds, "scores");
    行 74:  DataGrid1.DataSource = ds.Tables["scores"].DefaultView;
    行 75:  DataGrid1.DataBind();
     源文件: d:\inetpub\wwwroot\web\web\webform1.aspx.cs    行: 73 堆栈跟踪: 
    [SqlException: 第 1 行: 'where' 附近有语法错误。]
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
       System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
       Web.WebForm1.Button1_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\web\web\webform1.aspx.cs:73
       System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       System.Web.UI.Page.ProcessRequestMain() 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573 
      

  3.   

    问题在这两行中 mycommand.Fill(ds, "scores");               
     DataGrid1.DataSource = ds.Tables["scores"].DefaultView;   .Fill方法中第2个参数"scores"和["scores"]是什么?? 代表什么?回答:
    1、.Fill方法中第2个参数"scores"是一个表名,即你执行sql语句返回的数据会填充到ds中的表"scores"中;
    2、["scores"]也是表名,即ds中的"scores"表。
    在你的程序中,这两个是同一个ds中的同一张表。
      

  4.   

    SQL语句错误,把生成的SQL语句到查询分析器里检查一下
      

  5.   

    这个错误是你的
    case "完美": SQstr = " 成绩 = 100"; break;
    case "较好": SQstr = " 成绩>=80 and 成绩<=90"; break;
    case "一般": SQstr = " 成绩>=60 and 成绩<=80"; 
    中有问题,“成绩”是你数据库中表TABLE3的字段吗?检查一下。然后调试根踪一下,调到SqlDataAdapter mycommand = new SqlDataAdapter(SQst, myconn);
    时,把SQst中的sql拿到sql查询分析器中执行一下,看什么地方有错误?
      

  6.   

    在71行做个断点,在调试窗口显示出SQst字符串的内容检查一下
      

  7.   

    switch(DropDownList1.SelectedIndex.ToString())
    {
    case "完美": SQstr = " and 成绩 = 100"; break;
    case "较好": SQstr = " and (成绩>=80 and 成绩<=90)"; break;
    case "一般": SQstr = " and (成绩>=60 and 成绩<=80)"; break;
    default: Console.WriteLine("123"); break;

    }
    myconn.Open();

    string SQst = "select * from TABLE3 where 1>0 " + SQstr;
      

  8.   

    default情况下你的sql语句多了where
      

  9.   

    string SQst = "select * from TABLE3 where 这里要带一个空格" + SQstr;
      

  10.   

    string SQst = "select * from TABLE3 where" + SQstr;
    where后面不加条件也可以的吗?