MySQL Forums
Forum List  »  MySQL for Visual Studio

Re: Read using ExecuteScalar
Posted by: Luis Penunuri
Date: March 08, 2016 05:28PM

Hi Hunnar,

Thank you for using MySql as your database provider.

How are you storing the result of the ExecuteScalar command ?

Consider the following scenario:
1.- A database called "test", with a table called "people", having a column called "Salary", which is of type "VARCHAR(45)", and storing the values: "9.25", "28.5", ...

2.- Having the following code:
Dim conn As MySql.Data.MySqlClient.MySqlConnection
Dim cmd As MySql.Data.MySqlClient.MySqlCommand
Dim res As Integer
conn = New MySql.Data.MySqlClient.MySqlConnection("Server=my_server;Database=test;Uid=user_id;Pwd=user_password;")
conn.Open()
cmd = New MySql.Data.MySqlClient.MySqlCommand("SELECT Salary FROM people WHERE Age=20")
cmd.Connection = conn
res = cmd.ExecuteScalar()
Console.WriteLine(res)
Console.ReadLine()

The result you will see in the output is 9, because the datatype holding the ExecuteScalar command is Integer. If you change it to string or decimal, you should be able to see the correct value "9.25".

Please let me know if this scenario describes what you are currently getting.

Thank you, and have a good one !

Options: ReplyQuote


Subject
Views
Written By
Posted
946
March 01, 2016 08:23AM
Re: Read using ExecuteScalar
481
March 08, 2016 05:28PM
486
March 09, 2016 12:03AM
459
March 09, 2016 10:54AM
438
March 10, 2016 12:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.