Using IDbCommand.Prepare()

  • Thread starter Thread starter Soren Staun Jorgensen
  • Start date Start date
S

Soren Staun Jorgensen

Hi,

It it possible to store multiple queries, returning a resultset, using
IDbCommand.Prepare() on the server - and afterwards reading all results, one
after one, using IDataReader.NextResult() to advance to next results, if any
??

Kinda like executing a batch query from a single stored procedure ?

Thanks in advance....

Soren
 
Hi Soren,

Based on my understanding, you want to return multiple result sets from a
single command object.

It is possible to do this. For example, you can put multiple SELECT
statements in the CommandText property of a SqlCommand. SELECT statements
are seprated with a semicolon ';' . When filling the DataSet, the result
sets will be put into multiple tables in the same DataSet. Here's an
example:

sqlcommand.CommandText = "SELECT * FROM Customers; SELECT * FROM Employees";

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Soren Staun Jorgensen" <[email protected]>
| Subject: Using IDbCommand.Prepare()
| Date: Wed, 12 Nov 2003 13:24:02 +0100
| Lines: 15
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: 0x50c70a5c.adsl-fixed.tele.dk 80.199.10.92
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66109
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Hi,
|
| It it possible to store multiple queries, returning a resultset, using
| IDbCommand.Prepare() on the server - and afterwards reading all results,
one
| after one, using IDataReader.NextResult() to advance to next results, if
any
| ??
|
| Kinda like executing a batch query from a single stored procedure ?
|
| Thanks in advance....
|
| Soren
|
|
|
|
 
Hi Kevin

Thanks for your reply, I did'nt know this was possible, so you answer did
help something though :o)

Actually I have a stored procedure which takes some parameters, say @X and
@Y, and returns a single resultset for each call.
This procedure has to be executed multiple times with different values for
@X and @Y.
I could then simply set parameters, execute and read result for each time
necessary, but this would be far less efficient than returning batched
resultsets.
So what i meant was; can I prepare these calls to the stored procedure, with
different values for @X and @Y, using Prepare() and then read all,
hopefully, returned resultsets like a batch query ??

Soren
 
Hi Soren,

I don't think this can work. A DbCommand can take only one group of
parameter values before it is executed. Prepare method is used to compile
the command text when command type is set to CommandType.Text. It is used
to accelerate the execution of the command. According to the source code,
Prepare() will do nothing if the command type is StoredProcedure.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Soren Staun Jorgensen" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: Re: Using IDbCommand.Prepare()
| Date: Thu, 13 Nov 2003 21:57:27 +0100
| Lines: 80
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: 0x50c70a5c.adsl-fixed.tele.dk 80.199.10.92
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66257
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Hi Kevin
|
| Thanks for your reply, I did'nt know this was possible, so you answer did
| help something though :o)
|
| Actually I have a stored procedure which takes some parameters, say @X and
| @Y, and returns a single resultset for each call.
| This procedure has to be executed multiple times with different values for
| @X and @Y.
| I could then simply set parameters, execute and read result for each time
| necessary, but this would be far less efficient than returning batched
| resultsets.
| So what i meant was; can I prepare these calls to the stored procedure,
with
| different values for @X and @Y, using Prepare() and then read all,
| hopefully, returned resultsets like a batch query ??
|
| Soren
|
|
| "Kevin Yu [MSFT]" <[email protected]> skrev i en meddelelse
| | > Hi Soren,
| >
| > Based on my understanding, you want to return multiple result sets from
a
| > single command object.
| >
| > It is possible to do this. For example, you can put multiple SELECT
| > statements in the CommandText property of a SqlCommand. SELECT
statements
| > are seprated with a semicolon ';' . When filling the DataSet, the result
| > sets will be put into multiple tables in the same DataSet. Here's an
| > example:
| >
| > sqlcommand.CommandText = "SELECT * FROM Customers; SELECT * FROM
| Employees";
| >
| > If anything is unclear, please feel free to reply to the post.
| >
| > Kevin Yu
| > =======
| > "This posting is provided "AS IS" with no warranties, and confers no
| > rights."
| >
| > --------------------
| > | From: "Soren Staun Jorgensen" <[email protected]>
| > | Subject: Using IDbCommand.Prepare()
| > | Date: Wed, 12 Nov 2003 13:24:02 +0100
| > | Lines: 15
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.adonet
| > | NNTP-Posting-Host: 0x50c70a5c.adsl-fixed.tele.dk 80.199.10.92
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.adonet:66109
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| > |
| > | Hi,
| > |
| > | It it possible to store multiple queries, returning a resultset, using
| > | IDbCommand.Prepare() on the server - and afterwards reading all
results,
| > one
| > | after one, using IDataReader.NextResult() to advance to next results,
if
| > any
| > | ??
| > |
| > | Kinda like executing a batch query from a single stored procedure ?
| > |
| > | Thanks in advance....
| > |
| > | Soren
| > |
| > |
| > |
| > |
| >
|
|
|
 
Soren

You could do this in code using a similar technique, i.e.

sqlcommand.CommandText = "EXEC dbo.MyProc 1, 2; EXEC dbo.MyProc 3, 4";

You have to guard against illegal values in the parameters, but that's
fairly simple to acheive

Paul
 
Hi Soren,

I'd like to know if this issue had been resolved yet. Is there anything
that I can help on this? I'm still monitoring on it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| X-Tomcat-ID: 47403977
| References: <#[email protected]>
<[email protected]>
<[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (Kevin Yu [MSFT])
| Organization: Microsoft
| Date: Fri, 14 Nov 2003 01:42:02 GMT
| Subject: Re: Using IDbCommand.Prepare()
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Lines: 106
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66285
| NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
|
| Hi Soren,
|
| I don't think this can work. A DbCommand can take only one group of
| parameter values before it is executed. Prepare method is used to compile
| the command text when command type is set to CommandType.Text. It is used
| to accelerate the execution of the command. According to the source code,
| Prepare() will do nothing if the command type is StoredProcedure.
|
| If anything is unclear, please feel free to reply to the post.
|
| Kevin Yu
| =======
| "This posting is provided "AS IS" with no warranties, and confers no
| rights."
|
| --------------------
| | From: "Soren Staun Jorgensen" <[email protected]>
| | References: <#[email protected]>
| <[email protected]>
| | Subject: Re: Using IDbCommand.Prepare()
| | Date: Thu, 13 Nov 2003 21:57:27 +0100
| | Lines: 80
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | Message-ID: <[email protected]>
| | Newsgroups: microsoft.public.dotnet.framework.adonet
| | NNTP-Posting-Host: 0x50c70a5c.adsl-fixed.tele.dk 80.199.10.92
| | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.adonet:66257
| | X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| |
| | Hi Kevin
| |
| | Thanks for your reply, I did'nt know this was possible, so you answer
did
| | help something though :o)
| |
| | Actually I have a stored procedure which takes some parameters, say @X
and
| | @Y, and returns a single resultset for each call.
| | This procedure has to be executed multiple times with different values
for
| | @X and @Y.
| | I could then simply set parameters, execute and read result for each
time
| | necessary, but this would be far less efficient than returning batched
| | resultsets.
| | So what i meant was; can I prepare these calls to the stored procedure,
| with
| | different values for @X and @Y, using Prepare() and then read all,
| | hopefully, returned resultsets like a batch query ??
| |
| | Soren
| |
| |
| | "Kevin Yu [MSFT]" <[email protected]> skrev i en meddelelse
| | | | > Hi Soren,
| | >
| | > Based on my understanding, you want to return multiple result sets
from
| a
| | > single command object.
| | >
| | > It is possible to do this. For example, you can put multiple SELECT
| | > statements in the CommandText property of a SqlCommand. SELECT
| statements
| | > are seprated with a semicolon ';' . When filling the DataSet, the
result
| | > sets will be put into multiple tables in the same DataSet. Here's an
| | > example:
| | >
| | > sqlcommand.CommandText = "SELECT * FROM Customers; SELECT * FROM
| | Employees";
| | >
| | > If anything is unclear, please feel free to reply to the post.
| | >
| | > Kevin Yu
| | > =======
| | > "This posting is provided "AS IS" with no warranties, and confers no
| | > rights."
| | >
| | > --------------------
| | > | From: "Soren Staun Jorgensen" <[email protected]>
| | > | Subject: Using IDbCommand.Prepare()
| | > | Date: Wed, 12 Nov 2003 13:24:02 +0100
| | > | Lines: 15
| | > | X-Priority: 3
| | > | X-MSMail-Priority: Normal
| | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | > | Message-ID: <#[email protected]>
| | > | Newsgroups: microsoft.public.dotnet.framework.adonet
| | > | NNTP-Posting-Host: 0x50c70a5c.adsl-fixed.tele.dk 80.199.10.92
| | > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| | > | Xref: cpmsftngxa06.phx.gbl
| | microsoft.public.dotnet.framework.adonet:66109
| | > | X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| | > |
| | > | Hi,
| | > |
| | > | It it possible to store multiple queries, returning a resultset,
using
| | > | IDbCommand.Prepare() on the server - and afterwards reading all
| results,
| | > one
| | > | after one, using IDataReader.NextResult() to advance to next
results,
| if
| | > any
| | > | ??
| | > |
| | > | Kinda like executing a batch query from a single stored procedure ?
| | > |
| | > | Thanks in advance....
| | > |
| | > | Soren
| | > |
| | > |
| | > |
| | > |
| | >
| |
| |
| |
|
|
 
Back
Top