1

I use ExecuteReader to select all (SELECT*) for all field like this

string query = "SELECT* FROM tb_patient_information ";
        if (this.OpenConnection() == true)
        { //Create Command
            MySqlCommand cmd = new MySqlCommand(query, connection);
            //Create a data reader and Execute the command
            MySqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            { ... }

but I only want to select in specific column and cell like in red square.. like this pictureI only want to select in specific cell&column like in red square

Prasetyo Jean
  • 57
  • 2
  • 4
  • 10
  • Why not use mysql to achieve the column part? SELECT [ColumnName] FROM tb_patient_information – Ash Apr 09 '13 at 14:17
  • 3
    Suggestion: Never use * to select in your programs, specify the column names specifically. This way your program won't crash or have unexpected functionality if the table changes (and you don't have to remember to change several things). – David S. Apr 09 '13 at 14:17
  • but I only want to select in specific column and cell like in red square – Prasetyo Jean Apr 09 '13 at 14:51

4 Answers4

3

You can get the specific column inside the while clause.

while (dataReader.Read())
{ 
    var _column = dataReader["Nama_Kategori"];
}
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

Consider using

        string query = "SELECT column FROM tb_patient_information ";
        if (this.OpenConnection() == true)
        { 
            //Create Command
            MySqlCommand cmd = new MySqlCommand(query, connection);
            //Create a data reader and Execute the command
            MySqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            { 
                dataReader.ExecuteScalar();
             }
        }

or use dataReader["columnName"]

Ali
  • 1,409
  • 13
  • 23
1

You can use ExecuteScalar() of MySqlCommand method to retieve single value

MySqlCommand myCommand = new MySqlCommand("SELECT Nama_Kategori FROM tb_patient_information WHERE Id_kategori = 'KI-02'", myConnection);
myCommand.Connection.Open();
myCommand.ExecuteScalar();
myConnection.Close();
Alex
  • 8,827
  • 3
  • 42
  • 58
0

SQL Query

If you want only third row data then try the below query :

Select * from (Select row_number() over (order by subssn) as rownum, * FROM 
tb_patient_information)result Where rownum = 3

-This Query return the 3rd row on Result Set

In DataReader

while (dataReader.Read())
{ 
string Id = dataReader["Id_kategori"].ToString();
string Name = dataReader["Nama_Kategori"].ToString();
}

OR IF You Say I only use Select * from tb_patient_information and i need 3rd row result Then try like below

         int count=1;  
         while (dataReader.Read())  
          {  

            if(count == 3)
            {
             string Id = dataReader["Id_kategori"].ToString();
             string Name = dataReader["Nama_Kategori"].ToString();
            }  
           count ++;  
          }  
Pandian
  • 8,848
  • 2
  • 23
  • 33