Oledbexception未得到处理 - 需要C#解释 [英] Oledbexception was unhandled - C# explanation needed please

查看:201
本文介绍了Oledbexception未得到处理 - 需要C#解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个连接到Access的软件的用户登录,我继续收到相同的错误,突出显示该行



'OleDbDataReader reader = command .ExecuteReader();'说...



'System.Data.dll中出现未处理的'System.Data.OleDb.OleDbException'类型异常



附加信息:查询表达式'Username ='MGRjs'和Password'Candy')'中的语法错误(缺少运算符)。'

< br $>


I am creating a user login for my software which is connected to Access, I keep on getting the same error which highlights the line

'OleDbDataReader reader = command.ExecuteReader();' saying...

'An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (missing operator) in query expression 'Username= 'MGRjs' and Password'Candy')'.'


private void LoginButton_Click(object sender, EventArgs e)
{
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password'" + PSWtbx.Text + "')";

        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;
        while (reader.Read())
        {
            count = count + 1;
        }

        if (count == 1)
        {

            MainSystem mainForm = new MainSystem();
            mainForm.FormClosed += new FormClosedEventHandler(Login_FormClosed);
            mainForm.Show();
            this.Hide();
            LoginError.Visible = false;
            connection.Close();
            connection.Dispose();
        }

        else
        {
            LoginError.Visible = true;
        }
        connection.Close();





有什么建议吗?



什么我试过了:



重新连接访问数据库,附加不同的数据库



Any suggestions?

What I have tried:

Reconnecting access database, attaching different database

推荐答案

两件事,两者都很重要:

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



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

Two things, both important:
1) 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. Use Parametrized 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

--'

其他一切都是评论。

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



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



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



修复这些,你的错误很明显:密码和报价之间会有=在用户输入的文本周围。

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?

2) 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.[^]

Fix those, and your error will be apparent: there would have been an "=" between "Password" and the quote around the user entered text.


检查命令中的sql帮助



Examine the sql help in your command

command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password'" + PSWtbx.Text + "')";





它看起来像有效的SQL吗?是





Does it look like valid SQL? Is

and Password'Candy'





有效SQL?



更改为





valid SQL?

Change it to

command.CommandText = "select * from NewUser where Username= '" + IDtbx.Text + "' and Password='" + PSWtbx.Text + "')";





但是你确实需要放弃这种执行SQL的方式,而是使用参数化查询。



However you really need to ditch this way of executing SQL and use parameterised queries instead.


这篇关于Oledbexception未得到处理 - 需要C#解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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