对象引用未设置为对象的实例 - 错误

时间:2010-06-13 11:07:26

标签: c# ms-access oledbconnection

我正在尝试从Visual C#插入Access数据库。但是我收到了这个错误:error

我在代码中做错了什么?值是正确的,它们来自输入框。

谢谢!

3 个答案:

答案 0 :(得分:3)

从图片看起来,adapter.InsertCommand属性为null 而不是

adapter.Insertcommand.CommandText = ...

使用

insertCommand.CommandText = ...
adapter.InsertCommand = insertCommand;

答案 1 :(得分:1)

您正在创建OleDbDataAdapter adapter确定,并且您正在创建(独立)OleDbCommand insertCommand - 但您创建实例adapter.InsertCommand - 该变量将为NULL!

你需要这样做:

OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand();

创建adapter.InsertCommand而不是独立的实例。

答案 2 :(得分:0)

在图片的左下角,我看到“adapter.InserCommand”为空,而在您创建新的“oleDbCommand”后发生错误,它是异常的来源。为什么?因为您创建了一个“oleDbCommand”并且未将其分配给适配器。

无论如何,不​​推荐你处理sql的方式,而且代码很容易受到sql注入攻击。 另外,建议不要在“Try”块中放入大量代码,因为以后找不到问题的根源。

相关问题