我已经从教程中复制了确切的代码,并且所有连接建立起来了,并且我的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);
}
}
}
}
您的代码将很容易破解!请阅读SQL注入攻击和mySQL参数。 https://blog.udemy.com/sql-injection-tutorial/和http://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp – beercohol