如何将Datagridview中的数据插入数据库 [英] How do I insert data from Datagridview into database

查看:256
本文介绍了如何将Datagridview中的数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在插入按钮下

Under the insert button

sql = "insert into student (name,id)" & _

 "Values("& dgvData.CurrentRow.Cell(0).Value &","& dgvData.CurrentRow.Cell(0). Value &")"

connect()
cmd=new OledbCommand (sql,connect)
cmd.ExecuteNonQuery()

< br $> b $ b





这是代码,但是当我尝试插入它不运行它显示主键不能包含空值





That's the code but when I try to insert it doesn't run it displays primary key cannot contain a null value

推荐答案

如消息所示,您发送的是null,因为不允许使用主键。你需要检查当前行的Cell(0)中的值不为null时才允许插入命令。



另外,你发送的是命名为主键。这是故意的吗?如果是,那么如果有两个同名的人可能会出现问题。
As the message states, you are sending null as primary key is not allowed. You will need to put a check that insert command is only allowed when the value in Cell(0) of the current row is not null.

Also, you are sending name as primary key. Is that intentional? If yes then, there could be a problem if there are two people with same name.


这篇关于如何将Datagridview中的数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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