Reading money Type Field in DB

  • Thread starter Thread starter Ghost
  • Start date Start date
G

Ghost

Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type
variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?
 
Use the C# decimal datatype rather than float - this is compatible with the
Sql Server money datatype.

One other point is that to get the best performance out of the sqldatareader
you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.
 
How about this problem:
The same problem I have when I'm try to assign record value to "char" type
variable.
myCharVar = (char)mySqlDataReader["SomeField"];

Steve Willcock said:
Use the C# decimal datatype rather than float - this is compatible with the
Sql Server money datatype.

One other point is that to get the best performance out of the sqldatareader
you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.

--
Steve Willcock, MCSD
http://www.willcockconsulting.com/


Ghost said:
Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type
variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?
 
That depends what datatype "SomeField" is in the here - if it's a char or
varchar Sql Server datatype then use the C# string datatype as follows:

string s = (string)mySqlDataReader["SomeField"];
or
string s = mySqlDataReader.GetString(2);

Steve

Ghost said:
How about this problem:
The same problem I have when I'm try to assign record value to "char" type
variable.
myCharVar = (char)mySqlDataReader["SomeField"];

Use the C# decimal datatype rather than float - this is compatible with the
Sql Server money datatype.

One other point is that to get the best performance out of the sqldatareader
you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.

--
Steve Willcock, MCSD
http://www.willcockconsulting.com/


Ghost said:
Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type
variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?
 
Back
Top