Connect to a Microsoft® SQL Server® database and 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 =
[]
Select all data from productTable and sort it by the product number. data is a table containing the imported data that results from executing the SQL SELECT statement.
selectquery = 'SELECT * FROM productTable ORDER BY productNumber';
data = select(conn,selectquery);
Create a database connection using a JDBC driver. To create this connection, you must configure a JDBC data source. For more information, see the configureJDBCDataSource function. Then, create a DatabaseDatastore object and close it.
Create a database connection to the JDBC data source MSSQLServerJDBCAuth. This data source configures a JDBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.
connection objects and DatabaseDatastore objects remain open
until you close them using the close function.
Always close these objects when you finish using them.
Executing close with a DatabaseDatastore object releases the
MATLAB resources associated with the connection object.
Note
When you close the MATLAB session, MATLAB closes open DatabaseDatastore objects and
connections. However, the database might not free up the connections.
Consult your database administrator about the remaining
connections.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.