I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined" , that I would like to set to Null. My first thought was to create an array and then use a foreach statement to run each string through the switch statement. The obvious problem here is that you cannot assign to the iteration variables. How can I get around this?
Apr 11 '08
13 1995
So here you are formatting the first item in the array to equal the next item and then adding the string AND to the end I am inexperience both with building sql statements dynamically as well as not using parameters.
Not quite. The way string.Format works is that it evaluates "{#}" as one of the parameters following. So: -
string temp = "Bob";
-
string out = String.Format("{0} is his name",temp);
-
//out == "Bob is his name"
-
What I did in that loop:
lets say I have a sql statement with 4 possible params: nameFirst, nameLast, nameMiddle, and userName. What I might do is: -
string[] paramArray = new string[4];
-
paramArray[0] = "nameFirst";
-
paramArray[1] = "nameLast";
-
paramArray[2] = "nameMiddle";
-
paramArray[3] = "userName";
-
Then, I would get my value strings like you did, and assign them to stringsArray. Then run them through the switch statement and so forth.
Then you could build your sql statement. -
string sql = "select * from tableName ";
-
bool hasParams = false;
-
for(int i=0; i<paramArray.Length; i++)
-
{
-
if(stringsArray[i] != null)
-
{
-
hasParams = true;
-
break;
-
}
-
}
-
if(hasParams)
-
{
-
sql += " where ";
-
for(int i=0; i<stringsArray.Length; i++)
-
{
-
if(stringsArray[i] != null)
-
sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
-
}
-
sql = sql.Substring(0,sql.Length - 4);
-
}
-
-
What I did there is check to see if any of the strings aren't null (as per the way you were setting them null in the switch.) Then, if there are any "where" parameters, we finish building the sql statement. What that String.Format would evaluate to is "paramName = 'paramValue' AND "
So, in practice, if nameFirst should have a value of Bob, it will evaluate to:
"nameFirst = 'Bob' AND "
And at the end, we remove the extra "AND " (4 chars off the end.) You have a complete sql statement.
Maybe not the most simple way of doing it, but it works.
So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:
INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**
cmd.Parameters. AddWithValue("@ parameter1", value1);
cmd.Parameters. AddWithValue("@ parameter2", value2);
***
Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.
So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:
INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**
cmd.Parameters. AddWithValue("@ parameter1", value1);
cmd.Parameters. AddWithValue("@ parameter2", value2);
***
Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.
Ok, I was working on the incorrect assumption on a select statement. What you want to do is perfectly possible with System.Data.Sql Types. -
SqlCommand cmd = new SqlCommand(sqlInsertText,connectionObject);
-
SqlParameter parameter = new SqlParameter("@param1", System.Data.SqlTypes.SqlString.Null);
-
cmd.Parameters.Add(parameter);
-
That will insert a null value for that param. Sorry for all the confusion :(
Now if this isn't a varchar or nvarchar field you might have to use System.Data.Sql Types. whateverDbTypeY ouNeed, but this works for an nvarchar.
I suppose I should have been more specific... but I think this may have something to do with why I was having a difficult time grasping this. I appreciate the effort nonetheless.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Guadala Harry |
last post by:
After RTFM, I hope I have missed something: I would like to have a switch()
statement process multiple input values the same way - but without having
multiple case: blocks for multiple input values that need to be processed
the same way. For example, please consider the following sample switch block
and notice that "other value 1, 2, and 3" all execute DoOtherThing(). How
can I get all of those cases to NOT have to appear in
their own...
|
by: gordon |
last post by:
Hi
I am trying to set some elements to values depending on the selection in a
test box. The first possible value that can be selected should set
fmttest1= "", fmttest2=" to " and fmttest3="". The second value that can be
selected from the test box should set the fmttest1 ="From ", fmttest2=" to
", fmttest3=" less than".
here is what i have so far but i cannot get an if statement to apply the
condition to more than one item.
|
by: priyanka |
last post by:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test...
|
by: Kevin Blount |
last post by:
I've been unsuccessful finding a site that tells me, so I'm asking here:
can I have multiple 'clauses' per 'case' in a 'switch' statement?
e.g.
switch "myVar"
{
case "1", "a":
break;
|
by: Peter Kirk |
last post by:
Hi
i have a string variable which can take one of a set of many values.
Depending on the value I need to take different (but related) actions. So I
have a big if/else-if construct - but what is a better way of doing this?
if ("control" == commandString)
{
}
else if ("execute" == commandString)
| |
by: jsta43catrocks |
last post by:
In C++, my "switch" statement is okay when I ask it do evaluate ONE expression. (My number that I'm evaluating is one of ten single digits; I have ten cases for the ten digits.)
BUT, I have five separate digits to evaluate.
I can't believe that the only solution is to do:
switch (num1)
{
case 0:...
case 9:
|
by: steve.lorimer |
last post by:
Thank you for taking the time to look at this:
I'm looking for a pre-processor command that will allow me to resolve
const strings into const char literals at compile time.
Looking at the code below, I can take 5 characters and create a const
uint64_t value at compile time.
I can then create a switch statement that has these const values as
the various cases.
Since each case resolves to an integer constant value at compile time,
|
by: steve.lorimer |
last post by:
I'm looking for a pre-processor command that will allow me to resolve
const
strings into const char literals at compile time.
Looking at the code below, I can take 5 characters and create a const
uint64_t value at compile time.
I can then create a switch statement that has these const values as
the
various cases.
Since each case resolves to an integer constant value at compile
|
by: Silent1Mezzo |
last post by:
Is it possible to have multiple control variables in a switch statement?
For example:
switch(a , b)
{
case(a>b):
case(b<a):
case(a==b):
}
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |