Basic SQL Connect
'Imports System.Data.SqlClient
Try
'Database Connection and Query
Dim sqlConn As SqlConnection = Nothing
Dim sqlCmd As SqlCommand = Nothing
Dim strConnection As String = Nothing
'Get connection string from Appsettings Web.Config
strConnection = "Connection String Goes Here"
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("Select * from BogusTable with (nolock)", sqlConn)
sqlConn.Open()
DataGrid1.DataSource = sqlCmd.ExecuteReader() 'Using .NET DataGrid
DataGrid1.DataBind()
sqlConn.Close()
sqlConn = Nothing
Catch ex As Exception
Response.Write = "ERROR: " & ex.ToString
End Try
There are plenty of other ways to do this.... but this is the way that works for me.
Execute Store Procedure Without Parameters (VB.NET)
Try
'Database Connection and Query
Dim SQLStr As String = "Stored Procedure Name Goes Here"
Dim ConStr As String = "Connection String Goes Here"
Dim sqlConn As New System.Data.SqlClient.SqlConnection(ConStr)
Dim ds As New DataSet
Dim SQLAdapter As New System.Data.SqlClient.SqlDataAdapter(SQLStr, ConStr)
SQLAdapter.Fill(ds)
sqlConn.Close()
sqlConn = Nothing
'Do something with the data set or call function here
Catch ex As Exception
Response.Write(Now + " " + "ERROR: " & Err.Description & " " & Err.Source & " " & Err.Number & " " & Err.HelpContext & " " & Err.HelpFile)
End Try