How to make a mysql query from c# and save it in a variable?

Susana Esparza

I want my c # program to take the data from the mysql database and save that data to a variable type double. I was trying but I still can't do it.

`

MySqlDataReader read = null;
            
string selectCmd = "SELECT Saldo from cuentas where NoCuenta=" + cuenta + ";";
            
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
            
MySqlCommand MyCommand2 = new MySqlCommand(selectCmd, MyConn2);
            
MyConn2.Open();
            
read = MyCommand2.ExecuteReader();
            
MessageBox.Show("read: " +read);
            
bool saldoI = read.Read();
            
double saldo = Convert.ToDouble(saldoI);
           
 MessageBox.Show("Aqui se muestra el saldo inicial " + read);
            
double saldoFinal = saldo - monto;

`

I know that as I am transforming it from a bool type variable it returns a 1, but I require that it return the data type double. Example: 300.2

Athanasios Kataras

Why not:

if (read.Read())
    double saldo = Convert.ToDouble(reader[0]);

The read.Read() return value, returns true if there is indeed a returned row from your query and false if there isn't or you read them all.

To actually get the data you need to access the current row's column in the data reader.

Example in the link above:

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to input value from user and save in variable wxDev-C++

분류에서Dev

Any ideas on how to save location point to MySQL variable

분류에서Dev

Why wont this MySQL Query save

분류에서Dev

MySQL stored procedure return variable values from seconds query?

분류에서Dev

How to resolve the id from MySQL DB into a variable

분류에서Dev

Save variable as variable in MYSQL for later use

분류에서Dev

MySQL - Help me to make a query

분류에서Dev

How to query from Mysql DATETIME column based on day name

분류에서Dev

How to query from Mysql DATETIME column based on day name

분류에서Dev

How to make a symlink from /A to /B, from running in /C

분류에서Dev

Java to MySQL: How to update row with a value from variable?

분류에서Dev

save variable to interactive namespace from python debugger

분류에서Dev

How to make two query to redis from node.js using promise from when.js

분류에서Dev

How to save results of a SQL query to html file?

분류에서Dev

Using javascript variable in mysql query and populate listview

분류에서Dev

c++ How to make multidimentional vector from a table in a text file?

분류에서Dev

How to put result of query in a variable?

분류에서Dev

Haystack: Make query from one project to another

분류에서Dev

Is there a way to get directory part from variable in make?

분류에서Dev

MySQL query, how to optimize it better

분류에서Dev

How to pass & symbol in MySQL query?

분류에서Dev

Query in using "char*" variable in c++

분류에서Dev

Download image from url using request and save to variable

분류에서Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

분류에서Dev

how to get data from cache memory without executing mysql db query

분류에서Dev

How do I make variable in for loop bigger that in was

분류에서Dev

How to make PATH variable exports on MacOSX persistent?

분류에서Dev

How to make bash glob a string variable?

분류에서Dev

How to make a variable plus a random number?

Related 관련 기사

  1. 1

    How to input value from user and save in variable wxDev-C++

  2. 2

    Any ideas on how to save location point to MySQL variable

  3. 3

    Why wont this MySQL Query save

  4. 4

    MySQL stored procedure return variable values from seconds query?

  5. 5

    How to resolve the id from MySQL DB into a variable

  6. 6

    Save variable as variable in MYSQL for later use

  7. 7

    MySQL - Help me to make a query

  8. 8

    How to query from Mysql DATETIME column based on day name

  9. 9

    How to query from Mysql DATETIME column based on day name

  10. 10

    How to make a symlink from /A to /B, from running in /C

  11. 11

    Java to MySQL: How to update row with a value from variable?

  12. 12

    save variable to interactive namespace from python debugger

  13. 13

    How to make two query to redis from node.js using promise from when.js

  14. 14

    How to save results of a SQL query to html file?

  15. 15

    Using javascript variable in mysql query and populate listview

  16. 16

    c++ How to make multidimentional vector from a table in a text file?

  17. 17

    How to put result of query in a variable?

  18. 18

    Haystack: Make query from one project to another

  19. 19

    Is there a way to get directory part from variable in make?

  20. 20

    MySQL query, how to optimize it better

  21. 21

    How to pass & symbol in MySQL query?

  22. 22

    Query in using "char*" variable in c++

  23. 23

    Download image from url using request and save to variable

  24. 24

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  25. 25

    how to get data from cache memory without executing mysql db query

  26. 26

    How do I make variable in for loop bigger that in was

  27. 27

    How to make PATH variable exports on MacOSX persistent?

  28. 28

    How to make bash glob a string variable?

  29. 29

    How to make a variable plus a random number?

뜨겁다태그

보관