Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#app中where子句中的未知列_C#_Mysql - Fatal编程技术网

C#app中where子句中的未知列

C#app中where子句中的未知列,c#,mysql,C#,Mysql,我正在尝试开发一个C#应用程序,我想让登录表单连接到远程服务器。我已连接到服务器,但当我尝试登录时,行:MySqlDataReader=cmd.ExecuteReader();给我一个错误:where子句中的未知列“admin”你知道问题出在哪里吗?这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;

我正在尝试开发一个C#应用程序,我想让登录表单连接到远程服务器。我已连接到服务器,但当我尝试登录时,行:MySqlDataReader=cmd.ExecuteReader();给我一个错误:
where子句中的未知列“admin”
你知道问题出在哪里吗?这是我的密码:

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

namespace ECBSRecruitmentAgencySoftware
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }  

        public bool tryLogin(string username , string password)
        {
             MySqlConnection con = new MySqlConnection("host=aaaaaaaa.baaadsg;user=saaaaaak;password=2333333336;database=soaaaaaaaa2;");
             MySqlCommand cmd = new MySqlCommand("Select * FROM niki WHERE user_name = `" + username + "` AND user_password = `" + password + "`;");
             cmd.Connection = con;
             con.Open();
             MySqlDataReader reader = cmd.ExecuteReader();
             if (reader.Read() != false)
             {
                 if (reader.IsDBNull(0) == true)
                 {
                     cmd.Connection.Close();
                     reader.Dispose();
                     cmd.Dispose();
                     return false;
                 }
                 else
                 {
                     cmd.Connection.Close();
                     reader.Dispose();
                     cmd.Dispose();
                     return true;
                  }
             }
             else 
             {
                 return false;
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tryLogin(user.Text, pass.Text) == true)
            {
                MainScreen F2 = new MainScreen();
                F2.Show();
                this.Hide();
            }            
            else 
                MessageBox.Show("Wrong details!");             
        } 
    }
 }

在查询中,使用
而不是(`)引用字符串

试试这个:

MySqlCommand cmd = new MySqlCommand("Select * FROM niki WHERE user_name = '" + username + "' AND user_password = '" + password + "'");
如果名称是保留名称,则`是指表和列的名称。例如,tbl.`from`将指一个名为“from”的列,而不是SQL保留字from


这就是为什么您会收到一条错误消息,指出列名“admin”无效,因为MySQL在命令中找到“admin”时认为您引用的是一个名为“admin”的列。猜测您在登录表单中输入“admin”作为用户名当然没有奖品!:)

使用命名参数而不是您的解决方案:

MySqlCommand cmd = new MySqlCommand("Select * FROM niki WHERE user_name = @username AND user_password = @password");
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);

UPD:更新为AddWithValue,表示未处理异常

1。您应该使用参数化查询,而不是字符串连接。您非常容易受到SQL注入的攻击。2.您不应该存储明文密码,您应该存储密码的哈希值,每当有人输入密码时,您应该立即对其进行哈希,然后忽略明文密码,只需比较两个哈希值。3.您应该返回count(),而不是返回查询中的所有列。你只关心*是否有一行有正确的用户/密码,而不关心所有的信息是什么。我假设你试图用“admin”用户名登录?@Servy你能给我举个例子,或者我可以在你的密码文本框中查找try
”或“1”='1
)@Tremmors我替换了真实的用户名,但让我们假装这是真实的用户名,是的,我想使用那个。
cmd.Parameters.AddWithValue(“@username”,username)
MySqlCommand cmd = new MySqlCommand("Select * FROM niki WHERE user_name = @username AND user_password = @password");
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);