SQL CE e l’Arithmetic Overflow

L’esigenza del cliente consiste in un’aplicazione che, utilizzando SQL CE, permetta la memorizzazione di una serie di informazioni; una di queste è un valore numerico che potra variare da 0 a 99,9 con precisione massima ad un numero dopo la virgola.

Perfetto, creo un DB SQL CE contente la tabella “test” con, fra gli altri, il campo “colonna” di tipo numeric (3,1).

   1: create table test 
   2: (
   3: Id int primary key identity(1,1),
   4: ...
   5: colonna numeric (3,1)
   6: )

Scrivo anche un test che eseguendo il codice:

   1: using (var connection = new SqlCeConnection("Data Source=… "))
   2: {
   3:     connection.Open();
   4:     SqlCeCommand command = connection.CreateCommand();
   5:     command.CommandText = "INSERT INTO Test (Colonna) VALUES (@p0)";
   6:     command.Parameters.Add(new SqlCeParameter("p0", 100));
   7:     command.ExecuteNonQuery();
   8: }

si aspetta un’eccezione.

Perfetto, eseguendo il test l’eccezione viene sollevata ed il test passa.

In realtà l’applicazione non esegue direttamente le istruzioni SQL, ma scrive e legge le informazioni dal DB utilizzando una entity mappata con NHibernate, tale entity espone la property “colonna” di tipo double.

Ovviamente anche questa parte di codice è coperta da alcuni test che ne verifichino il corretto funzionamento.

In particolare scriviamo anche un test che istanziando la entity e valorizzando la property colonna con il valore 100 si aspetta un’eccezione al momento della Insert nel DB.

Ricreo la tabella ed eseguo il test.

Qui esiste nasce il problema: l’eccezione NON viene sollevata!

Ma come? Il campo non era un numeric (3,1)?

A casa mia 100 è maggiore di 99,9!

Come prima azione, dopo aver eseguito il test che inserisce il valore 100 senza aver sollevato l’eccezione, mi collego al db Sql CE con la management studio ed eseguo l’istruzione

   1:  
   2: Select count(*) from test

Risultato = “1”.

Ma allora il record è entrato!

Eseguo l’istruzione

   1: Select Id from test

Risultato = “1”.

Allora eseguo l’istruzione

   1: Select colonna from test

Risultato = “Major Error 0x80004005, Minor Error 0 Arithmetic Overflow”.

Dopo una decina di secondi conditi da una sequenza di “brutti porchi” eseguo l’istruzione

   1: Select cast([COLONNA] as numeric(4,1)) from test

Risultato = “100”.

Colto da un dubbio amletico vado a controllare le proprietà del campo colonna e vedo: “numeric (3,1)

Premesso che ci sono mille soluzioni per aggirare il problema vorrei capire perchè esiste questo comportamento!

Alcune premesse:

La versione del mapping di NHibernate utilizzata è la seguente

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >

la property è semplicemente mappata così:

<property name="Colonna"/>

Il dialetto è quello per Sql CE:

<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>

Ora vorrei rispondere alla domanda: “Perchè inserendo il valore 100 con un’istruzione sql ottengo (giustamente) l’eccezione mentre con NHibernate no?”

Scaricando il codice di NHibernate (http://sourceforge.net/projects/nhibernate/ ) e modificando la reference del mio progetto agganciandomi così al codice posso facilmente debuggare quello che avviene al “Save” del mio repository.

Dietro le quinte Nhibernate non fa “magie”, alla fine dei suoi ragionamenti genera una SqlCeConnection ed un SqlCeCommand e ne esegue il metodo ExecuteNonQuery.

L’unica particolarità consiste nel SqlDbType utilizzato da Nhibernate, se la property della mia entity è definita come double Nhibernate forza il SqlDbType del parametro a “Float” e non “Decimal”.

Perfetto: abbiamo risposto alla domanda, ci basta mappare la property così:

<property name="Colonna" type="Decimal" />

ed NHibernate utilizzando il SqlDbType corretto genererà la giusta eccezione.

Ora però mi sorge un’altra domanda: io posso anche accettare che mi si imponga quale SqlDbType utilizzare ma se il campo è numeric (3,1) perchè SQL CE si è memorizzato il valore 100?

A questa domanda non ho ancora saputo rispondere... idee?

Riccardo.

posted @ mercoledì 9 settembre 2009 18:15

Print

Comments on this entry:

# re: SQL CE e l’Arithmetic Overflow

Left by Dissertation Writing at 27/09/2010 19:04
Gravatar
Truly impressive post about "SQL CE e l’Arithmetic Overflow". Custom Dissertation Writing | Dissertation Writing

# re: SQL CE e l’Arithmetic Overflow

Left by SEO at 12/10/2012 14:29
Gravatar
entity espone la property “colonna” di tipo double.

# re: SQL CE e l’Arithmetic Overflow

Left by custom dissertation writing serv at 29/08/2016 18:19
Gravatar
Writing an Essay is an Art which consists of following steps Pick a topic, Preparing outline, Introduction, and Conclusion. Find the best custom dissertation writing service here.

# re: SQL CE e l’Arithmetic Overflow

Left by Habeeb at 01/09/2016 14:49
Gravatar
I just read the same query on another blog too.
The customer's requirement consists in that un'aplicazione , using SQL CE , allowing the storage of a series of information.
assignment writing services by queensland-assignment

# re: SQL CE e l’Arithmetic Overflow

Left by Jason Loy at 08/12/2016 15:54
Gravatar
Great post. I don't think any app gets better than this gem.
http://xmtvplayerdownloaded.org/
http://showbox-windows.com/
http://tubemateapkdownload.co/

# re: SQL CE e l’Arithmetic Overflow

Left by psn codes list at 26/04/2017 14:40
Gravatar
I look over right here plenty of ideas of interesting individuals. Thanks73B4

# re: SQL CE e l’Arithmetic Overflow

Left by naina at 01/04/2018 15:39
Gravatar
This is also a very good post which I really enjoyed reading.
facebook video downloader

# re: SQL CE e l’Arithmetic Overflow

Left by homedepot survey at 04/06/2018 19:39
Gravatar
The Home Depot or Home Depot is an American home improvement supplies retailing company that sells tools, construction products, and services.

homedepot.com/survey |
home depot survey

# Download Showbox APK for Android

Left by ShowNuh at 18/11/2018 17:03
Gravatar
Looking for the best app to watch free movies on your iOS phone? Then your should check Showbox Application. This is the most famous app today that has a big library of shows and movies. This app is also available for Windows users. But your need to download it first to enjoy free tv-shows [url=http://showbox-all.com]Download Showbox APP for Windows[/url]

# Download Showbox APP for Android

Left by ShowNuh at 18/11/2018 17:18
Gravatar
Looking for the best app to watch free movies on your iOS phone? Then your should check Showbox app. This is the most famous app today that has a big library of tv shows and films. This app is also available for iMac users. But your need to download it first to enjoy free shows [url=http://showbox-all.com]Download Showbox APP for Windows[/url]

# re: SQL CE e l’Arithmetic Overflow

Left by showbox at 22/11/2018 15:00
Gravatar
Showbox download:
http://showbox-all.com
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011