Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

isopen

Determine if database connection or database cursor is open

Syntax

i = isopen(conn)
i = isopen(curs)

Description

example

i = isopen(conn) returns 1 if the database connection is open and 0 if the database connection is closed or invalid.

example

i = isopen(curs) returns the same values using the database cursor object.

Examples

collapse all

First, connect to the Microsoft® SQL Server® database. Verify the database connection. Then, import data from the database into MATLAB®. Determine the highest unit cost among the retrieved products in the table. Close the database connection.

Create an ODBC database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The database contains the table productTable.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Check the database connection. If the Message property is empty, the connection is successful.

conn.Message
ans =

     []

Determine if the database connection is open. The isopen function returns the numeric scalar 1 that denotes the database connection is open.

i = isopen(conn)
i =

     1

Select all data from productTable and sort it by the product number. data is a table that contains the imported data from executing the SQL SELECT statement.

selectquery = 'SELECT * FROM productTable ORDER BY productNumber';
data = select(conn,selectquery);

Display first three rows of data.

data(1:3,:)
ans =

  3×5 table

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

    1                4.0035e+05     1001              14          'Building Blocks' 
    2                4.0031e+05     1002               9          'Painting Set'    
    3                  4.01e+05     1009              17          'Slinky'          

Determine the highest unit cost in the table.

max(data.unitCost)
ans =

    24

Close the database connection.

close(conn)

Determine if the database connection is closed. The isopen function returns the numeric scalar 0 that denotes the database connection is closed. If the database connection is invalid, the isopen function returns the same result.

i = isopen(conn)
i =

     0

First, connect to the Microsoft® SQL Server® database. Verify the database cursor. Import data from the database into MATLAB®. Determine the highest unit cost among the retrieved products in the table. Close the database connection.

Create an ODBC database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The database contains the table productTable.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Check the database connection. If the Message property is empty, the connection is successful.

conn.Message
ans =

     []

Create a cursor object by executing an SQL query in the database. Select all data from the table productTable using the database connection.

sqlquery = 'SELECT * FROM productTable ORDER BY productNumber';
curs = exec(conn,sqlquery);

Determine if the database cursor is open. The isopen function returns the numeric scalar 1 that denotes the database cursor is open.

i = isopen(curs)
i =

     1

Import data from the executed SQL query. Display the first three rows of imported data.

curs = fetch(curs);
curs.Data(1:3,:)
ans =

  3×5 table

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

    1                4.0035e+05     1001              14          'Building Blocks' 
    2                4.0031e+05     1002               9          'Painting Set'    
    3                  4.01e+05     1009              17          'Slinky'          

Determine the highest unit cost in the table.

data = curs.Data;
max(data.unitCost)
ans =

    24

After you finish working with the cursor object, close it.

close(curs)

Determine if the database cursor is closed. The isopen function returns the numeric scalar 0 that denotes the database cursor is closed. If the cursor object is invalid, the isopen function returns the same result.

i = isopen(curs)
i =

     0

Close the database connection.

close(conn)

Input Arguments

collapse all

Database connection, specified as a connection object created using the database function.

Database cursor, specified as a cursor object created using the exec function.

Introduced in R2015b

Was this topic helpful?