1

我曾经ExecuteReader为这样的所有字段选择所有(SELECT *)

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())
            { ... }

但我只想在特定的列和单元格中选择,如红色方块中.. 像这张图片我只想在特定的单元格和列中选择,如红色方块

4

4 回答 4

3

while您可以获取子句中的特定列。

while (dataReader.Read())
{ 
    var _column = dataReader["Nama_Kategori"];
}
于 2013-04-09T14:15:49.973 回答
1

考虑使用

        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();
             }
        }

或使用dataReader["columnName"]

于 2013-04-09T14:18:47.987 回答
1

您可以使用MySqlCommand 方法的ExecuteScalar()来检索单个值

MySqlCommand myCommand = new MySqlCommand("SELECT Nama_Kategori FROM tb_patient_information WHERE Id_kategori = 'KI-02'", myConnection);
myCommand.Connection.Open();
myCommand.ExecuteScalar();
myConnection.Close();
于 2013-04-09T14:21:24.827 回答
0

SQL 查询

如果您只想要第三行数据,请尝试以下查询:

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

-此查询返回结果集的第 3 行

在数据读取器中

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

或者 如果您说我只使用Select * from tb_patient_information并且我需要第三行结果 然后尝试如下

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

            if(count == 3)
            {
             string Id = dataReader["Id_kategori"].ToString();
             string Name = dataReader["Nama_Kategori"].ToString();
            }  
           count ++;  
          }  
于 2013-04-09T14:17:30.840 回答