?
Solved

Reading SQL server Numeric data type into a C# variable

Posted on 2006-07-01
3
Medium Priority
?
902 Views
Last Modified: 2011-10-03
Hello,

I have:
- a Microsoft SQLServer database.
- a table named _Tab containing:
- 1 column, named Value, assigned a Numeric data type
- 1 row of data, 9998887777

I'd like to:
My main goal is to read that Numeric data into an integer or something that will NOT spit out a scientific notation.
I want to read it into an Int or something so that if i print that value to a screen after its read into a variable, it will
still be 9998887777.   Not 1+0E878748 or whatever.

I know how to set up the sqldatareader and how to loop through to read the data, what i do not know is how to cast the value properly so it comes in without errors or changes.

Thanks so much for the help.
0
Comment
Question by:RichardRiga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
aaronfeng earned 2000 total points
ID: 17025340
Try to use Int64 because the value 9998887777 is too big for Int32.

using (SqlConnection con = new SqlConnection("...")) {
      SqlCommand command = con.CreateCommand();
        SqlDataAdapter adaptor = new SqlDataAdapter(command);

      Int64 value = 0;
      command.CommandText = "SELECT * FROM Table_2";
      try {
            con.Open();
            value = Int64.Parse(command.ExecuteScalar().ToString());
      }
      finally {
            con.Close();
      }
}

The code above worked just fine with Int64.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17025355
Just for your information:

79,228,162,514,264,337,593,543,950,335 - Decimal Max
9,223,372,036,854,775,807 - Int64 Max
2,147,483,647 - Int32 Max

If the value comes from the database can be bigger than Int64, you can use decimal.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 1

Author Comment

by:RichardRiga
ID: 17026517
Thanks a ton, Aaron!
For the sample and the later comment.  Helped a lot.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question