[英]Object reference not set to an instance of an object - error
从图片中看起来像是adapter.InsertCommand属性为null。
代替
adapter.Insertcommand.CommandText = ...
采用
insertCommand.CommandText = ...
adapter.InsertCommand = insertCommand;
您正在创建OleDbDataAdapter adapter
,还正在创建(独立的) OleDbCommand insertCommand
但您没有为adapter.InsertCommand
创建实例-该变量将为NULL!
您需要做:
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand();
创建adapter.InsertCommand
而不是独立实例。
在图片的左下角,我看到“ adapter.InserCommand”为空,而在创建新的“ oleDbCommand”后发生了错误,这是异常的来源。 为什么呢 因为您创建了一个“ oleDbCommand”,但未将其分配给适配器。
无论如何,不建议您使用sql进行处理,并且代码易于受到sql注入攻击。 不建议在“ Try”块中放入大量代码,因为以后找不到问题的根源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.