Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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# System.Data.OleDb.OleDbException';发生在System.Data.dll中_C#_Sql_Database_Ms Access_Oledb - Fatal编程技术网

C# System.Data.OleDb.OleDbException';发生在System.Data.dll中

C# System.Data.OleDb.OleDbException';发生在System.Data.dll中,c#,sql,database,ms-access,oledb,C#,Sql,Database,Ms Access,Oledb,我应该能够在一个文本框中键入一个邮政编码,所以当程序运行时,应该在另一个文本框中输入一个街道名称和匹配的邮政编码 然而,我得到了这个错误,我不能真正解决它。错误出现在这一行: leesAdres = Adres.ExecuteReader(); 整个代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb;

我应该能够在一个文本框中键入一个邮政编码,所以当程序运行时,应该在另一个文本框中输入一个街道名称和匹配的邮政编码

然而,我得到了这个错误,我不能真正解决它。错误出现在这一行:

leesAdres = Adres.ExecuteReader();
整个代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnZoek_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            OleDbCommand Adres = new OleDbCommand("Select `street` from `postcode` where `postcode` LIKE txtPostcode.Text", con);
            OleDbDataReader readAdres;
            con.ConnectionString = "provider=Microsoft.Ace.OLEDB.12.0; data Source = C:\\Users\\name\\Documents\\school\\Adodotnet\\postcode.accdb";

            con.Open();
            readAdres = Adres.ExecuteReader();

            while (leesAdres.Read())
            {
                txtStreet.Text = leesAdres.GetValue(0).ToString();
            }
        }
    }
}

我怀疑这可能是我的SQL命令,但我不确定,我没有SQL和数据库方面的经验

您在队列中误解了查询字符串

"Select `street` from `postcode` where `postcode` LIKE txtPostcode.Text", con
应该是

"Select `street` from `postcode` where `postcode` LIKE '%" + txtPostcode.Text+ "%'" , con
为了使您的代码更加健壮,并防止SQL注入,您可以将其构造为

SqlCommand cmd = new SqlCommand("Select `street` from `postcode` where `postcode` LIKE @P1" , conn);
cmd.Parameters.Add(P1);
cmd.Parameters["P1"].Value = txtTagNumber.Text;

还可以研究stringbuilder类用于构造实际字符串的用法。

您在该行中对查询字符串的构造是错误的

"Select `street` from `postcode` where `postcode` LIKE txtPostcode.Text", con
应该是

"Select `street` from `postcode` where `postcode` LIKE '%" + txtPostcode.Text+ "%'" , con
为了使您的代码更加健壮,并防止SQL注入,您可以将其构造为

SqlCommand cmd = new SqlCommand("Select `street` from `postcode` where `postcode` LIKE @P1" , conn);
cmd.Parameters.Add(P1);
cmd.Parameters["P1"].Value = txtTagNumber.Text;

还可以研究stringbuilder类用于构造实际字符串的用法。

您在该行中对查询字符串的构造是错误的

"Select `street` from `postcode` where `postcode` LIKE txtPostcode.Text", con
应该是

"Select `street` from `postcode` where `postcode` LIKE '%" + txtPostcode.Text+ "%'" , con
为了使您的代码更加健壮,并防止SQL注入,您可以将其构造为

SqlCommand cmd = new SqlCommand("Select `street` from `postcode` where `postcode` LIKE @P1" , conn);
cmd.Parameters.Add(P1);
cmd.Parameters["P1"].Value = txtTagNumber.Text;

还可以研究stringbuilder类用于构造实际字符串的用法。

您在该行中对查询字符串的构造是错误的

"Select `street` from `postcode` where `postcode` LIKE txtPostcode.Text", con
应该是

"Select `street` from `postcode` where `postcode` LIKE '%" + txtPostcode.Text+ "%'" , con
为了使您的代码更加健壮,并防止SQL注入,您可以将其构造为

SqlCommand cmd = new SqlCommand("Select `street` from `postcode` where `postcode` LIKE @P1" , conn);
cmd.Parameters.Add(P1);
cmd.Parameters["P1"].Value = txtTagNumber.Text;



还可以研究使用stringbuilder类来构造实际字符串。

我可以证实您对SQL命令的怀疑:您没有从文本框
txtPostcode
传递值,而是实际提交了文本控件名称(和属性)。@Filburt您能告诉我如何解决它吗?正如我所说,我对这一点还不熟悉。为了将来的参考,包括您看到的错误消息可以帮助诊断错误issue@BadAtPHP请参阅DB010s的答案以解决此问题,但请注意,正如Liath指出的,这被认为是不好的做法。研究将查询条件作为参数传递。否则你的标题会受到攻击。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。我可以确认您对SQL命令的怀疑:您没有从文本框
txtPostcode
传递值,而是实际提交文本控件名称(和属性)。@Filburt您能告诉我如何解决它吗?正如我所说,我对这一点还不熟悉。为了将来的参考,包括您看到的错误消息可以帮助诊断错误issue@BadAtPHP请参阅DB010s的答案以解决此问题,但请注意,正如Liath指出的,这被认为是不好的做法。研究将查询条件作为参数传递。否则你的标题会受到攻击。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。我可以确认您对SQL命令的怀疑:您没有从文本框
txtPostcode
传递值,而是实际提交文本控件名称(和属性)。@Filburt您能告诉我如何解决它吗?正如我所说,我对这一点还不熟悉。为了将来的参考,包括您看到的错误消息可以帮助诊断错误issue@BadAtPHP请参阅DB010s的答案以解决此问题,但请注意,正如Liath指出的,这被认为是不好的做法。研究将查询条件作为参数传递。否则你的标题会受到攻击。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。我可以确认您对SQL命令的怀疑:您没有从文本框
txtPostcode
传递值,而是实际提交文本控件名称(和属性)。@Filburt您能告诉我如何解决它吗?正如我所说,我对这一点还不熟悉。为了将来的参考,包括您看到的错误消息可以帮助诊断错误issue@BadAtPHP请参阅DB010s的答案以解决此问题,但请注意,正如Liath指出的,这被认为是不好的做法。研究将查询条件作为参数传递。否则你的标题会受到攻击。我已经编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。虽然你是对的,但我认为我们不应该提倡这种SQL。我建议更新您的答案,使其不易受到SQL注入攻击。从目前的情况看,这是我的自我控制,不要投反对票…不要投反对票。可以对
txtPostcode
的内容进行特殊检查(在验证期间)。关于代码注入的简单警告就足够了。@Sinatr我在考虑更多关于参数化的问题queries@Liath他不需要(对我来说)太难,这只是我在练习。而且DB101也起作用了!谢谢@BadAtPHP——我不会投反对票,但Liath有权。如果你在练习,你需要养成好习惯。不在特设SQL中使用内联变量被认为是最佳实践。使用字符串生成器和参数化查询——它可以防止人们攻击您的SQL命令。想象一下,如果他们在文本框中放入一堆SQL,代码会像普通SQL一样处理它。垃圾数据库很容易——或者用WAIT命令永远锁定它。虽然你是对的,但我认为我们不应该提倡这种SQL。我建议更新您的答案,使其不易受到SQL注入攻击。从目前的情况看,这是我的自我控制,不要投反对票…不要投反对票。可以对
txtPostcode
的内容进行特殊检查(在验证期间)。关于代码注入的简单警告就足够了。@Sinatr我在考虑更多关于参数化的问题queries@Liath他不需要(对我来说)太难,这只是我在练习。而且DB101也起作用了!谢谢@BadAtPHP——我不会投反对票,但Liath有权。如果你在练习,你需要养成好习惯。不在特设SQL中使用内联变量被认为是最佳实践。使用字符串生成器和参数化查询——它可以防止人们攻击您的SQL命令。想象一下,如果他们在你的文本框中放进一堆SQL,代码会像这样处理它