[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 908
  • Last Modified:

Reading SQL server Numeric data type into a C# variable

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
RichardRiga
Asked:
RichardRiga
  • 2
1 Solution
 
aaronfengCommented:
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
 
aaronfengCommented:
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
 
RichardRigaAuthor Commented:
Thanks a ton, Aaron!
For the sample and the later comment.  Helped a lot.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now