Debian Codegraph @

doc/ProgrammerManual/Tutorial/conne.html

raw symbols SCIP
1 <HTML>
2 <HEAD>
3 <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
4 <TITLE>Connecting </TITLE>
5 <LINK REL="StyleSheet" Href="odbc.css">
6 </HEAD>
7 <BODY bgcolor="white">
8
9 <table width="90%" cols="3" border="0">
10 <TR>
11 <TD colspan="3" class="big">Connecting to a Datasource</TD>
12 </TR>
13 <TR>
14 <TD colspan="3"><P>First thing you will need is a variable of type <CODE>SQLHENV</CODE>. This is
15 a handle (pointer) to an internal ODBC structure which holds all informationen about the
16 ODBC environment. Without a handle of that kind you won't be able do to very much.
17 To get this handle you call <CODE><A HREF="gloss.html#alloc">SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;V_OD_Env)</A></CODE>.
18 <CODE>V_OD_Erg</CODE> is a variable of type <CODE>SQLHENV</CODE> which holds the allocated environment handle.</P>
19 <P>
20 If you have allocated the handle you need to define which version of ODBC to use. Therefore
21 you should call <CODE><A HREF="gloss.html#envattr">SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0)</A></CODE>.
22 The constant <CODE>SQL_ATTR_ODBC_VERSION</CODE> defines that the needed version of ODBC will be defined
23 and <CODE>SQL_OV_ODBC3</CODE> says that the program will need ODBC 3.0.
24 </P>
25 <P>Next thing to do is to create a handle for the database connection which is of the type <CODE>SQLHDBC</CODE>. Once again you call
26 <CODE>SQLAllocHandle</CODE> this time with <CODE>SQL_HANDLE_DBC</CODE> and the variable to the environment returned by the first call to
27 <CODE>SQLAllocHandle</CODE>.
28 </P><P>
29 Then you may choose to modify the connection attributes, mainly the timeout for any given
30 action on the connection. You do this by calling <CODE><A HREF="gloss.html#conattr">SQLSetConnectAttr</A></CODE>
31 with the connection handle, attribute and value pointer and the string length (if available).</P>
32 <P>
33 Finally we are able to connect to the database via <CODE><A href="gloss.html#conn">SQLConnect</A></CODE>, which needs the name of the data source,
34 the username and password as parameters. For each parameter you need to specify how long the string is or just gib <COde>SQL_NTS</CODE> which
35 says that it is a string which length has to be determined by <CODE>SQLConnect</CODE>
36 </P>
37 That's it, we are connected to the database. Please note, that all functions mentioned on this page return either <CODE>SQL_SUCCESS</CODE>, <CODE>SQL_SUCCESS_WITH_INFO</CODE>
38 if all went smoothly or <CODE>SQL_ERROR</CODE> or <CODE>SQL_INVALID_HANDLE</CODE> in case of an error. We will have a look on how to get diagnostic messages a little later.
39 <P>
40 So let's have a look at the code:
41 </TD>
42 </TR>
43 </TABLE>
44 <TABLE>
45 <TR>
46 <TD>
47 <PRE><CODE class="list">
48 <A NAME="list"></A>
49 /* odbc.c
50
51 testing unixODBC
52 */
53 #include &lt;stdlib.h&gt;
54 #include &lt;stdio.h&gt;
55 #include &lt;odbc/sql.h&gt;
56 #include &lt;odbc/sqlext.h&gt;
57 #include &lt;odbc/sqltypes.h&gt;
58
59 SQLHENV V_OD_Env; <FONT COLOR="green">// Handle ODBC environment</FONT>
60 long V_OD_erg; <FONT COLOR="green">// result of functions</FONT>
61 SQLHDBC V_OD_hdbc; <FONT COLOR="green">// Handle connection</FONT>
62
63 char V_OD_stat[10]; <FONT COLOR="green">// Status SQL</FONT>
64 SQLINTEGER V_OD_err,V_OD_rowanz,V_OD_id;
65 SQLSMALLINT V_OD_mlen;
66 char V_OD_msg[200],V_OD_buffer[200];
67
68
69 int main(int argc,char *argv[])
70 {
71 <FONT COLOR="green">// 1. allocate Environment handle and register version </FONT>
72 V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);
73 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
74 {
75 printf("Error AllocHandle\n");
76 exit(0);
77 }
78 V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION,
79 (void*)SQL_OV_ODBC3, 0);
80 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
81 {
82 printf("Error SetEnv\n");
83 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
84 exit(0);
85 }
86 <FONT COLOR="green">// 2. allocate connection handle, set timeout</FONT>
87 V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc);
88 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
89 {
90 printf("Error AllocHDB %d\n",V_OD_erg);
91 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
92 exit(0);
93 }
94 SQLSetConnectAttr(V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
95 <FONT COLOR="green">// 3. Connect to the datasource "web" </FONT>
96 V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "web", SQL_NTS,
97 (SQLCHAR*) "christa", SQL_NTS,
98 (SQLCHAR*) "", SQL_NTS);
99 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
100 {
101 printf("Error SQLConnect %d\n",V_OD_erg);
102 SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1,
103 V_OD_stat, &V_OD_err,V_OD_msg,100,&V_OD_mlen);
104 printf("%s (%d)\n",V_OD_msg,V_OD_err);
105 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
106 exit(0);
107 }
108 printf("Connected !\n");
109 <FONT COLOR="green">/* continued on next page */</FONT>
110 </CODE>
111 </PRE>
112 </TD>
113 </TR>
114 </TABLE>
115 </BODY>
116 </HTML>