C# 基于MySql的C-windows应用
我已经从一个教程中复制了确切的代码,所有的连接构建都在工作,我的Sql select语句也在工作,只是读卡器命令没有运行,读卡器出现故障,并且没有增加计数值 这是我的密码C# 基于MySql的C-windows应用,c#,mysql,C#,Mysql,我已经从一个教程中复制了确切的代码,所有的连接构建都在工作,我的Sql select语句也在工作,只是读卡器命令没有运行,读卡器出现故障,并且没有增加计数值 这是我的密码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
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);
}
}
}
}
您的MySql命令有一些问题,您忘记了*后面的空格。此外,如果您使用的是textbox中的用户名和密码,则必须使用Text属性。这就是为什么没有数据读取,也没有计数增量
MySqlCommand Selectcommand = new MySqlCommand("select * from mydb.supervisors where username='" + this.text1_txt.Text + "' and password = '" + this.text2_txt.Text + "';", myConn);
您的MySql命令有一些问题,您忘记了*后面的空格。此外,如果您使用的是textbox中的用户名和密码,则必须使用Text属性。这就是为什么没有数据读取,也没有计数增量
MySqlCommand Selectcommand = new MySqlCommand("select * from mydb.supervisors where username='" + this.text1_txt.Text + "' and password = '" + this.text2_txt.Text + "';", myConn);
我想你是想写:
this.text1_txt.Text and this.text2_txt.Text
而不仅仅是this.text1_txt和this.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");
}
...
我想你是想写:
this.text1_txt.Text and this.text2_txt.Text
而不仅仅是this.text1_txt和this.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");
}
...
除了上面的注释之外,如果您只对返回的记录数感兴趣,那么最好使用下面的SQL语句只返回记录数。只有几行,SELECT*FROM可以工作,但是如果你点击了一个记录数量非常多的表,那么SELECT*FROM将返回所有数据,并可能影响应用程序的性能;然而,计数*不会返回数据
SELECT COUNT(*) AS RECORD_COUNT
FROM MYDB.SUPERVISORS
WHERE ...
reader.Read();
if (reader[0] != 1)
{
MessageBox.Show("Access Denied");
return;
}
如果你真的必须保持栈的IF,那么你应该考虑使用开关代替。那是我的近况;在最后一个IF内,因此当计数不是0时不会被调用
我想这取决于你使用它的目的,但就我个人而言,如果我打算在登录时拒绝访问,我永远不会解释为什么拒绝访问-因为这可以帮助人们进入,而你不应该进入。除了上面的评论,如果您只对返回的记录数感兴趣,那么最好使用下面的SQL语句只返回记录数。只有几行,SELECT*FROM可以工作,但是如果你点击了一个记录数量非常多的表,那么SELECT*FROM将返回所有数据,并可能影响应用程序的性能;然而,计数*不会返回数据
SELECT COUNT(*) AS RECORD_COUNT
FROM MYDB.SUPERVISORS
WHERE ...
reader.Read();
if (reader[0] != 1)
{
MessageBox.Show("Access Denied");
return;
}
如果你真的必须保持栈的IF,那么你应该考虑使用开关代替。那是我的近况;在最后一个IF内,因此当计数不是0时不会被调用
我想这取决于你使用它的目的,但就我个人而言,如果我打算在登录时拒绝访问,我将永远不会解释为什么拒绝访问-因为这可以帮助人们进入,而你不应该。你的代码将很容易被破解!请阅读SQL注入攻击和mySQL参数。你的代码将很容易被破解!请阅读SQL注入攻击和mySQL参数。和