close

Close database connection, DatabaseDatastore, cursor, or resultset object

Syntax

Description

example

close(object) closes the database and driver resource utilizer object to free up database and driver resources.

Examples

expand all

Close the Database Connection Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Close the database connection conn.

close(conn)

Close the DatabaseDatastore Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Create a DatabaseDatastore object dbds using the database connection conn and SQL query sqlquery. This SQL query retrieves all products from the product table productTable ordered by product number.

sqlquery = 'select * from productTable order by productNumber';

dbds = datastore(conn,sqlquery);

Close the DatabaseDatastore object dbds.

close(dbds)

Close the Cursor Object

Using the native ODBC interface, connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database.ODBCConnection('dbtoolboxdemo','admin','admin');

Select data from productTable that you access using the database.ODBCConnection object conn. Assign the returned cursor object to the variable curs.

sqlquery = 'select * from productTable';
curs = exec(conn,sqlquery);

Close the cursor object curs before closing the database connection.

close(curs)

Close the database connection conn.

close(conn)

Close the Resultset Object

Connect to the database with the ODBC data source name dbtoolboxdemo with the user name admin and password admin.

conn = database('dbtoolboxdemo','admin','admin');

Select data from productTable that you access using the database connection object conn. Assign the returned cursor object to the variable curs.

sqlquery = 'select * from productTable';
curs = exec(conn,sqlquery);

Construct a resultset object rset.

rset = resultset(curs);

Close the resultset object rset.

close(rset)

Close the cursor object curs before closing the database connection.

close(curs)

Close the database connection conn.

close(conn)

Input Arguments

expand all

object — Database and driver resource utilizerdatabase connection object | DatabaseDatastore object | cursor object | resultset object

Database and driver resource utilizer, specified as a database connection object created using database, a DatabaseDatastore object created using datastore, a cursor object created using exec, or a resultset object created using resultset. This table describes the allowable objects for close.

ObjectDescription

conn

Database connection object or native ODBC database connection object

dbds

DatabaseDatastore object

curs

Cursor object or native ODBC cursor object

rset

Resultset object

Database connections, DatabaseDatastore objects, cursors, and resultset objects remain open until you close them using the close function. Always close a cursor, DatabaseDatastore, connection, or resultset object when you finish using it. Close a cursor before closing the connection used for that cursor. Executing close with a DatabaseDatastore object releases the MATLAB® resources associated with database connection and cursor objects.

    Note:   The MATLAB session closes open cursors, DatabaseDatastore objects, and connections when exiting, however, the database might not free up the cursors and connections.

Was this topic helpful?