数据库连接C#ExecuteReader with MySql

时间:2012-12-09 17:24:33

标签: c# mysql

我有一些问题,我已经在互联网上看到了代码的和平,只有这与Oledb已经完成了。

现在我已经重写了MySQL.data.mysqlclient的代码,如下所示:

MySqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and password='"+PassWrd.Text+"' LIMIT 1";
        dr = cmd.ExecuteNonReader();
        cmd = new MySqlCommand(cmdstr, connection);
        cmd.Dispose();

        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        connection.Close();

    }

现在问题是dr = cmd.ExecuteNonReader()的另一种方法。

UPDATE ----------------

string server;
        string database;
        string uid;
        string password;
        server = "localhost";
        database = "cmstt";
        uid = "root";
        password = "";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        MySqlConnection connection = new MySqlConnection(connectionString);

        connection.Open();

        MySqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and pass='"+PassWrd.Text+"' LIMIT 1";
        dr = cmd.ExecuteReader();
        cmd = new MySqlCommand(cmdstr, connection);


        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        cmd.Dispose();
        connection.Close();

2 个答案:

答案 0 :(得分:2)

ExecuteReader()不是ExecuteNonReader()

//在这里提及您的连接和连接字符串。

        connection.open()

        SqlDataReader dr = null;
        MySqlCommand cmd = null;
        string cmdstr = "SELECT * FROM users WHERE email='"+UsrName.Text+"' and password='"+PassWrd.Text+"' LIMIT 1";

        cmd = new MySqlCommand(cmdstr, connection);
        dr = cmd.ExecuteReader();

        if (dr.Read() == true)
        {
            MessageBox.Show("Succesvol ingelogd");
        }
        else
        {
            MessageBox.Show("Geen juiste gegevens");
        }
        connection.Close();

    }

答案 1 :(得分:0)

您过早地处理cmd。在关闭连接之前处理它。