Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.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#-从mySQL数据库获取数据时出错_C#_Mysql - Fatal编程技术网

C#-从mySQL数据库获取数据时出错

C#-从mySQL数据库获取数据时出错,c#,mysql,C#,Mysql,我当前在尝试从mySQL获取数据时出错: Additional information: Could not find specified column in results: admin 我的代码是: public int getLevel() { string sqlCommand = "Select level from users where username = 'admin'"; int value = 0; MySqlCo

我当前在尝试从mySQL获取数据时出错:

Additional information: Could not find specified column in results: admin
我的代码是:

public int getLevel()
    {
        string sqlCommand = "Select level from users where username = 'admin'";
        int value = 0;

        MySqlConnection con = new MySqlConnection("host=111.222.111.222;user=MYUSERNAME;password=MYPASSWORD;database=tcg;");
        MySqlCommand cmd = new MySqlCommand(sqlCommand, con);

        con.Open();

        MySqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            value += int.Parse(reader.GetString("admin"));
        }
        return value;
    }
尝试:

如果您只有一名管理员,请使用:

public int getLevel()
 {
     int value = 0;
     using(MySqlConnection con = new MySqlConnection("host=45.37.80.181;user=MYUSERNAME;password=MYPASSWORD;database=tcg;"))
     {
         con.Open();
         using(MySqlCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "Select level from users where username = @ad";//add Order by if you need to
             cmd.Parameters.AddWithValue("@ad","admin");
             value += Convert.ToInt32(com.ExecuteScalar());//this assumes you will get an integer value

        }
        con.Close();
     }
     return value;
}

您只需返回列
级别
,在开始编写太多代码之前,请利用
使用
块来安全使用资源。否则,您的所有代码都将如上面所示:pYeah,我正在尝试获取level的数据,那么为什么您有
GetString(“admin”)如果你以前从未使用过结果集,那么就这么说,也许有人可以键入答案。但这就像是一个1小时的教程,stackoverflow不是那种网站不知道你为什么会有DV。这里有一个。谢谢你的帮助,因为我在SELECT语句中放了TOP,它是SQL而不是MySQL。我还没来得及编辑,就有人把他们的内裤弄得乱七八糟。不用担心,很高兴这有帮助。干杯
public int getLevel()
 {
     int value = 0;
     using(MySqlConnection con = new MySqlConnection("host=45.37.80.181;user=MYUSERNAME;password=MYPASSWORD;database=tcg;"))
     {
         con.Open();
         using(MySqlCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "Select level from users where username = @ad";//add Order by if you need to
             cmd.Parameters.AddWithValue("@ad","admin");
             value += Convert.ToInt32(com.ExecuteScalar());//this assumes you will get an integer value

        }
        con.Close();
     }
     return value;
}