繁体   English   中英

C#Windows应用程序与MySql

[英]C# windows application with MySql

我已经从教程中复制了确切的代码,并且所有连接建立过程都在起作用,而我的Sql select语句也正在起作用,只是Reader命令无法正常工作,Reader出现了故障并且没有增加Count值。

这是我的代码

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using MySql.Data.MySqlClient;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string myConnection = "datasource= localhost;port=3306;username=root;password=2905";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand Selectcommand = new MySqlCommand("select *from mydb.supervisors where username='" + this.text1_txt + "' and password = '" + this.text2_txt + "';", myConn);

            myConn.Open();
            MySqlDataReader myReader;

            myReader = Selectcommand.ExecuteReader();
            int count = 0;
            while (myReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                MessageBox.Show("Yayyyy");
            }
            else if (count > 1)
            {
                MessageBox.Show("Duplicate Parameters - Accesss Denied");

            }
            else if (count == 0)
            {
                MessageBox.Show("UserName, Password Dont Match with Hostel");
                myConn.Close();
            }
            }




        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

}
}

我想你打算写:

this.text1_txt.Text and this.text2_txt.Text

而不只是this.text1_txtthis.text2_txt 仅使用this.text2_txt将使用ToString()方法从对象中获取字符串以进行连接。

您应该使用参数...

MySqlCommand Selectcommand = 
      new MySqlCommand(
         @"select * from mydb.supervisors 
            where username= @username and password = @password;", myConn);

Selectcommand.Parameters.AddWithValue("@username ", this.text1_txt.Text);
Selectcommand.Parameters.AddWithValue("@password", this.text2_txt.Text);

您的查询很可能会产生一或零行的结果,但是您可能想要尝试以下操作:

MySqlCommand Selectcommand = 
      new MySqlCommand(
         @"select count(*) as numrows from mydb.supervisors 
            where username= @username and password = @password;", myConn);

int numrows =  (int) Selectcommand.ExecuteScalar(); 

if (numrows == 1)
{
    MessageBox.Show("Yayyyy");
}
...

您的MySql command有一些问题,如果您正在使用textbo x中的usernamepassword ,那么您在*之后也忘记了空格,那么您必须使用Text属性。 这就是为什么没有数据在读取并且没有计数增量的原因。

MySqlCommand Selectcommand = new MySqlCommand("select * from mydb.supervisors where username='" + this.text1_txt.Text + "' and password = '" + this.text2_txt.Text + "';", myConn);

除了上面的注释,如果您只对返回的记录数感兴趣,那么最好使用以下SQL语句仅返回记录数。 仅使用几行,SELECT * FROM将起作用,但是如果您击中具有大量记录的表,则SELECT * FROM将带回所有这些数据,并可能影响应用程序的性能。 而COUNT(*)不会带回数据。

SELECT COUNT(*) AS RECORD_COUNT
FROM MYDB.SUPERVISORS
WHERE ...

reader.Read();

if (reader[0] != 1)
{
  MessageBox.Show("Access Denied");
  return;
}

如果确实必须保留“ IF”堆栈,则应考虑改用“ SWITCH”。 那myConn.Close(); 在最后一个IF内,因此当计数不为0时不会调用

我想这取决于您使用它的目的,但是就我个人而言,如果我要拒绝访问(作为登录的一部分),我将永远不会提供任何有关为什么拒绝访问的解释-因为这可以帮助人们获得访问权限在,当你不应该。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM