When working in Microsoft Visual Studio, if you create a connection using the Server Explorer, a link to the database would display as a sub-node of the Data Connection link. You can re-use one of these connections when necessary.
If you are "physically" connecting to a server or a database using the Microsoft SQL Server Management Studio, the steps we have described so far allow you to open the connection. After programmatically creating a connection string, to apply it and actually establish the connection, you must call the Open() method of the SqlConnection class. Its syntax is: Public Sub Open Here is an example of calling it: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim connection As SqlConnection = _ New SqlConnection("Server=(local);Initial Catalog='exercise1';") connection.Open() End Sub End Class As you can see, this method does not take any argument. The SqlConnection object that calls it is responsible to get the connection string ready:
After using a connection and getting the necessary information from it, you should terminate it. If you are working in Microsoft SQL Server Management Studio, to close the connection, you can simply close the window as an application. If you are working in Microsoft Visual Studio, to close a connection, you can right-click it in the Server Explorer and click Close Connection. If you are working from a SqlConnection object, to close a connection, you can call the Close() method of the SqlConnection class. Its syntax is: Public Sub Close This method is simply called to close the current connection. Here is an example of calling it: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim connection As SqlConnection = _ New SqlConnection("Server=(local);Initial Catalog='exercise1';") connection.Open() ' Blah Blah Blah connection.Close() End Sub End Class While you should avoid calling the Open() method more than once if a connection is already opened, you can call the Close() method more than once. You should always remember to close the connection so that the resources that the database application was using can be made available to other applications. Fortunately, the Visual Basic language provides an alternative that can close the connection for it. To assist you with this, you can use the Using operator as follows: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Using connection As SqlConnection = _ New SqlConnection("Data Source=(local);Integrated Security=yes") connection.Open() End Using End Sub End Class When this code executes, it opens the connection. Inside of the curly brackets, you can do whatever you want. When the compiler reaches the closing curly bracket, it calls the SqlConnection.Close() method, which means you do not need to remember to close it.
We have learned different ways of connecting to a server. After establishing a connection, if you are successful, the database system becomes available to you and you can take actions, such as creating a database and/or manipulating data. An action you perform on the database server or on a database is called a command. To support the various commands you can perform on a Microsoft SQL Server database, the System.Data.SqlClient namespace provides the SqlCommand class. To use it, you can declare a variable of type SqlCommand using one of its constructors.
The SqlCommand class is equipped with four constructors. The default constructor allows you to initiate a command without specifying what action would be taken. The action to perform is created as a string statement. This action is represented by the CommandText property of the SqlCommand class, which is of type String. If you want to use the default constructor, you can then create a string that would carry the action to perform. Once the string is ready, you can assign it the CommandText property. This would be done as follows: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim CommandToExecute As SqlCommand = New SqlCommand() Dim strCommandToExecute As String = "Blah Blah Blah" CommandToExecute.CommandText = strCommandToExecute End Sub End Class After creating the action that would be performed, you must specify what connection would carry it. To do this, you can first create a SqlConnection object. To provide it to the command, the SqlCommand class is equipped with a property named Connection that is of type SqlConnection. After creating a SqlConnection object, to provide it to the command, you can assign it to the SqlCommand.Connection property. This would be done as follows: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim strConnection As String = "Server=(local);" & _ "Integrated Security=no;" & _ "User ID=sa;PWD=$outh~@kotA" Dim connection As SqlConnection = New SqlConnection(strConnection) Dim CommandToExecute As SqlCommand = New SqlCommand() Dim strCommandToExecute As String = "Blah Blah Blah" connection.Open() CommandToExecute.Connection = connection CommandToExecute.CommandText = strCommandToExecute connection.Close() End Sub End Class Instead of declaring a SqlCommand variable and the command text separately, as an alternative, you can define the command text when declaring the SqlCommand variable. To do this, you can use the second constructor of the SqlCommand class. The syntax of this constructor is: Public Sub New(cmdText As String) Once again, after using this constructor, you must specify what connection would carry the action. To do this, you can assign a SqlConnection object to the Connection property of your SqlCommand. Here is an example: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim strConnection As String = "Server=(local);" & _ "Integrated Security=no;" & _ "User ID=sa;PWD=$outh~@kotA" Dim connection As SqlConnection = New SqlConnection(strConnection) Dim CommandToExecute As SqlCommand = New SqlCommand("Blah Blah Blah") connection.Open() CommandToExecute.Connection = connection connection.Close() End Sub End Class Instead of assigning a SqlConnection object to the SqlCommand.Connection property, you can specify what connection would carry the action at the same time you are creating the command. To specify the connection when declaring the SqlCommand variable, you can use the third constructor of this class. Its syntax is: Public Sub New(cmdText As String, connection As SqlConnection) The second argument to this constructor is an established connection you would have defined. Here is an example: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim connection As SqlConnection = New SqlConnection("Server=(local);" & _ "Integrated Security=True;") Dim CommandToExecute As SqlCommand = New SqlCommand("Blah Blah Blah", connection) connection.Open() CommandToExecute.Connection = connection connection.Close() End Sub End Class If you had initiated the action using the default constructor of the SqlCommand class, you can assign a SqlConnection object to the Connection property of the SqlCommand class.
After establishing a connection and specifying what command needs to be carried, you can execute it. To support this, the SqlCommand class is equipped with the ExecuteNonQuery() method. Its syntax is: Public Overrides Function ExecuteNonQuery As Integer This method does not take any argument. The SqlCommand object that calls it must have prepared a valid command.
In some cases, some actions take longer than others to execute. For this type of command, the compiler would keep trying to execute a command until successful. If there is a problem, this operation can take long or too long. You can specify how long the compiler should wait to try executing the command, again. The SqlCommand.CommandTimeOut property allows you to specify the time to wait before trying to execute a command. The default value of this property is 30 (seconds). If you want a different value, assign it to your SqlCommand variable.
To allow you to specify the type of command you want to perform, the SqlCommand class is equipped with the CommandType property, which is based on the CommandType enumeration. The CommandType enumeration has three members: StoredProcedure, TableDirect, and Text. For a SqlCommand object, the default value is Text.
A data command is used to initiate an action to perform on a database. To read data of a database, one of the objects you can use is called a data reader. With a data reader, the compiler reads the first value, then moves to the second value, then moves to the third value, and so on. One of the particularities of a data reader is that, once it visits a value, reads it, and moves to the next value, the compiler cannot refer to the previous value. This can be illustrated as follows:
To support data readers, the .NET Framework provides, for a Microsoft SQL Server database, a class named SqlDataReader. To get a data reader, you can declare a variable of type SqlDataReader. This class does not have a constructor. This means that, to use it, you must (directly) specify where it would read its data. To provide data to the reader, the SqlCommand class is equipped with the ExecuteReader() method that is overloaded with two versions. The simplest version of this method uses the following syntax: Public Function ExecuteReader As SqlDataReader Before using a data reader, you should first create a command that would specify how data would be acquired. Once the data is read, you can pass it to the data reader by assigning the result of a call to a SqlCommand.ExecuteReader() method to a SqlDataReader object.
Once data is supplied to the reader, you can access it, one value at a time, from top to bottom. To access data that the reader acquired, you can call its Read() method whose syntax is: Public Overrides Function Read As Boolean As mentioned already, the Read() method simply reads a value and moves on. When reading the values, the data reader reads one value at a time and moves to the next. |
|
|||||||||||||||||||||||||||||
|