我想从我的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
上面的代码不起作用,我知道我需要执行查询但是我完全迷失了。
答案 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()