Reading money Type Field in DB

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?
 
S

Steve Willcock

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.
 
G

Ghost

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?
 
S

Steve Willcock

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top