问题
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 picture
回答1:
You can get the specific column inside the while
clause.
while (dataReader.Read())
{
var _column = dataReader["Nama_Kategori"];
}
回答2:
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"]
回答3:
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();
回答4:
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 ++;
}
来源:https://stackoverflow.com/questions/15904333/how-to-read-specific-column-and-cell-in-mysql-in-c