2013-01-16 75 views
0

我想從我的ASP.NET頁面(vb)中啓動SQL查詢,查詢將執行的操作是從列中查找最大值,然後返回該值並將其放入網頁的標籤中。ASP.NET SQL查詢查找MAX值

目前我不知道如何激發SQL命令,然後返回值,我的代碼糾正是hihgly表示讚賞。

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 

上面的代碼不工作,我知道我需要執行查詢,但是我完全失去了。

回答

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() 
+0

它的工作,謝謝! –

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