Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从SQL C中key=some_值的表中选择MAX(Id)MAX?_C#_Mysql_Sql_Sql Server_Visual Studio - Fatal编程技术网

C# 如何从SQL C中key=some_值的表中选择MAX(Id)MAX?

C# 如何从SQL C中key=some_值的表中选择MAX(Id)MAX?,c#,mysql,sql,sql-server,visual-studio,C#,Mysql,Sql,Sql Server,Visual Studio,我需要从一个表中选择并获取一个唯一的值,该表中的键有重复的条目。例如,有一个事务IDId和一个AccountNumberAccountNumber,每个帐号有许多条目,但我必须只获取特定AccountNumber的最大事务IDId。我正在使用下面的代码,但结果中有多行。我做得对吗?此代码中没有语法错误。如果我删除GROUPBY,它会给出一个错误,说明没有GROUPBY语句 SqlConnection con = new SqlConnection(ConfigurationMan

我需要从一个表中选择并获取一个唯一的值,该表中的键有重复的条目。例如,有一个事务IDId和一个AccountNumberAccountNumber,每个帐号有许多条目,但我必须只获取特定AccountNumber的最大事务IDId。我正在使用下面的代码,但结果中有多行。我做得对吗?此代码中没有语法错误。如果我删除GROUPBY,它会给出一个错误,说明没有GROUPBY语句

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
        con.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter("select MAX(Id), Address, AccountNumber, Date, CustomerName, Debit, Credit, Balance from fianlTable WHERE (AccountNumber='"+textBox4.Text+"') ) GROUP BY Id, Address, AccountNumber, Date, CustomerName, Debit, Credit, Balance", con);
        sda.Fill(dt);
        dataGridView1.DataSource = dt;

不特定于C,但更基于SQL,您可以选择帐号的记录-按TransactionId Desc排序,并将结果限制为1

这看起来像:

SELECT Id, Address, AccountNumber, Date, CustomerName, Debit, Credit, Balance from fianlTable WHERE (AccountNumber='"+textBox4.Text+"') ORDER BY Id DESC LIMIT 1

顺便说一句,Lucas是正确的,您直接在查询中使用文本框中的值,可能会有人劫持您的代码:

如果您只想知道特定帐号的特定ID的最大值,那么您不需要其他字段,也不需要GROUP by

using(SqlConnection con = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(@"select MAX(Id)
                                        from fianlTable 
                                        WHERE AccountNumber=@num"), con)
{
    int maxValue = 0;
    con.Open();
    cmd.Parameters.Add("@num" SqlDbType.NVarChar).Value = textBox4.Text;
    object result = cmd.ExecuteScalar();
    if(result != null) 
      maxValue = Convert.ToInt32(result);
}
因此,不需要SqlDataAdapter所需的所有基础设施,只需要一个SqlCommand、它的文本和对ExecuteScalar的调用。还要注意,连接和命令等一次性对象由using语句包围,以确保正确关闭和处理


这里还有别的话要说。我不知道这段代码背后的原因,但我希望这不是为了在INSERT命令中获得要分配的下一个ID。在这种情况下使用MAX是错误的,因为返回的值在多用户环境中是不可信的

如果您想要maxid,那么thsi colunmn不应该在group by中

"select 
     Id
    , Address
    , AccountNumber
    , Date
    , CustomerName
    , Debit
    , Credit
    , Balance 
from fianlTable 
WHERE id = (select max(id) from fianlTable 
           WHERE  AccountNumber='"+textBox4.Text+"' 
GROUP BY AccountNumber);"

该代码中存在SQL注入漏洞。你应该立即停止你现在正在做的任何事情,如果你有兴趣知道某个特定帐户的最大值,那么你不需要其他字段,也不需要这个组BY@Lucas我知道SQL注入,但我正在构建一个测试应用程序,这只是我项目的开始,我稍后会解决它,不需要太多麻烦。请关注我的问题。你不需要使用con.Open-dataadapter会解决这个问题。如果确实打开了连接,则应在之后将其关闭。在任何一种情况下,您都应该使用con.Dispose来保持一切整洁。@Andrew很好的建议我不知道con.Dispose,谢谢。仍然没有任何进展会给出相同的多个结果。您的回答似乎很有帮助,但它说限制附近的语法不正确。我正在使用visual studio 2013。限制适用于MySQL和其他DBs。有关T-SQL等效项,请参见。Steve的答案更好:它更健壮,可以处理更多的数据库,使用更少的资源。而且,如果使用TOP,这个答案要求在返回TOP 1之前对结果集进行排序。有时很难理解OP是如何选择一个有用的答案的。您的答案是正确的,但它给出了一个错误ExecuteScalar尚未初始化。请在这方面指导我。SqlCommand cmd在usingSqlCommand cmd=new SqlCommand….中初始化。。。。可能是指向SqlCommand构造函数中缺少的连接。答案现在已确定。