📄 connection_examples.html
字号:
<html><head><meta name="KEYWORDS" content=" library, C++, Oracle, SQLServer, DB2, Sybase, Informix, InterBase, SQLBase, ODBC,
SQL, server, client, database, programming, development, software,
Win32, Windows, Linux, Unix, Solaris,
Microsoft, Borland, Centura, Gupta, IBM"><meta name="DESCRIPTION" content=
"SQLAPI++ is C++ library for accessing SQL databases (Oracle, SQLServer, Sybase, DB2, InterBase, SQLBase, Informix, ODBC).
It uses native APIs, provides low-level access to database-specific features."><title>SQLAPI++ Library - Online Documentation - SAConnection Examples</title></head><body bgcolor="#ffffff" BACKGROUND="../images/bg.gif" text="#000000"><!--GlbHdr--> <table WIDTH="600" height="40" frame="hsides" BORDER="2" BORDERCOLOR="#004fcd" align="right" cellpadding="0" cellspacing="2" id="TABLE1">
<tr><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="/index.html" style="TEXT-DECORATION: none">
<font color="white" size="3" face="sans-serif">
<b>Home</b></font></A></td></TD>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="/HowTo/index.html" style="TEXT-DECORATION: none"> <font color="white" size="3" face="sans-serif">
<b>How To</b></font></A></td><!--class="GlbActive"-->
<td width="70" bgcolor="#00bb00" bordercolor="black" align="middle"><A href="/OnLineDoc/index.html" style="TEXT-DECORATION: none"> <font color="white" size="3" face="sans-serif">
<b>Online Documentation</b></font></A></td><!--class="GlbDefault"-->
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="/Support/index.html" style="TEXT-DECORATION: none"> <font color="white" size="3" face="sans-serif">
<b>Support</b></font></A></td>
<td width=70 bgcolor=#000094 bordercolor=black align=middle><A href="/Download/index.html" style="TEXT-DECORATION: none" >
<font color=white size=3 face=sans-serif>
<b>Download</b></font></A></td>
<td width="70" bgcolor="#000094" bordercolor="black" align="middle"><A href="/Order/index.html" style="TEXT-DECORATION: none"> <font color="white" size="3" face="sans-serif">
<b>Order</b></font></A></td> </tr>
</table>
<p> </p>
<table width="100%">
<tr>
<td valign="top" width="165"><!--LeftMenu-->
<table frame="void" CELLSPACING="5" width="165">
<tr><td><p> </p></td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"><A href="Command.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
SACommand</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/red.gif" WIDTH="12" HEIGHT="12"><A href="Connection.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
SAConnection</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"><A href="Exception.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
SAException</font></A> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"><A href="Field.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
SAField</A> </FONT> </td>
</tr>
<tr>
<td><img align="middle" src="../images/green.gif" WIDTH="12" HEIGHT="12"><A href="Param.html" style="TEXT-DECORATION: none">
<font color="white" size="2" face="sans-serif">
SAParam</A> </FONT> </td>
</tr>
<tr><td><hr id=HR1></td></tr>
<tr>
<td><img align=middle src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="String.html" style ="TEXT-DECORATION: none" >
<font color=white size=2 face=sans-serif>
SAString</A> </FONT> </td>
</tr>
<tr>
<td><img align=middle src="../images/green.gif" WIDTH="12" HEIGHT="12">
<A href="DateTime.html" style ="TEXT-DECORATION: none" >
<font color=white size=2 face=sans-serif>
SADateTime</A> </FONT> </td>
</tr>
</table>
</td><!--empty column-->
<td width="20">
<p> </p>
</td><!--Content-->
<td valign="top" align="left">
<table width="100%" cellspacing="2" cellpadding="2">
<tr>
<td align="left"> <A href="Connection.html" style="TEXT-DECORATION: none"><h3>Class Members</h3></A>
</td>
<td align="left"> <A href="Connection_Usage.html" style="TEXT-DECORATION: none"><h3>Usage</h3></A>
</td>
</tr>
<tr>
<td colspan="2"><h1>SAConnection Examples</h1></td>
</tr>
</table>
<HR id=HR1>
<A name=cnstr></A>
<H3>SAConnection constructor</H3>
<P><FONT face=Courier>/*<BR>Default constructor creates SAConnection object that
is not initially connected to database or associated with specific client.
You have to call SAConnection::setClient() and/or SAConnection::Connect()
later.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P><FONT face=Courier><STRONG>SAConnection con, con2;</STRONG>
</FONT></P>
<HR id=HR1>
<A name=dstr></A>
<H3>SAConnection destructor</H3>
<P><FONT face=Courier>/*<BR>Destructor deletes SAConnection object. It also
disconnect object from database if needed.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P><FONT face=Courier>SAConnection pCon = new <FONT
face=Courier>SAConnection</FONT> ;</FONT></P>
<P><FONT face=Courier>...</FONT></P>
<P><FONT face=Courier><STRONG>delete pCon;</STRONG></FONT></P>
<HR id=HR1>
<A name=setClient></A>
<H3>setClient</H3>
<P><FONT face=Courier>/*<BR>
Exception handling is omitted for simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P><FONT face=Courier>SAConnection Con;</FONT></P>
<P><FONT
face=Courier><STRONG>Con.setClient(SA_Oracle_Client);</STRONG></FONT></P>
<HR id=HR1>
<A name=Client></A>
<H3>Client</H3><FONT face=Courier>
<P><FONT face=Courier>/*<BR> Queries current DBMS
client set by SAConnection::setClient or SAConnection::Connect.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P></FONT><FONT face=Courier>SAConnection Con;</FONT></P>
<P><FONT face=Courier><FONT face=Courier>/* connect to database and so on
*/<BR></FONT>...</FONT></P>
<P><FONT face=Courier>switch(</FONT><FONT face=Courier><STRONG>Con.Client()</STRONG>)<BR></FONT><FONT face=Courier>{</FONT><FONT face=Courier><BR> case
SA_Client_NotSpecified:<BR>
break;<BR> case
SA_ODBC_Client:<BR> /*do smth.
specific for ODBC*/<BR>
break;<BR> case
SA_Oracle_Client:<BR> /*do smth.
specific for Oracle*/<BR>
break;<BR> case
SA_SQLServer_Client:<BR> /*do
smth. specific for
SQLServer*/<BR>
break;<BR> case
SA_Interbase_Client:<BR> /*do
smth. specific for
Interbase*/<BR>
break;<BR> case
SA_SQLBase_Client:<BR> /*do
smth. specific for SQLBase*/<BR>
break;<BR> case
SA_DB2_Client:<BR> /*do smth.
specific for DB2*/<BR>
break;<BR> case
SA_Informix_Client:<BR> /*do
smth. specific for
Informix*/<BR>
break;<BR> case
SA_Sybase_Client:<BR> /*do smth.
specific for Sybase*/<BR>
break;</FONT><FONT face=Courier><BR>}</FONT></P>
<HR id=HR1>
<A name=ClientVersion></A>
<H3>ClientVersion</H3>
<P><FONT face=Courier>/*<BR>Query and print client version.<BR>
Exception handling is omitted for simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P><FONT face=Courier>SAConnection con;</FONT></P>
<P><FONT
face=Courier>con.setClient(SA_Oracle_Client);</FONT></P>
<P><FONT face=Courier>short minor =
(short)(<STRONG>con.ClientVersion() </STRONG>& 0xFFFF);</FONT></P>
<P><FONT face=Courier>short major =
(short)(<STRONG>con.ClientVersion() </STRONG>>> 16);</FONT></P>
<P><FONT face=Courier>printf("Client version:
%hd.%hd\n", major, minor);</FONT></P>
<HR id=HR1>
<A name=ServerVersion></A>
<H3>ServerVersion</H3>
<P><FONT face=Courier>/*<BR>Query and print server version.<BR>
Exception handling is omitted for simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P><FONT face=Courier>
<P><FONT face=Courier>SAConnection con;</FONT></P>
<P>con.Connect("dbname", "username", "password",
SA_Oracle_Client);</P>
<P>short minor = (short)(<STRONG>con.ServerVersion()
</STRONG>& 0xFFFF);</P>
<P>short major = (short)(<STRONG>con.ServerVersion()
</STRONG>>> 16);</P>
<P>printf("Server version: %hd.%hd\n", major, minor);</P> </FONT>
<HR id=HR1>
<A name=ServerVersionString></A>
<H3>ServerVersionString</H3><FONT face=Courier>
<P><FONT face=Courier>/*<BR>Query and print server version
string.<BR>
Exception handling is omitted for simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P><FONT face=Courier>
<P><FONT face=Courier>SAConnection con;</FONT></P>
<P>con.Connect("dbname", "username", "password",
SA_Oracle_Client);</P>
<P>printf("Server: %s\n", (const
char*)<STRONG>con.ServerVersionString()</STRONG>);</P>
<P> </FONT></FONT>
<HR id=HR1>
<A name=isConnected></A>
<H3>isConnected</H3><FONT face=Courier><FONT face=Courier>
<P><FONT face=Courier>/*<BR>Disconnect only if we are really
connected.<BR>*/</FONT></P>
<P><FONT face=Courier>SAConnection con;</FONT></P>
<P><FONT face=Courier>/* connect to database and so on
*/<BR>...</FONT></P>
<P><FONT face=Courier>if(<STRONG>con.isConnected()</STRONG>)</FONT></P>
<P><FONT face=Courier> con.Disconnect(); </FONT>
<P> </FONT></FONT>
<HR id=HR1>
<A name=Connect></A>
<H3>Connect</H3>
<P><FONT face=Courier>/*<BR> First example.<BR>Initialize client and
connect to server with one call.<BR>Exception handling is omitted for
simplicity.<BR>*/</FONT><FONT face=Courier></FONT></P>
<P><FONT face=Courier>SAConnection con, con2;</FONT></P>
<P><STRONG><FONT face=Courier>con.Connect("dbname", "username", "password",
SA_Oracle_Client);</FONT></STRONG></P>
<P><FONT face=Courier><STRONG>con2.Connect("srvname@dbname", "username",
"password", SA_Sybase_Client);</STRONG>
</FONT></P>
<P><FONT face=Courier>/*<BR>Second example.<BR>First initialize client
then connect to server.<BR>Exception handling is omitted for
simplicity.<BR>*/</FONT></P>
<P><FONT face=Courier>SAConnection con, con2;</FONT></P>
<P><FONT face=Courier>con.setClient(SA_Oracle_Client);</FONT></P>
<P><FONT face=Courier>con2.setClient(SA_Sybase_Client);</FONT></P>
<P><STRONG><FONT face=Courier>con.Connect("dbname", "username",
"password");</FONT></STRONG></P>
<P><FONT face=Courier><STRONG>con2.Connect("srvname@dbname", "username",
"password");</STRONG>
</FONT></P>
<HR id=HR1>
<A name=Disconnect></A>
<H3>Disconnect</H3>
<P><FONT face=Courier>/*<BR> First
example.<BR>Disconnect only if we are really connected.<BR>*/</FONT></P>
<P><FONT face=Courier>SAConnection con;</FONT></P>
<P><FONT face=Courier>/* connect to database and so on
*/<BR>...</FONT></P>
<P><FONT face=Courier>if(con.isConnected())</FONT></P>
<P><FONT face=Courier>
<STRONG>con.Disconnect();</STRONG>
</FONT></P>
<P><FONT face=Courier>/*<BR>Second example.<BR>Disconnect is made
implicitly in destructor.<BR>*/</FONT></P>
<P><FONT face=Courier>{</FONT></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -