Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从类中获取价值_C#_Mysql_Sql_Select_Connect - Fatal编程技术网

C# 从类中获取价值

C# 从类中获取价值,c#,mysql,sql,select,connect,C#,Mysql,Sql,Select,Connect,因此,我有一个类Take,用于连接mysql。在这个类中,我有一个方法来调用一个查询,从mysql表中获取最后一条记录 public void Balance() { string query = "SELECT balance FROM history ORDER BY id DESC LIMIT 1 "; if (this.OpenConnection() == true) { MySqlCommand

因此,我有一个类
Take
,用于连接mysql。在这个类中,我有一个方法来调用一个查询,从mysql表中获取最后一条记录

    public void Balance()
    {
        string query = "SELECT balance FROM history ORDER BY id DESC LIMIT 1 ";
        if (this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
        }          
    }
在主窗体中,我调用该类和该方法

    take.Balance(); 

我知道,从上面的代码来看,我没有任何价值,但是<代码> null <代码>,所以我问我如何从这个查询中取值,并把它放在主代码中的<代码>文本框< /代码>?

有两件事要考虑。首先是查询

"SELECT balance FROM history ORDER BY id DESC LIMIT 1"

是一种查询,从某种意义上说,应该返回数据库中的一些有用数据,但不应该使用方法
ExecuteNonQuery
执行该查询,该方法旨在返回受非查询语句影响的行数。第二,返回类型<代码>余额< /代码>必须更改为其他类型,而不是<代码>空白>代码>,例如“代码> INT/CODE >或类似的东西,必须返回给调用方。

有两件事要考虑。首先是查询

"SELECT balance FROM history ORDER BY id DESC LIMIT 1"

是一种查询,从某种意义上说,应该返回数据库中的一些有用数据,但不应该使用方法
ExecuteNonQuery
执行该查询,该方法旨在返回受非查询语句影响的行数。其次,
Balance
的返回类型必须更改为
void
以外的其他类型,例如
int
或类似的类型,这些类型必须返回给调用方。

我个人认为,您应该提高编程的基本知识。示例代码中有两个大问题:

  • 你想得到这个值,但是你的函数是空的,不返回任何东西,甚至把这个值设置成某个变量
  • ExecuteOnQuery不是您的情况
  • 例如:

     public string Balance()
        {
            string query = "SELECT balance FROM history ORDER BY id DESC LIMIT 1 ";
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                return cmd.ExecuteScalar();
            }          
        }
    

    就个人而言,我认为你应该提高你的编程基础知识。示例代码中有两个大问题:

  • 你想得到这个值,但是你的函数是空的,不返回任何东西,甚至把这个值设置成某个变量
  • ExecuteOnQuery不是您的情况
  • 例如:

     public string Balance()
        {
            string query = "SELECT balance FROM history ORDER BY id DESC LIMIT 1 ";
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                return cmd.ExecuteScalar();
            }          
        }
    
    让我们来看看:

    // You probably want to return value: decimal, not void
    public decimal Balance() {
      // Make sql readable
      string query = 
        @"SELECT balance 
            FROM history 
        ORDER BY id DESC 
           LIMIT 1 ";
    
      // do not cache connection, but create a new one instead 
      using (MySqlConnection conn = new MySqlConnection(connectionStringHere)) {
        conn.Open();
    
        // wrap IDisposable into using  
        using (MySqlCommand cmd = new MySqlCommand(query, conn)) {
          // you want to return values: ExecuteReader (or ExecuteScalar) 
          // instead of ExecuteNonQuery
          using (var reader = cmd.ExecuteReader()) {
            if (reader.Read())
              return Convert.ToDecimal(reader.GetValue(0));
            else
              return 0m; // cursor is empty, let's return 0   
          }
        }          
      }
    } 
    
    让我们来看看:

    // You probably want to return value: decimal, not void
    public decimal Balance() {
      // Make sql readable
      string query = 
        @"SELECT balance 
            FROM history 
        ORDER BY id DESC 
           LIMIT 1 ";
    
      // do not cache connection, but create a new one instead 
      using (MySqlConnection conn = new MySqlConnection(connectionStringHere)) {
        conn.Open();
    
        // wrap IDisposable into using  
        using (MySqlCommand cmd = new MySqlCommand(query, conn)) {
          // you want to return values: ExecuteReader (or ExecuteScalar) 
          // instead of ExecuteNonQuery
          using (var reader = cmd.ExecuteReader()) {
            if (reader.Read())
              return Convert.ToDecimal(reader.GetValue(0));
            else
              return 0m; // cursor is empty, let's return 0   
          }
        }          
      }
    } 
    

    您的函数
    Balance()
    返回一个
    void
    (与“无返回值”相同)。例如,将其更改为字符串,从查询中读取数据(必须更改,
    ExecuteNonQuery
    不返回值),然后使用
    return
    返回数据。您的函数
    Balance()
    返回一个
    void
    (如“无返回值”)。例如,将其更改为字符串,从查询中读取数据(必须更改,
    ExecuteNonQuery
    不返回值),然后使用
    return
    返回数据。谢谢!但现在我想在我的主窗体中获得该值,我正在键入
    take.Balance()但它显示警告1字段“Budget.Form1.take”从未分配给,并且其默认值始终为空。在我的主窗体中,我应该如何将该值放入文本框?谢谢!但现在我想在我的主窗体中获得该值,我正在键入
    take.Balance()但它显示警告1字段“Budget.Form1.take”从未分配给,并且其默认值始终为空。在主窗体中,我应该如何将该值放入文本框?