PC Review


Reply
Thread Tools Rate Thread

ADO.NET SQL Parameters

 
 
=?Utf-8?B?UmFkaW9TaWduYWw=?=
Guest
Posts: n/a
 
      11th Jan 2005
In my C# code, I want to SELECT rows first based upon one column's values and
then different rows based upon a different column's values. Using Query
Builder, I construct: SELECT * FROM myTable WHERE (Var1 = ?) OR (Var2 = ?)
Then in code:
MyDataAdapter.GetFillParameters()[0].Value = myValue;
MyDataAdapter.GetFillParameters()[1].Value = -9999;

There's got to be a better way? What can I do without having a second data
adapter with a different SELECT SQL? Is there anyway to update the SELECT
statement in the code to reference the other column's value?
 
Reply With Quote
 
 
 
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      11th Jan 2005
Hi,

IMO the fastest way is to use two selects.
Other then that you might do something like:
WHERE (Var1 = ? OR ? is NULL) AND (Var2 = ? OR ? IS NULL)
(introduce two new parameters that serve as flags whether the criteria is
valid).
However, you might run into performance drawbacks doing it in this way...

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com


"RadioSignal" <(E-Mail Removed)> wrote in message
news6EED71C-59E1-4016-837E-(E-Mail Removed)...
> In my C# code, I want to SELECT rows first based upon one column's values
> and
> then different rows based upon a different column's values. Using Query
> Builder, I construct: SELECT * FROM myTable WHERE (Var1 = ?) OR (Var2 = ?)
> Then in code:
> MyDataAdapter.GetFillParameters()[0].Value = myValue;
> MyDataAdapter.GetFillParameters()[1].Value = -9999;
>
> There's got to be a better way? What can I do without having a second data
> adapter with a different SELECT SQL? Is there anyway to update the SELECT
> statement in the code to reference the other column's value?



 
Reply With Quote
 
William \(Bill\) Vaughn
Guest
Posts: n/a
 
      11th Jan 2005
Did you consider using the UNION operator that can return a single rowset
built from two SELECT products?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"RadioSignal" <(E-Mail Removed)> wrote in message
news6EED71C-59E1-4016-837E-(E-Mail Removed)...
> In my C# code, I want to SELECT rows first based upon one column's values
> and
> then different rows based upon a different column's values. Using Query
> Builder, I construct: SELECT * FROM myTable WHERE (Var1 = ?) OR (Var2 = ?)
> Then in code:
> MyDataAdapter.GetFillParameters()[0].Value = myValue;
> MyDataAdapter.GetFillParameters()[1].Value = -9999;
>
> There's got to be a better way? What can I do without having a second data
> adapter with a different SELECT SQL? Is there anyway to update the SELECT
> statement in the code to reference the other column's value?



 
Reply With Quote
 
=?Utf-8?B?UmFkaW9TaWduYWw=?=
Guest
Posts: n/a
 
      12th Jan 2005
Thank you. I'm not quite sure how that would help in this case. I really only
want to execute one SELECT with one WHERE at a time. Trouble is I can't
easily switch the SQL statement created by the IDE to another defferent WHERE
clause. After Miha's reply, I tried updating the SQL SELECT statement in my
code but then I had to clear parameter characteristics and re-add the new
parameter to the data adapter. That works fine, albeit inconvenient. I
suppose the other possibility is to have another data adapter containing the
second SELECT statement and switch between them as needed in my code.

"William (Bill) Vaughn" wrote:

> Did you consider using the UNION operator that can return a single rowset
> built from two SELECT products?
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
>
> "RadioSignal" <(E-Mail Removed)> wrote in message
> news6EED71C-59E1-4016-837E-(E-Mail Removed)...
> > In my C# code, I want to SELECT rows first based upon one column's values
> > and
> > then different rows based upon a different column's values. Using Query
> > Builder, I construct: SELECT * FROM myTable WHERE (Var1 = ?) OR (Var2 = ?)
> > Then in code:
> > MyDataAdapter.GetFillParameters()[0].Value = myValue;
> > MyDataAdapter.GetFillParameters()[1].Value = -9999;
> >
> > There's got to be a better way? What can I do without having a second data
> > adapter with a different SELECT SQL? Is there anyway to update the SELECT
> > statement in the code to reference the other column's value?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?UmFkaW9TaWduYWw=?=
Guest
Posts: n/a
 
      12th Jan 2005
Thank you. I figured out how to update the SQL SELECT statement from the IDE
in the data adapter but then had to clear the old parameter characteristics
and re-add my new parameter. That worked fine, albeit inconvenient. Perhapps
for instances like this I should just create a "new" data adapter in my code
with a hardcoded parameterless SQL SELECT... WHERE clause.

"Miha Markic [MVP C#]" wrote:

> Hi,
>
> IMO the fastest way is to use two selects.
> Other then that you might do something like:
> WHERE (Var1 = ? OR ? is NULL) AND (Var2 = ? OR ? IS NULL)
> (introduce two new parameters that serve as flags whether the criteria is
> valid).
> However, you might run into performance drawbacks doing it in this way...
>
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> SLODUG - Slovene Developer Users Group
> www.rthand.com
>
>
> "RadioSignal" <(E-Mail Removed)> wrote in message
> news6EED71C-59E1-4016-837E-(E-Mail Removed)...
> > In my C# code, I want to SELECT rows first based upon one column's values
> > and
> > then different rows based upon a different column's values. Using Query
> > Builder, I construct: SELECT * FROM myTable WHERE (Var1 = ?) OR (Var2 = ?)
> > Then in code:
> > MyDataAdapter.GetFillParameters()[0].Value = myValue;
> > MyDataAdapter.GetFillParameters()[1].Value = -9999;
> >
> > There's got to be a better way? What can I do without having a second data
> > adapter with a different SELECT SQL? Is there anyway to update the SELECT
> > statement in the code to reference the other column's value?

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpectedly prompted for parameters using parameters of form EdA Microsoft Access Forms 0 30th Apr 2008 09:53 PM
Help Needed. Update with parameters error - No value given for one or more parameters. Hexman Microsoft VB .NET 2 10th Aug 2006 07:46 PM
How can i make a help file with parameters as the menus parameters =?Utf-8?B?S2ltb24=?= Microsoft Access 0 21st Oct 2005 05:56 PM
Passing output parameters to SQL Server stored procedures through an SqlCommand object's Parameters collection Mark Rae Microsoft ADO .NET 8 26th Sep 2005 03:28 PM
Why is 2098 the maximum number of parameters in SqlCommand.Parameters Paul Clark Microsoft ADO .NET 3 24th Aug 2003 06:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:34 AM.