C#visual studio with sqlconnection我该怎么做.. [英] C# visual studio with sqlconnection how do I do this..

查看:93
本文介绍了C#visual studio with sqlconnection我该怎么做..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者

即时尝试登录并使用存储在localhost中的用户名和密码数据注册表单。

我可以获得示例如何操作..谢谢



我尝试过的事情:



编码不好

im a beginner
im trying to make login and register forms with username&password data stored in localhost..
can i get example how to do it.. thanks

What I have tried:

im bad at coding

public partial class Login : Form
    {
		public Login()
        {
            InitializeComponent();
            txtPassword.PasswordChar = '*';
            txtPassword.MaxLength = 10;

        }

        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3307;Initial Catalog='dblogin';username=root;password=320139");
        MySqlDataAdapter adapter;
        DataTable table = new DataTable();

        private void btnLogin_Click(object sender, EventArgs e)
        {
            adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + TxtUsername.Text + "' AND `password` = '" + txtPassword.Text + "'", connection);

            if (TxtUsername.Text == username)
            {
                if (txtPassword.Text == password)
                {
                    ListMeja lm = new ListMeja();
                    this.Hide();
                    lm.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Username/password incorrect", "alert!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }





此行错误



error at this line

if (TxtUsername.Text == username)



当前上下文中不存在用户名:


The name username does not exist in the current context

推荐答案

你必须执行存储结果的SQL查询。因为查询已经将结果限制为匹配用户名和密码,所以只要组合是唯一的(应该是唯一的),将只返回一行或一行。因此,返回的行数可用于检查正确的值:

You have to execute the SQL query storing the result. Because the query already limits the result to matching username and password, there will be only no or one row returned provided that the combination is unique (which it should be). So the number of returned rows can be used to check for proper values:
adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = '" + TxtUsername.Text + "' AND `password` = '" + txtPassword.Text + "'", connection);
DataSet dataset;
if (adapter.Fill(dataset) == 0)
{
    MessageBox.Show("Username/password incorrect", "alert!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
    // Valid data
}



另请注意,您的查询容易出现 SQL注入 - 维基百科 [ ^ ]。永远不要通过字符串连接创建SQL命令。请改用参数化查询:


Note also that your query is prone to SQL injection - Wikipedia[^]. Never create SQL commands by string concatenation. Use parametrised queries instead:

adapter = new MySqlDataAdapter("SELECT `username`, `password` FROM `users` WHERE `username` = @username AND `password` = @password", connection);
adapter.SelectCommand.Parameters.Add("@username", MySqlDbType.VarChar).Value = TxtUsername.Text;
adapter.SelectCommand.Parameters.Add("@password", MySqlDbType.VarChar).Value = TxtPassword.Text;
DataSet dataset;
int rows = adapter.Fill(dataset);


No。不要这样做!

首先:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

No. Do not do it like that!.
First off: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

和登录一起做?这只是简单的邀请麻烦,因为任何人都可以通过输入这样的用户名来绕过您的登录保护:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And to do it with login? That's just plain inviting trouble as anyone can bypass your login protect by typing in a username like this:

Admin';--





其次:绝不以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]



第三:为什么你要检查用户名和用户名密码?它们甚至不存在,你根本不使用DB的结果。



停止猜测你在做什么,并阅读DB和如何使用它们 - 这个文本框不够大,不足以教你所有你需要知道的东西,而你似乎还没有把握任何足够的东西来解决这个项目。



Secondly: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Third: why on earth are you checking the username against the username, and the password against the password? They don't even exist, and you don't use the results from the DB at all.

Stop guessing what you are doing, and read up on DB's and how to use them - this text box isn't big enough to teach you all that you need to know, and you don't seem to have grasped any of it enough to tackle the project yet.


这篇关于C#visual studio with sqlconnection我该怎么做..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆