Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Default SqlParameter Value To Null
Message
De
14/02/2009 13:56:07
 
 
À
14/02/2009 12:58:47
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01381129
Message ID:
01381797
Vues:
22
>I am surprised at your reply. I know that I have used NULL values as meaningful values before. Unfortunately I have been thinking for a half hour of an example but can't. :-(

And when you come up with an example, let us know ... <g> ... I can't imagine NULL would be meaningful in any way other than to mean the user has never entered data for that column. Sorry! <g>

>BTW, C# rocks. I am really digging it.

Do you like it better than VB? (here comes the flame wars!)

For some reason I keep getting caught up on lines like these though:
>StringBuilder x = New StringBuilder; //Stupid lowercase new and () after empty constructor calls

7 years ago when I started with C# after having done various flavors of FoxPro for 15 years, I ran into similar issues. The semi-colon was really a major pain, because of how it's the exact opposite behavior between FoxPro and C#. Now however, every once in awhile I find myself tweaking an old VFP app and mis-using the semi-colon yet again. <g>

~~Bonnie




>Bonnie,
>I am surprised at your reply. I know that I have used NULL values as meaningful values before. Unfortunately I have been thinking for a half hour of an example but can't. :-(
>
>Basically, on an INSERT I would default the value to NULL and not pass it in, like we've mentioned. But in an UPDATE sometime I would have valid reasons to pass in System.DbNull.Value.
>
>BTW, C# rocks. I am really digging it. For some reason I keep getting caught up on lines like these though:
>
>StringBuilder x = New StringBuilder; //Stupid lowercase new and () after empty constructor calls
>
>
>Oh, and comments. It's hard to break the 'Comment habit.
>
>>As Mike and Naomi both mentioned, your Stored Proc parameters should be defaulting to NULL and should know how to process NULL values, something like this:
>>
>>
>>CREATE PROCEDURE bsp_ActivityPut
>>	@activitykey		bigint = NULL OUTPUT,
>>	@userkey		bigint = NULL,
>>	@startdatetime		datetime = NULL,
>>	@enddatetime		datetime = NULL
>>AS
>>		UPDATE Activity
>>		SET userkey = ISNULL(@userkey, userkey),
>>		    startdatetime = ISNULL(@startdatetime, startdatetime),
>>		    enddatetime = ISNULL(@enddatetime, enddatetime),
>>		WHERE activitykey = @activitykey
>>
>>
>>
>>Your C# DataAccess should check if the field is DBNull.Value and if so should not create a parameter. IOW, you never pass NULLs back to the Stored Procs.
>>
>>When I've said this to some people, they've countered with ... "but what if I want to reset the database column back to a NULL"? And I reply "NULL means nothing was ever entered. If a user has actually put some value in a field, saved it, and then later changed their mind, you should NOT put a NULL back into that column. It needs to default to something else that means nothing, but not a NULL".
>>
>>~~Bonnie
>>
>>
>>
>>
>>>I have the following piece of code:
>>>
>>>
>>>SqlParameter PartDesc = new SqlParameter();
>>>PartDesc.SqlDbType = SqlDbType.VarChar;
>>>PartDesc.ParameterName = "@Part_Description";
>>>PartDesc.Value = txtPartDesc.Text;
>>>
>>>
>>>If the user didn't type anything into the txtPartDesc field, how would ensure that NULL was passed to the stored procedure?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform