int, int16 (short) and null value

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

Inside my database I need to add a short value.
Inside my code I use int (int32), to convert the int to int16... is this the
good/correct way?
int myvar = 12;
To database... (int16)myvar


For a function I need to add an int value (this one will be added inside the
database).
Now I don't have an int value.
I can add an -1 value but maybe there are other options. I tried null-value,
but this does not work.
Are there other options?

Thanks!
 
Hi Arjen,

Take a look at SqlParameter/OleDbParameter they hugely simplify dealing with database values.

using(SqlConnection conn = new SqlConnection(@"integrated security=SSPI;
data source=DATASERVER;initial catalog=TestDB"))
{
string commandString = "INSERT INTO TestTable (ShortColumn) VALUES (@myParam)";

SqlParameter myParam = new SqlParameter("@myParam", SqlDbType.SmallInt);
SqlCommand comm = new SqlCommand(commandString, conn);
comm.Parameters.Add(myParam);

// insert value here
myParam.Value = myValue;

conn.Open();
int result = comm.ExecuteNonQuery();
conn.Close();
}

myValue can be any numeric type you like and the SqlParameter will perform any necessary actions to create a database smallint out of it.

To insert a null value into the database set

myParam.Value = DBNull.Value;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top