First and foremost, in your code behind file within an ASP.NET application (or simply the .vb or .cs file of your .NET desktop application), you will need to first import the namespace that has the relevant database-related classes and methods, etc.
Note: All examples use the Visual Basic language, but the concept is the same for both Visual Basic and C#, for example.
Code:
Imports System.Data.SqlClient
Web.config:
Code:
<configuration> <connectionStrings> <add name="ApplicationServices" connectionString="Server=example.com;Initial Catalog=the_database;UID=the_user_of_db;Password=pw_here" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
Code:
Dim ConnectionStringReference = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
Now, in whatever method you wish to execute queries, etc., you first connect to the database and then execute the query:
Code:
Try Dim FetchSubRows As New SqlConnection(ConnectionStringReference) 'the class-level variable ConnectionString FetchSubRows.Open() 'opens the connection Dim FetchQuery As String = "SELECT Submission_ID, Submission_Title, Submission_Body, Submission_IP, Submission_Date FROM Submissions ORDER BY Submission_ID DESC" 'the SQL string Dim ExecuteCommand As New SqlCommand(FetchQuery, FetchSubRows) 'execute the query Dim DataReader As SqlDataReader = ExecuteCommand.ExecuteReader() 'we want to "read" the information, since it is a SELECT statement above While DataReader.Read() 'the Read() method advances to the next applicable record of fetched data...so it will continue looping until all has been returned, essentially Return DataReader End While If DataReader.HasRows = False Then 'has it got any actual rows? FetchSubmissionsError = 0 End If Catch ex As SqlException 'if there was an error, perhaps with the SQL statement itself, catch this error and put it in a variable to be displayed elsewhere FetchSubmissionsError = -1 FetchSubmissionsErrorMessage = ex.Message End Try
Regarding the connection string
Yes, you will need a connection string obviously. If you want to include this directly in your code, here is an example:
Code:
Try Dim FetchSubRows As New SqlConnection("Server=example.com;Initial Catalog=the_database;UID=the_user_of_db;Password=pw_here") ' << visible change is this FetchSubRows.Open() 'opens the connection Dim FetchQuery As String = "SELECT Submission_ID, Submission_Title, Submission_Body, Submission_IP, Submission_Date FROM Submissions ORDER BY Submission_ID DESC" 'the SQL string Dim ExecuteCommand As New SqlCommand(FetchQuery, FetchSubRows) 'execute the query Dim DataReader As SqlDataReader = ExecuteCommand.ExecuteReader() 'we want to "read" the information, since it is a SELECT statement above While DataReader.Read() 'the Read() method advances to the next applicable record of fetched data...so it will continue looping until all has been returned, essentially Return DataReader End While If DataReader.HasRows = False Then 'has it got any actual rows? FetchSubmissionsError = 0 End If Catch ex As SqlException 'if there was an error, perhaps with the SQL statement itself, catch this error and put it in a variable to be displayed elsewhere FetchSubmissionsError = -1 FetchSubmissionsErrorMessage = ex.Message End Try
Useful websites and resources:
ConnectionString Keyword Reference