使用Datagrid添加,删除,编辑和更新数据库 [英] Add, delete, edit and update database using Datagrid
问题描述
大家好,
我现在已经处理了这个问题四个小时了,几乎放弃了。我只想问一下如何使用datagrid和更新SQL服务器来完成创建,删除,更新和删除。
这就是我所拥有的:
Private Sub LoadData()
Hi Everyone,
I've been dealing with this issue for four hours now and almost giving up. I just want to ask how create, delete, update and delete can be done with the use of datagrid and update SQL server.
This is what I have:
Private Sub LoadData()
Try
Dim connectionString As String = "Data Source=PHEMSSCGP-L655\SQLEXPRESS;Initial Catalog=COE;" & "Integrated Security=SSPI;"
conn = New SqlConnection(connectionString)
conn.Open()
Dim adap As New SqlDataAdapter("Select SerialNum , BuildNum, PartBuildID , OrderNum , XPart , YPart ,Rot, Height , PartMass , SupportMass , Special FROM tblBuildPlan where BuildNum =" & cmbBuildNum.SelectedValue(), conn)
Dim ds As New DataSet
adap.Fill(ds, "BAM")
GVBuilPlan.DataSource = ds.Tables(0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
对于更新按钮,这就是我所拥有的:
For update button this is what I have:
Try
Dim cmbl As SqlCommandBuilder = New SqlCommandBuilder(da)
cmbl.GetUpdateCommand()
da.Update(ds, "BAM")
MessageBox.Show("Success")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
我可以在gridview中成功显示数据库中的值但是当我尝试编辑或添加数据并点击更新按钮时会出现问题。
这是错误:
对于SelectCommand,不支持UpdateCommand的动态SQL生成没有返回任何关键列信息。
根据我所读到的,我必须有一个我做的原始密钥,并且它是来自tblBuildMaster表的SerialNum。
任何帮助,将不胜感激。
谢谢大家!
I can successfully display the values in the db in the gridview but the problem happens when I try to edit or add data and hit update button.
This is the error:
Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
According to what I read, I have to have a primiary key which I do, and it is the SerialNum from the tblBuildMaster table.
Any help, would be greatly appreciated.
Thank you everyone!
推荐答案
根据错误看起来你没有桌上的主键。请再次检查表上是否有PRIMARY KEY约束,而不是唯一索引或类似的索引。你可以使用例如SSMS能力来编写表格脚本以查看表格上的约束条件。
作为旁注,我真的建议使用 SqlParameter [ ^ ]在你的select语句可以安全地避免SQL注入,转换问题等。
Based on the error it looks like you don't have a primary key on the table. Please check again that you have a PRIMARY KEY constraint on the table, not a unique index or nothing similar. You can use for example SSMS ability to script the table to see what are the constraints on the table.
As a side note I'd really suggest using SqlParameter[^] in your select statement in orderto be safe from SQL injections, conversion problems etc.
这篇关于使用Datagrid添加,删除,编辑和更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!