简体   繁体   English

使用MySQL数据阅读器

[英]Using MySQL Data Reader

I'm not familiar with using Data Reader, i need help with the following code, i want to retrieve a single data from the database. 我不熟悉使用Data Reader,我需要以下代码的帮助,我想从数据库中检索单个数据。

MySqlDataAdapter data = new MySqlDataAdapter(cmd);
                    conn.Open();
                    DataTable dt = new DataTable();
                    data.Fill(dt);
                    gridView1.DataSource = dt;

                    int retrievedValue = 0;
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if ((int)reader["order_status"] == 0)
                            {
                                retrievedValue = (int)reader.GetValue(0);

                                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Green;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                            }
                        }
                    }

reader["order_status"] returns object , since you told it is an already integer, you need to cast it to int first. reader["order_status"]返回object ,因为你告诉它已经是整数,你需要先将它reader["order_status"]int

You need to use == operator as well since it is a equality operator. 你需要使用==运算符 ,因为它是一个相等运算符。 = operator is an assignment operator. =运算符是赋值运算符。

if ((int)reader["order_status"] == 0)

Or you can use GetInt32 method with it's zero-based column number. 或者您可以使用GetInt32方法及其基于零的列号。 Let's say it's the first column that your query returns, you can use it like; 假设它是您的查询返回的第一列,您可以像使用它一样使用它;

if(reader.GetInt32(0) == 0)

By the way, if you wanna get only single value, I strongly suspect you may wanna use ExecuteScalar method since it get's the first column of the first row. 顺便说一下,如果你只想得到单个值,我强烈怀疑你可能想要使用ExecuteScalar方法,因为它是第一行的第一列。 Then you can structure your query as SELECT order_status FROM ... etc.. 然后,您可以将查询结构化为SELECT order_status FROM ...等。

Be sure to assign a variable before while (reader.Read()) otherwise it will and error. 确保在while (reader.Read())之前分配一个变量,否则它将会出错。 Then close the data reader once you are finished using it. 完成使用后,请关闭数据阅读器。 Like so: 像这样:

 using (MySqlDataReader reader = cmd.ExecuteReader())    
    {

    int retrievedValue = 0;

          while (reader.Read())
          {
                retrievedValue = (int)reader.GetValue(0);
                if (retrievedValue == 0)
                {

                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Green;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                }
                else if (retrievedValue == 1)
                {

                    GridView View2 = sender as GridView;
                                    e.Appearance.BackColor = Color.Red;
                                    e.Appearance.BackColor2 = Color.ForestGreen;
                }
          }//and so on...
          reader.Close();
    }

I hope this is was you're looking for. 我希望这是你正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM