2016-08-05 141 views
0

我已经从教程中复制了确切的代码,并且所有连接建立起来了,并且我的Sql select语句也在工作,只是读取器命令不起作用读取器出现故障并且不会增加计数值。与MySql的C#窗口应用程序

我的继承人代码

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); 
     } 
    } 

} 
} 
+2

您的代码将很容易破解!请阅读SQL注入攻击和mySQL参数。 https://blog.udemy.com/sql-injection-tutorial/和http://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp – beercohol

回答

0

MySql command存在一些问题,你以后忘了空间*此外,如果你正在使用usernamepasswordtextbo X,则你必须使用Text财产。这就是为什么没有数据读取和计数增量。

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

我假设你的意思是写:

this.text1_txt.Text and this.text2_txt.Text 

,而不是仅仅this.text1_txtthis.text2_txt。仅使用this.text2_txt将使用ToString()方法从对象中获取串联用于concatanation。

您应该使用参数...

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"); 
} 
... 
0

附加到上面的评论,如果你只对正在返回的记录数感兴趣,那么用下面的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();是在最后的中频,所以不会被调用时,计数不是0

我想这取决于你用什么这个,但个人,如果我要拒绝访问(作为一个登录),我永远不会提供任何解释为什么访问被拒绝 - 因为这可以帮助人们进入,当你不应该。