I'm using System.Data.SQLite to save data to database. In table there are 2 columns defined as float. However when I save data to DB it saves only integer part.
12.2345 => 12
11.5324 => 11
(I don't know how exactly it rounds, it just rounds)
Yes I'm sure I'm using floats in my application, and I'm sure that CommandText contains float numbers NOT integer.
You need to be using floats in your application, but you also need to make sure the table entry in the SQLite database has been explicitly declared to be that REAL type.
There is no support for floats as build-in type. Use TEXT instead.
Related
I have a MSSQL table where I have records which contains amount of cryptocurrencies in their smallest units (BTC-Satoshi, ETH-Wei...) This amounts can be pretty big (1 ETH is 1,000,000,000,000,000,000 Wei) so I wanted to store them in a NUMERIC(38,0) field type.
Then I have my client application where I am using Entity Framework Core (6), and in my entity class I have amount declared as a BigInteger. The problem is that I don't know how to do the mapping. I can use ValueConverter<BigInteger, decimal> but I (of course) get an error (overflow) when the number in the database is larger than the range of the Decimal type.
Are there any other options or what are the best practices? The other problem is that I would also want to use aggregate functions (SUM) on this amounts, but I'm not sure if this is possible using BigInteger and EF Core). I would also probably have problems with plain ADO and usage of SqlDataReader.GetDecimal (since I will get the same overflow error). Any advices?
I (of course) get an error (overflow) when the number in the database is larger than the range of the Decimal type.
If you need numbers larger than a .NET Decimal, you'll have to convert them to strings in SQL Server to read them on the client.
David
I am trying to insert double values to my database via EF 5. I generated EF entity model from db. There is a price column in the table which is float, and naturally EF generated a double type for the mapper class.
I read some string values from a file and convert it to double and save it to db. When I debug I can see that values are converted correctly. For example string value "120,53" is converted to double like 120.53, just fine. But when I save my context it goes to db like "12053".
What can cause such a problem? Is there any setting in SQL Server has anything to do with this?
I believe the problem is in your Convert.ToDouble and the , being used instead of a decimal. Without supplying the culture for the number it's likely instead interpreting the , as a thousandths separator and ignoring it. Try passing in CultureInfo.CurrentCulture as the second argument (IFormatProvider) if the culture of your environment is one where the , is used as a decimal. Or, since it looks like you're replacing a decimal with the , - just don't replace it.
When I am Entering a double number from C# to the DB I use a real type in the DB. When I check the value it is exactly what I enter.
Now when I go to retreive the value from the DB and store it into a C# double, it adds random decimal values at then end. The number in the database is the correct value that I want, but the value in C# as a double is just random (sometimes higher sometimes lower then the actual value.)
ie
- Enter into the db 123.43 as a double to an sql real.
- View the value in the DB, it's exactly 123.43
- Get the value from the DB and store into a C# double, value in the double is now 123.4300000305176
I have tried changing the type to float, decimal, etc. in the DB but these types actually alter the value when I put it into the DB to the same format as the above.
Any help on whats going on or how to fix it?
You should probably be using Decimal type. See What represents a double in sql server? for further explanation.
Try using a Single if your DB type is real. Check here for data mappings http://msdn.microsoft.com/en-us/library/ms131092.aspx
I am trying to read Oracle Spatial data with C# using ODP.NET.
In some cases, my Oracle Spatial data has Number values in the SDO_GEOMETRY’s OrdinateArray that are too big for .NET to handle. So, when I try to read the SDO_GEOMETRY values, it throws a “System.OverflowException: Arithmetic operation resulted in an overflow”. In my case, the ordinate values just have too many digits after the decimal point, and I don’t care about losing this information.
My code is based on the sample app here: http://www.orafaq.com/forum/mv/msg/27794/296419/0/#msg_296419
I see there are SafeMapping approaches with DataSets to read Number types that won’t fit into Decimal types, but I don’t see how to apply this to an internal part of the SDO_GEOMETRY type.
Is there a way around this problem?
What is the Oracle Datatype of "OrdinateArray"? If it is a user defined type (eg VARRAY), you can create a custom .NET class to accept the data. For more information on this, read up on "User Defined Types"
It's probably too late for you but maybe someone could use my solution to the problem.
I run into this problem while making custom shapefile to Oracle Locator importer in C#.
What I did is I changed ordinatesArray variable type from decimal[] to double[] in SdoGeometry class. Same change (decimal to double)was needed for
public class OrdinatesArrayFactory : OracleArrayTypeFactoryBase {}
and
OrdinatesArray = GetValue((int)OracleObjectColumns.SDO_ORDINATES) in MapToCustomObject method.
In fact the code was working OK with decimal type when I imported data using oracle.spatial.util.SampleShapefileToJGeomFeature tool.
Problems started when I imported data using my tool (shapefile geometry to WKB and then insert to Oracle using
INSERT INTO some_table (GEOM) VALUES (SDO_UTIL.FROM_WKBGEOMETRY())
For some reason ordinates where too big for decimal although I handled precision.
The scenario is that I want to encrypt finance numbers in a column with a data type of int in a sql server table.
It is a big app so it is difficult to change the table column data type from int to any other data type.
I'm using sql server 2005 and asp.net C#.
Is there a two-way encryption method for a column with a data type of int?
Could I use a user-defined-function in sql server 2005 or a possibly a C# method?
I'm sorry but I simply can't see the rationale for encrypting numbers in a database. If you want to protect the data from prying eyes, surely SQL Server has security built into it, yes?
In that case, protect the database with its standard security. If not, get a better DBMS (though I'd be surprised if this were necessary).
If you have bits of information from that table that you want to make available (like some columns but not others), use a view, or a trigger to update another table (less secured), or a periodic transfer to that table.
XOR?
:)
Hmm, need more text...
There are a few two way encryption schemes available in .Net.
Simple insecure two-way "obfuscation" for C#
You can either convert the integer to it's byte array equivalent or convert it to a base-64 string and encrypt that.
Well, every injective, surjective function from int to int can be used as a way to "encode" an integer.
You could build such a function by creating a random array with 65536 items with no duplicate entries und using f(i) = a[i]. To "decode" your int you simply create another array with b[i] = x | a[x] = i.
As the others have mentioned, this may not be what you REALLY want to do. =)
Edit: Check out Jim Dennis' comment!
You might want to look at format preserving encryption.