未将对象引用设置为对象的实例 - 错误
I'm trying to insert in an Access Database, from visual C#. But i got this error: error
What am I doing wrong in code? The values are correct, they comes from the input boxes.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从图中看来,adapter.InsertCommand 属性为 null。
而不是
使用
From the picture it looks like the adapter.InsertCommand property is null.
Instead of
use
您正在创建
OleDbDataAdapter 适配器
,还可以创建一个(独立的)OleDbCommand insertCommand
- 但您不 为adapter.InsertCommand
创建一个实例 - 该变量将为 NULL !您需要执行以下操作:
创建
adapter.InsertCommand
而不是独立实例。You're creating the
OleDbDataAdapter adapter
ok, and you're creating a (stand-alone)OleDbCommand insertCommand
as well - but you're NOT creating an instance foradapter.InsertCommand
- that variable will be NULL !You need to do:
Create the
adapter.InsertCommand
instead of a stand-alone instance.在图片的左下角,我看到“adapter.InserCommand”为空,而在创建新的“oleDbCommand”后发生错误,这是异常的根源。为什么 ?因为您创建了一个“oleDbCommand”并且没有将其分配给您的适配器。
无论如何,不推荐您处理sql的方式,并且代码容易受到sql注入攻击。
另外,不建议将大量代码放入“Try”块中,因为稍后您无法找到问题的根源。
At bottom left of picture I see "adapter.InserCommand" is null while the error occurred after you created a new "oleDbCommand" and it's the source of exception. Why ? Because you created one "oleDbCommand" and didn't assign it to your adapter.
Anyway, the way you deal with sql is not recommended and the code is prone to sql injection attacks.
Also putting a large amount of code inside a "Try" block is not recommended since you cannot find the source of problem later.