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
Solved

Reading SQL server Numeric data type into a C# variable

Posted on 2006-07-01
3
892 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
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
aaronfeng earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 41
Reference issues in Visual Studio 3 41
Printing 1 58
Code works but breaks when I add one section 4 20
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…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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