Craig

Craig

  • NA
  • 20
  • 0

How do you insert null values into SQL TABLE? Can I use DBnull.value to do this?

Jun 4 2007 10:18 AM

EX:

If I had a table (table1):
ID: text
JAN: int (null values allowed)
FEB: int (null values allowed)
MAR: int (null values allowed)


and I wanted to insert the following record into the table

record 1:

idcode: "tim"
janvalue: missing
febvalue: 2
marvalue: 5

C# code

string idcode;
idcode="tim";
int janvalue;
janvalue = ?;
int febvalue;
febvalue=2;
int marvalue;
marvalue=5;

string sqlparam=@"insert into table1
(id,
jan,
feb,
mar)
values
(@ idcode,
@janvalue,
@febvalue,
@marvalue)";

 

SqlCommand cmd = new SqlCommand(sqlparam, conn);
cmd.Parameters.Add("@idcode", SqlDbType.VarChar, 11);
cmd.Parameters.Add("@janvalue", SqlDbType.Int);
cmd.Parameters.Add("@febvalue", SqlDbType.Int);
cmd.Parameters.Add("@marvalue", SqlDbType.Int);
try{
conn.Open();
cmd.Parameters["@blscode"].Value = blscode;
cmd.Parameters["@janvalue"].Value = janvalue;
cmd.Parameters["@febvalue"].Value = febvalue;
cmd.Parameters["@marvalue"].Value = marvalue;
}
..........


 


Answers (1)