0

我试图从数据库中获取单个值,但循环似乎根本没有开始?

if (connection.State == ConnectionState.Open)
        {
            MySqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                value = dataReader["amount"].ToString();
            }
            dataReader.Close();
            connection.Close();
        }

这些是命令:

 public string value; 
    public static string Konekcija = "Server=127.0.0.1; Database=CardIgrica; Uid=admin; Pwd=admin;";
    public string komanda = "SELECT amount FROM CardIgrica.creaures WHERE id = '1';";
    MySqlConnection connection = new MySqlConnection(Konekcija);
MySqlCommand cmd = new MySqlCommand(komanda, connection);
connection.Open();
4

4 回答 4

0

If you need single value, you can use cmd.ExecuteScalar(); instead of cmd.ExecuteReader(); because ExecuteScalar() will return single value.

于 2013-10-23T13:07:03.750 回答
0

尝试这个

    using (SqlConnection conn = new SqlConnection(Konekcija))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();
        cmd.CommandText = komanda ;
        conn.Open();
        var dataReader= cmd.ExecuteReader();

        while (result.Read())
        {
          value = dataReader["amount"].ToString();
        }


        conn.Close();

    }
于 2013-10-23T13:06:17.103 回答
0

试试这个选择

public string value {get; set;}
string komanda ="SELECT amount FROM CardIgrica.creaures WHERE id = 1"; 
于 2013-10-23T11:25:27.470 回答
0

首先打开你的 sql 连接,然后创建 MySqlCommand 的对象,如下所示:

MySqlConnection connection = new MySqlConnection(Konekcija);

connection.Open();


MySqlCommand cmd = new MySqlCommand(komanda, connection);
于 2013-10-23T12:49:50.200 回答