Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting .NET Double.MinValue into an Access "Double" field

Tags:

.net

ms-access

I am trying to save some double values from my .NET project to MS Access using adodb. If I have uninitialized values I want to save Double.MinValue as a default value into my access db.

The problem is that the range differs in .NET and in Access. For .NET for example the minvalue is

-1.79769313486232e308

and for MS Access the minvalue is

-1.79769313486231E308

can anybody tell me why there is a difference because both are using 8 bytes ... or is there any workaround for my problem. I want to make sure that my users are able to use the full range of Double. Because they won't know that they are "not allowed" to use Double.MinValue ...

Thanks for help in advance!

like image 487
Silvio Marcovic Avatar asked Dec 18 '13 13:12

Silvio Marcovic


2 Answers

okay I checked it again and now I have to realize that I am at fault here. The numbers posted above are wrong. For both its the value

-1.7976931348623157e308. 

When I asked the question I viewed an untrustworthy source. The problem is that upon converting Double.MinValue to a string the resulting string is "-1.79769313486232e308" and converting it back to double results in an OverflowException.

You can test this with following snippet

double dblVal = Double.MinValue;
string strVal = dblVal.ToString();
dblVal = Convert.ToDouble(strVal);

Hence I am sending an sql query in order to store the value in access I am doing this transformation..

Next time I should check my sources better (: It seems impossible to send Double.MinValue via sql statement to access. I did a workaround in my AccessInterface.

like image 163
Silvio Marcovic Avatar answered Oct 12 '22 06:10

Silvio Marcovic


(Note that this original answer was based on the premise of the question that there WAS a discrepancy between the minimum Double values available in .NET and Access. As stated in the other answer from the question owner, that is actually not true.)

My guess would be that the discrepancy is simply an obscure implementation detail. The VBA/VB6 code base is much older than the .NET code base and the people who built VBA/VB6 may have either

  • hit a limitation of the environment (at the time) that prevented them from squeezing that last 0.00000000000001 of precision into the range, or
  • had to make a compromise (e.g., for efficiency in computation, storage, or whatever) and the "price" of that compromise was that the Double range was reduced by that miniscule amount.

Software design sometimes involves trade-offs, so perhaps that last 0.00000000000001 was sacrificed for some other benefit. The true answer may be lost in the mists of time.

Edit re: statement in other answer

It seems impossible to send Double.MinValue via sql statement to access.

Although it may not be possible to create a SQL statement with a string literal containing Double.MinValue by simply invoking .ToString() on it, this code definitely does work (tested with an Access 2010 database):

using (var cmd = new OleDbCommand())
{
    cmd.Connection = con;
    cmd.CommandText = "INSERT INTO DoubleTest (DoubleField) VALUES (?)";
    cmd.Parameters.AddWithValue("?", Double.MinValue);
    cmd.ExecuteNonQuery();
}
like image 38
Gord Thompson Avatar answered Oct 12 '22 07:10

Gord Thompson