ASP.NET SQL查找查找MAX值

时间:2013-01-16 19:54:11

标签: asp.net sql visual-studio-2010

我想从我的ASP.NET页面(vb)触发SQL查询,查询将做的是从列中查找最大值,然后返回该值并将其放入网页的标签中

目前我不知道触发SQL命令然后返回值,我的代码更正非常感激。

Dim Con As New SqlConnection
        Dim SQL As String
        Con.ConnectionString = "Data Source=WCRDUSMJEMPR9\SQLEXPRESS;Initial Catalog=MicroDB;Integrated Security=True"
        Con.Open()
        SQL = "SELECT MAX(ID_ControlCharts) FROM ControlCharts"
        Label123.Text = SQL

上面的代码不起作用,我知道我需要执行查询但是我完全迷失了。

2 个答案:

答案 0 :(得分:0)

Dim com as SqlCommand = Con.CreateCommand
Label123.Text = com.ExecuteScalar(SQL)

答案 1 :(得分:0)

您需要创建sql命令并调用executescalar方法。

例如:

Dim Con As New SqlConnection
Dim SQL As String
Con.ConnectionString = "Data Source=WCRDUSMJEMPR9\SQLEXPRESS;Initial
    Catalog=MicroDB;Integrated Security=True"
Con.Open()
Dim cmd as new SQLCommand(sql,Con)
Dim obj = cmd.ExecuteScalar()
if(obj!=null)
 Label123.Text = obj.ToString()
end if

Con.Close()