Solved

Reading SQL server Numeric data type into a C# variable

Posted on 2006-07-01
3
888 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a ton, Aaron!
For the sample and the later comment.  Helped a lot.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Build JSON from table records 17 38
optimize  c# code 7 48
Default parameter problem in C# 3 25
Get String split 5 31
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now