【问题标题】:Displaying the whole row that contains max value for a specific integer type field. Returns error显示包含特定整数类型字段的最大值的整行。返回错误
【发布时间】:2019-06-15 13:24:05
【问题描述】:

我需要一些有关使用 SQL 的帮助,我需要具有 C#/SQL 知识的人,我只需要一行简单的代码,或者如果需要,需要一个只有一个目的的完整代码:仅显示具有最多数量的行Gridview/Label 中的表格 Cars 中的 Clikes,任何人都可以提供这样的代码并教我吗?这个问题与 Web 开发有关。

我已经尝试使用我将在下面提供的一些代码,但我总是收到一个错误,提示条件类型不匹配或类似的东西。

 //this button is inside a masterpage.cs file    
 protected void Button1_Click(object sender, EventArgs e)    
 {
     string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/DatabaseVSC.accdb");
     localhost.wbCarsDb o = new localhost.wbCarsDb();
     DataSet ds = o.GetMostPopularCar(constr);
     string x  =   (ds.Tables[0].Rows[0]["CLikes"].ToString());
     DataSet ds2 = new DataSet();
     ds2 = o.retDetailsCID_datasetR(x,constr);
     this.GridView2.DataSource = ds2;
     this.GridView2.DataBind();
     this.GridView1.DataSource = ds;
     this.GridView1.DataBind();
 }

 //these are codes used in the code above inside the button,  they're stored inside the main CarsDb class that I use to store all crucial codes
 [WebMethod]
 public DataSet GetMostPopularCar(string connectionstr)
 {
     string querystr = string.Format("SELECT MAX(CLikes) AS LargestLike FROM Cars");
     OleDbConnection connectObj = new OleDbConnection(connectionstr);
     OleDbDataAdapter da = new OleDbDataAdapter(querystr, connectObj);
     DataSet ds = new DataSet();
     da.Fill(ds);
     return ds;
 }

 [WebMethod]
 public DataSet retDetailsCID_datasetR(string CLikes, string connectionstr)
 {
     string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/DatabaseVSC.accdb");
     string querystr = string.Format("SELECT [CID] FROM [Cars] WHERE [CLikes]='{0}'", CLikes);
     OleDbConnection connectObj = new OleDbConnection(connectionstr);
     OleDbDataAdapter da = new OleDbDataAdapter(querystr, connectObj);
     DataSet ds = new DataSet();
     da.Fill(ds);
     return ds;
 }    

当我激活按钮时会发生以下情况:

列“CLikes”不属于表Table。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.ArgumentException:列 'CLikes' 没有 属于表Table。

第 26 行:localhost.wbCarsDb o = new localhost.wbCarsDb();
线 27: 数据集 ds = o.GetMostPopularCar(constr);
第 28 行:
字符串 x = (ds.Tables[0].Rows[0]["CLikes"].ToString());
第 29 行:DataSet ds2 = new DataSet(); // 第 28 行是红色的

源文件:c:\Users\alaas\OneDrive\Email attachments\Documents\School\ComputerProgramming\VintageSportsCars2\MasterPage.master.cs 线路:28

堆栈跟踪:

[ArgumentException: Column 'CLikes' does not belong to table Table.]
   System.Data.DataRow.GetDataColumn(String columnName) +5953463
   System.Data.DataRow.get_Item(String columnName) +13
   MasterPage.Button1_Click(Object sender, EventArgs e) in c:\Users\alaas\OneDrive\Email attachments\Documents\School\ComputerProgramming\VintageSportsCars2\MasterPage.master.cs:28
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9782698
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.7.3282.0

【问题讨论】:

  • 您在第一个查询中已将 CLikes 重命名为 LargestLike。您应该在读取数据集时使用它
  • 您的第二个查询,如果 Clikes 是一个数字列,那么您应该将其视为一个数字,而不是转换为字符串并使用字符串在数字列中查找值
  • 也可以使用1个查询得到结果SELECT [CID] FROM [Cars] WHERE [CLikes]=(SELECT MAX(CLikes) FROM Cars)
  • @Luuk 非常感谢!我只是想问一下,有没有办法显示整行而不是只显示该行中的 CID?
  • 排序答案是SELECT * FROM [Cars] WHERE .. 会这样做,但你不应该在程序中使用它!代替*,您应该提供该表中所有字段名称的逗号分隔列表(此处不知道的字段名称,因为您没有在此处提供该信息)。

标签: c# sql ms-access


【解决方案1】:

正如@Steve 在 cmets 中回答的那样 -

您在第一个查询中将CLikes 重命名为LargestLike。您应该在读取数据集时使用它

您的第二个查询,如果CLikes 是一个数字列,那么您应该将其视为一个数字,而不是转换为字符串并使用字符串在数字列中查找值。


我已将其添加为社区 Wiki,以便可以在不将答案隐藏在 cmets 中的情况下解决问题;全部归功于@Steve。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 2023-01-22
    • 1970-01-01
    相关资源
    最近更新 更多