Opening a connection using a connection string¶
Opening a connection using a connection string¶
1# This script shows how to use an IDatabaseConnection using a connection string
2
3ui = impact.gui
4ot = ui.output_toolbox
5
6# create a connection to any database using ODBC/ADO/ODBCDIRECT driver
7# set c = impact.databases.create_connection("ODBC", "impact Release 2016")
8c = impact.databases.create_connection("ADO", "Provider=SQLOLEDB.1;Password=quadra;Persist Security Info=True;User ID=sa;Initial Catalog=ImpactRelease2016;Data Source=SABREONE")
9
10# set c = impact.databases.create_connection("ODBCDIRECT", "DRIVER=SQL Server;DATABASE=ImpactRelease2016;SERVER=SABREONE;UID=sa;PWD=quadra")
11
12# to specify a user/password with ODBC you will need to use an ODBCDIRECT connection
13# with a DSN, UID and PWD as below. DSN references the DataSource name in the 32bit ODBC manager
14# set c = impact.databases.create_connection("ODBCDIRECT", "DSN=impact Release 2016;UID=sa;PWD=quadra")
15
16ot.clear()
17
18ot.add("Opening IDatabaseConnection")
19
20# need to connect to the database - this doesn't perform an impact login
21# but just connects so IDatabaseQuery's and IDatabaseCommand's can be used
22if c.connect():
23 ot.add("Connection Name: " + str(c.connection_name))
24 ot.add("Connection Type: " + str(c.connection_type))
25 ot.add("Connection String: " + str(c.connection_string))
26 ot.add("DBMS Type: " + str(c.dbms_type))
27 ot.add("DBMS version: " + str(c.dbms_version.as_string))
28
29 q = c.create_query()
30
31 # assign some SQL
32 q.sql = "select count(U_KEY) from USERS"
33
34 if q.open():
35 ot.add("Successfully opened query: '" + str(q.sql) + "'")
36
37 # read result from IDatabaseQuery
38 if not q.is_eof:
39 ot.add("USERS table has " + str(q.column(1).value) + " row")
40
41 else:
42 ot.add("Unable to determine number of rows")
43
44 # close the query
45 if q.close():
46 ot.add("Successfully closed query")
47
48 else:
49 ot.add("Unable to open query")
50
51 # disconnect from database
52 ot.add("Disconnect from database")
53
54 c.disconnect()
55
56else:
57 ot.add("Unable to connect")
58
59
60