[VB.NET]老问题,插入数据并发错误?

前端之家收集整理的这篇文章主要介绍了[VB.NET]老问题,插入数据并发错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
老问题,插入数据并发错误? ''简单的数据存取.动态表.有主键,原因在于未能自动提升主键值。(用自动生成的数据集就没此问题) Imports System.Data.sqlClient Public Class Form1 Dim comm As New sqlClient.sqlConnection Dim ad As New sqlDataAdapter("SELECT * FROM 数据表",comm) Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Dim st As String = "D:/temp/sql.mdf" Dim SqTex As String = "Data Source=./sqlEXPRESS;AttachDbFilename=" & st & ";Integrated Security=True;Connect Timeout=30;User Instance=True" comm.ConnectionString = SqTex comm.Open() Dim dber As New sqlCommandBuilder(ad) ad.DeleteCommand = dber.GetDeleteCommand ad.InsertCommand = dber.GetInsertCommand ad.UpdateCommand = dber.GetUpdateCommand Dim ds As New DataSet ad.FillSchema(ds,SchemaType.Mapped,"数据表") ad.Fill(ds,"数据表") DataGridView1.DataSource = ds DataGridView1.DataMember = "数据表" End Sub Private Sub DataGridView1_CellEndEdit(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit Try Me.Validate() Catch End Try Dim ds As DataSet =Ctype(DataGridView1.DataSource,DataSet) ad.Update(ds,"数据表") End Sub End Class __________________________________________________________________________ 什么问题? __________________________________________________________________________ 用解除约束试一试。 __________________________________________________________________________ 引用 1 楼 qinhl99 的回复: 什么问题? 1.)创建一个测试表 如: (先写入一条或多条记录) AAA BBB HHH 序号 ''----序号设为主键 .. .. .. 0 贴上代码执行,插入一条记录看会发生什么? __________________________________________________________________________ 今天忠于将问题解决了。还是老方法。 Dim cn As New sqlCommand("SELECT MAX(序号) FROM 数据表",comm) Dim sd As Integer = cn.ExecuteScalar + 1 ds.Tables("数据表").Columns("序号").AutoIncrement = True ds.Tables("数据表").Columns("序号").AutoIncrementSeed = sd ds.Tables("数据表").Columns("序号").AutoIncrementStep = 1 ''---------------------------- 用sql语句设置的主键在更新的时候都要刷新填充表,表会滚动, 不知还有更好的方法? __________________________________________________________________________ 来学习了. __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/264115.html

猜你在找的VB相关文章