Evridb examples: Difference between revisions
imported>Scott No edit summary |
imported>Scott No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 52: | Line 52: | ||
ans = | ans = | ||
1</pre> | 1</pre> | ||
'''NOTE:''' If you're using MS SQL Server Express as a DSN you need to designate the "server name" correctly. Get the connection properties by right clicking on table in Management Studio, right-click on table and select Properties and "View connection properties" in the lower left quadrant window. For example, server name might be "MYCOMPUTER-PC\SQLEXPRESS" where the server is name\SQLEXPRESS. Note this strategy for direct connection (below) as well. | |||
===Connecting to MS SQL Server=== | |||
This example used a .mdf file "attached" to a local instance of SQL Server Express. The local database server was created using these [http://msdn.microsoft.com/en-us/library/hh510202.aspx instructions]. Originally a DSN was created where the server syntax "(localdb)\v11.0" was located via Server Setup dialog box. | |||
<pre> | |||
>> dbo = evridb('mssql'); | |||
>> dbo.driver = '{SQL Server Native Client 11.0}'; | |||
>> dbo.provider = ''; | |||
>> dbo.server = '(localdb)\v11.0'; | |||
>> dbo.dbname = 'MyTestDatabase'; | |||
>> dbo.testconnection | |||
1 | |||
</pre> |
Latest revision as of 10:46, 26 September 2015
Examples use EVRI Database Object
NOTE: For PLS_Toolbox 6.1 and newer.
Connecting to Office 2007 Access Database
- If you don't have Office 2007 or newer installed you'll need to download and install the newest Access database driver from here:
- Make a database object with the appropriate 'location' and 'name':
>> mydb = evridb('type','access'); >> mydb.location = 'C:\Users\scott\Desktop'; >> mydb.dbname = 'test.accdb';
- Test the connection, if you don't get a 1 back check the location and dbname:
>> mydb.testconnection ans = 1
- Get a list of the tables in the database:
>> mytables = mydb.get_access_tables mytables = '~TMPCLP877991' 'table1' 'table2' 'table3'
- Get a cell array of all the table data in "table1" with the first row containing column names:
>> mydb.use_column_names = 'yes'; >> mydb.return_type = 'cell'; table1Data = mydb.runquery('select * from "table1"') table1Data = 'id' 'field01' [ 1] 'value1' [ 2] 'value2' [ 3] 'value3' [ 4] 'value4'
Connecting to System Data Source Name (DSN)
Make sure you have drivers installed and DSN created. Search Microsoft for DSN instruction for you particular system if you do not have DSN set up.
>> mydb = evridb('type','dsn'); >> mydb.dsn = 'name_of_DSN; >> mydb.testconnection ans = 1
NOTE: If you're using MS SQL Server Express as a DSN you need to designate the "server name" correctly. Get the connection properties by right clicking on table in Management Studio, right-click on table and select Properties and "View connection properties" in the lower left quadrant window. For example, server name might be "MYCOMPUTER-PC\SQLEXPRESS" where the server is name\SQLEXPRESS. Note this strategy for direct connection (below) as well.
Connecting to MS SQL Server
This example used a .mdf file "attached" to a local instance of SQL Server Express. The local database server was created using these instructions. Originally a DSN was created where the server syntax "(localdb)\v11.0" was located via Server Setup dialog box.
>> dbo = evridb('mssql'); >> dbo.driver = '{SQL Server Native Client 11.0}'; >> dbo.provider = ''; >> dbo.server = '(localdb)\v11.0'; >> dbo.dbname = 'MyTestDatabase'; >> dbo.testconnection 1