简体   繁体   中英

Test SQL Parameter for null or blank value

I'm performing a simple insert into an SQL database. The query is parametrised but when I call the method which performs this insert I may not always want to populate each parameter.

As follows:

using (var sqlconnection = new SqlConnection(Globals.AFWideSettings.SqlConnectionString))
            {
                sqlconnection.Open();
                using (
                    var sqlcommand =
                        new SqlCommand(
                            "insert into DataActions (afuser, aftable, changetype, originaldata, newdata) values (@afuser, @aftable, @changetype, @originaldata, @newdata);",
                            sqlconnection))
                {

                    sqlcommand.Parameters.Add(new SqlParameter("afuser", userActions.AfUser));
                    sqlcommand.Parameters.Add(new SqlParameter("aftable", userActions.AfTable));
                    sqlcommand.Parameters.Add(new SqlParameter("changetype", userActions.ChangeType));
                    sqlcommand.Parameters.Add(new SqlParameter("originaldata", userActions.OriginalData));
                    sqlcommand.Parameters.Add(new SqlParameter("newdata", userActions.NewData));

                    sqlcommand.ExecuteNonQuery();
                }
                sqlconnection.Close();
            }
        }

If I was testing for Null information coming out of the database I would do something like:

Id = getRecords.IsDBNull(0) ? -1 : getRecords.GetInt32(0)

Is there a equivalent way of doing this on sqlparameters ? I'm stumped.

(I know I could test each item individually, I just want to be efficient)

Many thanks

Well, you have to populate each parameter because it's in your SQL (unless you want to dynamically remove fields/values which would be messy).

I would just use the NULL coalesce operator (??) when setting the parameter values:

sqlcommand.Parameters.Add(
    new SqlParameter("afuser", userActions.AfUser ?? {insert default value here} ));

Okay, so the basic syntax is as follows:

CREATE PROCEDURE NameOfProcedure
/*
I like to put modification history, purpose for the procedure and stuff like that in here.
It's not required though
*/
(
@afuser NVARCHAR(255) DEFAULT NULL
,@aftable INT DEFAULT NULL

Do this for all your variables, I've shown an example of an integer field and an nvarchar field but use whatever the datatype is. The DEFAULT NULL bit is the part that says if no value is specified, use NULL.

)
AS
BEGIN

INSERT INTO DataActions (afuser, aftable, changetype, originaldata, newdata)
VALUES (@afuser, @aftable, @changetype, @originaldata, @newdata)

END

you could do it using the DBNull value

I would recommend using Parameters.AddWithValue() Method for example see the code below

sqlcommand.Parameters.AddWithValue("@afuser", userActions.AfUser ?? (object)DBNull.Value);

MSDN SqlParameterCollection.AddWithValue Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM