记录MYSQL登录详细信息

时间:2016-08-24 15:25:11

标签: c# mysql

我有一个登录mysql的工具,但是当有人登录或试图让任何人帮我这个时,我试图让它登录?它出于某种原因说无法连接到mysql,如果我在连接信息下添加以下日志代码,则无法找到连接

private void button1_Click(object sender, EventArgs e)
{
    try
    {


        string MyConnection = "datasource=localhost;port=3306;username=root;password=password";
        MySqlConnection MyConn = new MySqlConnection(MyConnection);
        MySqlCommand MyCommand = new MySqlCommand("select * from bans.toollogin where user_name='" + this.UserTextBox.Text + "' and password='" + this.PassTextBox.Text + "' ;", MyConn);
       MySqlDataReader MyReader;
        string cmdText = "INSERT INTO logs(login, password) VALUES (login, password)";
        MySqlCommand cmd = new MySqlCommand(cmdText, MyConn);
        cmd.Parameters.AddWithValue("login", UserTextBox.Text);
        cmd.Parameters.AddWithValue("password", PassTextBox.Text);
        cmd.ExecuteNonQuery();

        MyConn.Open();
        MyReader = MyCommand.ExecuteReader();
        int count = 0;
        while (MyReader.Read())
        {
            Console.WriteLine(MyReader[count]);
            count++;
        }
        if (count == 1)
        {
            MessageBox.Show("Username and password is correct");
            this.Hide();
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
        else if (count > 1)
        {

            MessageBox.Show("Duplicate Username and passwor.\nAccess denied.");
        }
       else

        {
            MessageBox.Show("Username and password is incorrect.\nPleas try again.");
        }
        MyConn.Close();

    }
    catch (Exception ex)
    {

        MessageBox.Show(ex.Message);
    }

}

我添加的代码尝试将其记录为

string cmdText = "INSERT INTO logs(login, password) VALUES (login, password)";
                MySqlCommand cmd = new MySqlCommand(cmdText, MyConn);
                cmd.Parameters.AddWithValue("login", UserTextBox.Text);
                cmd.Parameters.AddWithValue("password", PassTextBox.Text);
                cmd.ExecuteNonQuery();

0 个答案:

没有答案