Visual Basic 2010 Coding Briefs Data Access. Kevin Ph.D. Hough
Чтение книги онлайн.
Читать онлайн книгу Visual Basic 2010 Coding Briefs Data Access - Kevin Ph.D. Hough страница 3
A SqlConnection object represents a unique session to a SQL Server data source, and it is used together with a SqlDataAdapter and a SqlCommand to increase performance when connecting to a Microsoft SQL Server database.
•First, we set the variable SQLConn to a SQLConnection, and use the connection string that is passed to the procedure
•Next, we open the connection with the Open command
Closing a Database Connection
When a SqlConnection goes out of scope, it won't be closed, so we have to explicitly close the connection by calling Close or Dispose. Close and Dispose will both close a connection to an open database;however, Close is the preferred method of closing any open connection.
The code shown below in Listing 2 can be used to close a database connection.
Listing 2: Closing a Database Connection
Public Sub DBDisconnect(ByRef errorParms As Object)
Try
SqlConn.Close()
SqlConn = Nothing
SqlCmd = Nothing
Catch
' Return the error to the calling program.
errorParms(0) = 999
errorParms(1) = Err.Description
errorParms(2) =
System.Reflection.MethodBase.
GetCurrentMethod.Name
End Try
End Sub
The code in Listing 2 closes the database connection and sets the connection and the command objects to nothing, therefore, releasing the resources. Managing database connections is an important part of a good data access framework.
In the next section, we will develop the code that we will use to select records in Coding Briefs.
Конец ознакомительного фрагмента.
Текст предоставлен ООО «ЛитРес».
Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.
Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.