📄 bind.html
字号:
(for more details see
<A href="commands.html">Executing an SQL command</A>
and <A
href="procedures.html">Executing stored
procedures</A>):<BR><BR><FONT face=System>SACommand cmd(&Connection, "insert
into employees (name, age, comment) values
(:1, :2, :3)");</FONT><BR><BR>After creating
<EM>cmd</EM>
object and passing a command text three <A
href="../OnLineDoc/Param.html">SAParam</A> objects are created
automatically.
<LI>Bind input variables by assigning values to <A
href="../OnLineDoc/Param.html">SAParam</A> objects.<BR>Because of
identifying parameters by their positions we should call <A
href="../OnLineDoc/Command_Param.html">SACommand::Param</A>
method with the next syntax:<BR><BR><FONT
face=System>cmd.Param(1).setAsString() = "Roy
Mann";<BR>cmd.Param(2).setAsLong() = 42;<BR><FONT
face=System>cmd.Param(3).setAsNull();</FONT> <BR></FONT><BR>The
lines above set the value <EM>"Roy Mann"</EM> to the
parameter marked by <EM>:1</EM>, <EM>42</EM> to the parameter
marked by <EM>:2</EM> and <EM>null</EM> value to the
parameter marked by <EM>:3</EM>.
<LI>Execute the command:<BR><BR><FONT
face=System>cmd.Execute();<BR></FONT></LI></OL>
<H3>Example 2. </H3>
<P> We can use another variant of command text to insert a row
into the table:</P>
<P><EM>insert into EMPLOYEES (NAME, AGE) values (:name, :age,
:comment)</EM></P>
<P>This command has three input parameters to set values to
<EM>NAME</EM> and <EM>AGE</EM> columns. In that example input parameters
are marked by <EM>:name, :age, :comment</EM>. It means we will identify
a parameter by its <STRONG>name</STRONG> . </P>
<P>To create and execute the command we should do the following:</P>
<OL>
<LI>Create a command object <EM>cmd</EM>
(for more details see
<A href="commands.html">Executing an SQL command</A>
and <A
href="procedures.html">Executing stored
procedures</A>):<BR><BR><FONT face=System>SACommand cmd(&Connection, "insert
into employees (name, age, comment) values
(:name, :age, :comment)");</FONT><BR><BR>After creating
<EM>cmd</EM>object three <A
href="../OnLineDoc/Param.html">SAParam</A>
objects are created
automatically.
<LI>Bind input variables by assigning a value to <A
href="../OnLineDoc/Param.html">SAParam</A> object.<BR>Because of
identifying parameters by their names we should call <A
href="../OnLineDoc/Command_Param.html">SACommand::Param</A>
method with the next syntax:<BR><BR><FONT
face=System>cmd.Param("name").setAsString() =
"Roy Mann";<BR>cmd.Param("age").setAsLong() = 42;<BR><FONT
face=System>cmd.Param("comment").setAsString() =
"Manager";</FONT> <BR></FONT><BR>The lines above sets the value <EM>"Roy
Mann"</EM> to the parameter marked by <EM>:name</EM> ,
<EM>42</EM> to the parameter marked by <EM>:age</EM> and <EM>"Manager"</EM> to the
parameter marked by <EM>:comment</EM>.
<LI>Execute the command:<BR><BR><FONT
face=System>cmd.Execute();<BR></FONT></LI></OL>
<P> </P>
<H2><A name="Using the stream operator<<"><A name="Using the stream operator<<">Using the stream operator<<</A><A name="Using the stream operator<<"
>
</A></A></H2>
<P><A
href="../OnLineDoc/Command_operator_bind.html">SACommand::operator<<</A>
is a stream operator, so usually it is more convenient to use it
instead of assigning <A href="../OnLineDoc/Param.html">SAParam</A>
objects.</P>
<H3>Example 3. </H3>
<P>Let's execute the next SQL command to insert a row into the
table:</P>
<P><EM>insert into
EMPLOYEES (NAME, AGE, COMMENT) values (:1, :2, :3)</EM></P>
<P>This command has three input parameters to set values to
<EM>NAME, AGE</EM> and <EM>COMMENT</EM> columns. In that example input parameters
are marked by <EM>:1, :2, :3</EM>. It means we will identify a
parameter by its <STRONG>position</STRONG> . </P>
<P>To create and execute the command we should do the following:</P>
<OL>
<LI>Create a command object <EM>cmd</EM>
(for more details see
<A href="commands.html">Executing an SQL command</A>
and <A
href="procedures.html">Executing stored
procedures</A>):<BR><BR><FONT face=System>SACommand cmd(&Connection, "insert
into employees (name, age, comment) values
(:1, :2, :3)");</FONT><BR><BR>After creating
<EM>cmd</EM>
object three <A
href="../OnLineDoc/Param.html">SAParam</A>
objects are created automatically
but in contrast to the previous examples we will
not use them explicitly.
<LI>Bind input variables by using <A
href="../OnLineDoc/Command_operator_bind.html">SACommand::operator<<</A>.<BR>Because
of identifying parameters by their positions we can put the
values into a stream:<BR><BR><FONT face=System>cmd << "Roy Mann"
<< 42 << SANull();<BR></FONT><BR>
<P>The
line above sets the value <EM>"Roy Mann"</EM> to the
parameter marked by <EM>:1</EM>, <EM>42</EM> to the parameter
marked by <EM>:2</EM> and <EM>null</EM> value to the
parameter marked by <EM>:3</EM> . </P>
<P>The order that you associate the values
is important (i.e. the first '<<' associates a bind with
<EM>:1</EM>, the second '<<' with <EM
>:2</EM>, the third '<<' with
<EM>:3</EM> ). </P>
<LI>Execute the command:<BR><BR><FONT
face=System>cmd.Execute();<BR></FONT></LI></OL>
<H3>Example 4. </H3>
<P> We can use another variant of command text to insert a row
into the table:</P>
<P><EM>insert into
EMPLOYEES (NAME, AGE, COMMENT) values (:name, :age,
:comment)</EM></P>
<P>This command has three input parameters to set values to
<EM>NAME, AGE</EM> and <EM>COMMENT</EM> columns. In that example input parameters
are marked by <EM>:name, :age, :comment</EM>. It means we will identify
a parameter by its <STRONG>name</STRONG> . </P>
<P>To create and execute the command we should do the following:</P>
<OL>
<LI>Create a command object <EM>cmd</EM>
(for more details see
<A href="commands.html">Executing an SQL command</A>
and <A
href="procedures.html">Executing stored
procedures</A>):<BR><BR><FONT face=System>SACommand cmd(&Connection, "insert
into employees (name, age, comment) values
(:name, :age, :comment)");</FONT><BR><BR>After creating
<EM>cmd</EM> object three <A
href="http://www.sqlapi.com/OnLineDoc/Param.html">SAParam</A> objects are created automatically, but we will
not use them explicitly.
<LI>Bind input variables by putting values into a
stream.<BR>Because of identifying parameters by their names we
should use more complex sequence of stream elements. In
<STRONG>Example 3</STRONG> we put into a stream only parameters values
according to their positions (numbers). Now we should
explicitly specify the position of bind variable before putting
a value into a stream. To specify a position use
<STRONG>SAPos</STRONG> object:<BR><BR><FONT face=System>cmd
<< SAPos("name") << "Roy
Mann" << SAPos("age") << 42
<< SAPos("comment") << "Manager";<BR></FONT><BR>
<LI>Execute the command:<BR><BR><FONT
face=System>cmd.Execute();</FONT></LI></OL>
<P>Binding Long, BLob and CLob data can have some differences from binding
other data types. <FONT color=black face="">See <A
href="blobs.html">Working with Long or Lob(CLob, BLob) Data</A>
to get
more information.</FONT></P>
<h3>Problems and Questions</h3>
<p>If you don't find the answer to you questions
or have some problems on using the Library, please, send e-mail to
<A href="mailto:howto@sqlapi.com" >howto@sqlapi.com</A>. </p>
<p> </p>
</td>
<tr></tr>
</table></FONT>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -