什么是从数据表的列中找到最小的代码。我已经为此编写代码但是我得到错误代码如下所示 [英] What Is The Code For Finding Smallest No From The Column Of Datatable .I Have Write The Code For This But I Got Error That Code Is Shown Below

查看:102
本文介绍了什么是从数据表的列中找到最小的代码。我已经为此编写代码但是我得到错误代码如下所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

con = new SqlConnection(cs);
             con.Open();
             cmd = con.CreateCommand();
             cmd = new SqlCommand("select distinct RTRIM(FundLeft) from Invoice ", con);
             rdr = cmd.ExecuteReader();
             while (rdr.Read())
             {
                 DataRow[] dr = dt.Select("FundLeft = MIN(FundLeft)");
                 txtfundleft.Text = dr[0][1].ToString();
             }
             con.Close();

推荐答案

嗯......有很多东西这里错了。

首先,在你的SQL中使用RTRIM意味着FundLeft存储为VARCHAR或NVARCHAR值 - 这很容易做到,但如果你试图将它用作数值,就像你在这里一样。根据需要将其作为数值存储在INT或FLOAT中。它可能看起来更复杂,但从长远来看它会容易得多。



其次,你根本没有使用你从数据库中读取的数据:你设置一个阅读器,然后忽略它的价值,支持看起来像DataTable的东西。



我觉得你刚才在这里猜到了,而你真的没有坐下来想想你需要做什么。这种方法不起作用:你真的,真的需要停下来,坐下来找出你想要做的事情以及你真正需要做的事情。

我不能建议任何具体的改进来摆脱错误,因为据我所知,没有任何东西可以实际做任何你的任务!



以后:如果你收到错误 - 告诉我们这条消息是什么,以及它出现在哪一行?
Um...There are quite a lot of things wrong here.
Firstly, the use of RTRIM in your SQL implies that FundLeft is stored as a VARCHAR or NVARCHAR value - which is simple to do, but causes massive problems if you try to use it as a numeric value, as you are here. Store it as a numeric value in an INT or FLOAT as appropriate. It may seem more complex, but it's a lot easier in the long run.

Secondly, You aren't using the data you read from the DB at all: you set up a reader, and then ignore it's value in favour of what appears to be a DataTable.

I get the feeling that you have just guessed here, and that you really haven't sat down and thought about what you need to do. That approach won't work: you really, really need to stop, sit back and work out what you are trying to do and how you actually need to do it.
I can't suggest any specific improvements to get rid of the error, because as far as I can see, there is nothing there that will actually do whatever your task is!

And in future: if you get an error - tell us what the message is, and exactly which line it appears on?


而不是你的选择不同的查询只需使用



Rather than your "select distinct" query just use

select min(FundLeft) as FundLeft from Invoice





然后使用 cmd.ExecuteScalar [ ^ ]读取结果。


这篇关于什么是从数据表的列中找到最小的代码。我已经为此编写代码但是我得到错误代码如下所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆