SQL MAX函数 [英] SQL MAX Function
本文介绍了SQL MAX函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我试图从我的Customer表中的custID列中获取最高值,然后将其保存为整数。
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand()
getHighAccountNo.CommandType = CommandType.Text
custID = getHighAccountNo.CommandText = SELECT MAX(custID)AS HighestAccountID FROM Customer
' custID = getHighAccountNo.CommandText =SELECT IDENT_CURRENT('Customer')as LastIdentity
lblHighestAccountNo.Text = custID.ToString()
由于上面的代码总是返回0,我不确定我做错了什么。
感谢任何帮助,
感谢Glen。
解决方案
您定义命令文本,但我看不到您实际执行命令的位置并获取其结果。< br $> b $ b
可能:
getHighAccountNo.CommandText = SELECT MAX(custID)AS HighestAccountID FROM Customer
int cutomerId = getHighAccountNo.ExecuteScalar ();
lblHighestAccountNo.Text = customerId.ToString();
希望这有帮助。
< blockquote>您缺少ExecuteScalar。下面的代码应该这样做!
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand()
getHighAccountNo.CommandType = CommandType.Text
getHighAccountNo.CommandText = SELECT MAX(custID)AS HighestAccountID FROM Customer
custID = getHighAccountNo.ExecuteScalar()
' custID = getHighAccountNo.CommandText =SELECT IDENT_CURRENT('Customer' )作为LastIdentity
lblHighestAccountNo.Text = custID.ToString()
谢谢你们!那很有效!我很傻!本来应该注意到的!
再次感谢!
Hello,
I am trying to get the highest value from my custID column in my Customer table and to then save this out to an integer.
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand()
getHighAccountNo.CommandType = CommandType.Text
custID = getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer"
'custID = getHighAccountNo.CommandText = "SELECT IDENT_CURRENT('Customer') as LastIdentity"
lblHighestAccountNo.Text = custID.ToString()
I'm not sure what I have done wrong as the code above always returns 0.
Any help is appreciated,
Thanks Glen.
解决方案
You define the command text, but I cannot see where you actually execute the command and fetch its result.
Maybe:
getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer" int cutomerId = getHighAccountNo.ExecuteScalar(); lblHighestAccountNo.Text = customerId.ToString();
Hope this helps.
You are missing "ExecuteScalar". The code below should do!
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand() getHighAccountNo.CommandType = CommandType.Text getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer" custID = getHighAccountNo.ExecuteScalar() 'custID = getHighAccountNo.CommandText = "SELECT IDENT_CURRENT('Customer') as LastIdentity" lblHighestAccountNo.Text = custID.ToString()
Thanks guys! That worked! I am silly! Should have noticed that one!
Thanks again!
这篇关于SQL MAX函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文