Solved

Reading SQL server Numeric data type into a C# variable

Posted on 2006-07-01
3
889 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
How to LOAD up a .NET Framework website with data? 2 42
Excel file not created as expected 7 51
Server Error 11 48
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

27 Experts available now in Live!

Get 1:1 Help Now