473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

int, int16 (short) and null value

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!
Nov 17 '05 #1
1 3925
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;
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve - DND | last post by:
After downloading and using two different memory profiling applications today, it seems that sbyte, int16, and int32 are all stored using 12b of memory. Someone please tell me I'm reading these...
0
by: Pat Ireland | last post by:
To use the InvokeMember for calling a method in a dynamically loaded class requires that any arguments to the method must be past in an object . However, when I try to perform a pass by reference...
3
by: QuocSi | last post by:
Hi all, I have a bitsfield Structure MyBits a as boolean b as boolean c as boolean d as boolean end structure
2
by: Nathan Baulch | last post by:
For some reason I keep getting an InvalidProgramException when trying to emit a short (Int16) value in a DynamicMethod. The following code should replicate my problem. private delegate object...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.