将32位Access数据迁移至64位VS数据库时TableAdapter.Insert失效问题
问题:无法通过TableAdapter.Insert向数据库添加行
我尝试用TableAdapter.Insert向VideoDBDataSet关联的数据库插入行,但始终失败。相关配置:VideoDBDataSet关联VideoDBDataSet.xsd,RecsTableAdapter对应Recs表,TableAdapterManager已关联该TableAdapter,BindingSource指向VideoDBDataSet的Recs表。不管调用Update还是不调用都无效。
pRecs是存储数据库字段的结构体,CoverBytes是JPG文件的字节数据,相关代码如下:
Private Sub FillNewDatabase() Dim CoverBytes() As Byte 'We do this here because CoverBytes is used in this routine ReDim CoverBytes(0) Call GetCoverBytes(pRecs.Common.CoverPath, CoverBytes) RecsTableAdapter.Insert(pRecs.Common.Title, pRecs.Common.Director, pRecs.Common.Cast, pRecs.Common.Related, pRecs.Common.Genre, pRecs.Common.Rated, pRecs.Common.Release, pRecs.Common.Length, pRecs.Common.Link, pRecs.Common.AKA, pRecs.Common.Created, pRecs.Common.Synopse, pRecs.Common.Comment, pRecs.Common.AutoAdded, pRecs.Common.Series, pRecs.Common.Seasons, pRecs.Common.Episodes, CoverBytes, pRecs.Common.CoverPath, pRecs.DVD.DVDInfo, pRecs.DVD.DVDBurned, pRecs.DVD.DVDVob, pRecs.DVD.DVDCollection, pRecs.DVD.DVDDisks, pRecs.DVD.DVDIndexed, pRecs.DVD.DVDFiled, pRecs.DVD.DVDPoor, pRecs.DVD.DVDErrors, pRecs.DVD.DVDMissing, pRecs.DVD.DVDReplace, pRecs.DVD.DVDNote, pRecs.DVD.DVDFiledAs, pRecs.DVD.DVDQuality, pRecs.DVD.DVDReviewed, pRecs.DVD.DVDSubEnglish, pRecs.DVD.DVDSubForeign, pRecs.DVD.DVDAudioEnglish, pRecs.DVD.DVDAudioForeign, pRecs.PlayOn.POInfo, pRecs.PlayOn.PODownloaded, pRecs.PlayOn.POCommercials, pRecs.PlayOn.POMissing, pRecs.PlayOn.POReplace, pRecs.PlayOn.POLow, pRecs.PlayOn.PODan, pRecs.PlayOn.POChecked, pRecs.PlayOn.POTrimmed, pRecs.PlayOn.POPartial, pRecs.PlayOn.POLocConfirmed, pRecs.PlayOn.POLocation, pRecs.PlayOn.POSource, pRecs.PlayOn.POAlt, pRecs.PlayOn.POResolution, pRecs.PlayOn.POReviewed, pRecs.PlayOn.PONoteText, pRecs.PlayOn.PONoteColor, pRecs.PlayOn.POSubEnglish, pRecs.PlayOn.POSubForeign, pRecs.PlayOn.POAudioEnglish, pRecs.PlayOn.POAudioForeign, pRecs.Purchase.PurchaseInfo, pRecs.Purchase.PurchaseVarified, pRecs.Purchase.PurchasePartOfCollection, pRecs.Purchase.PurchaseCollectionName, pRecs.Purchase.PurchaseDVDs, pRecs.Purchase.PurchaseDrawer, pRecs.Purchase.PurchaseErrors, pRecs.Purchase.PurchaseMissing, pRecs.Purchase.PurchaseNoteText, pRecs.Word.WordInfo, pRecs.Word.WordSpecialNote, pRecs.Word.WordSpecialColor, pRecs.HardDrive.HDInfo, pRecs.HardDrive.HDReplace, pRecs.HardDrive.HDMissing, pRecs.HardDrive.HDErrors, pRecs.HardDrive.HDTextFound, pRecs.HardDrive.HDReviewed, pRecs.HardDrive.HDResolution, pRecs.HardDrive.HDQuality, pRecs.HardDrive.HDLabel, pRecs.HardDrive.HDFSize, pRecs.HardDrive.HDFPath, pRecs.HardDrive.HDFName, pRecs.HardDrive.HDSubEnglish, pRecs.HardDrive.HDSubForeign, pRecs.HardDrive.HDAudioEnglish, pRecs.HardDrive.HDAudioForeign) Validate() BindingSource.EndEdit() RecsTableAdapter.Update(VideoDBDataSet.Recs) TableAdapterManager.UpdateAll(VideoDBDataSet) VideoDBDataSet.AcceptChanges() End Sub
排查与解决方法
- 匹配Insert参数与表结构
核对RecsTableAdapter.Insert的参数顺序、数据类型是否和Recs表的字段完全一致,重点检查必填字段是否都传入有效值,CoverBytes对应的字段是否为VARBINARY(MAX)或支持二进制数据的类型。 - 移除冗余的Update调用
TableAdapter.Insert是直接向数据库提交数据的操作,不需要再调用RecsTableAdapter.Update和TableAdapterManager.UpdateAll——这两个方法是用来同步DataSet中已修改的行,和Insert逻辑无关,冗余调用可能引发冲突,建议删除这些代码。 - 添加异常捕获定位问题
用Try-Catch包裹Insert代码,直接获取错误信息,这是最快找到问题的方式:Private Sub FillNewDatabase() Dim CoverBytes() As Byte ReDim CoverBytes(0) Call GetCoverBytes(pRecs.Common.CoverPath, CoverBytes) Try RecsTableAdapter.Insert(pRecs.Common.Title, ' 省略所有参数... pRecs.HardDrive.HDAudioForeign) VideoDBDataSet.Recs.AcceptChanges() ' 同步DataSet状态(可选) Catch ex As Exception MsgBox("插入失败:" & ex.Message & vbCrLf & ex.StackTrace) End Try End Sub - 验证连接与权限
确认RecsTableAdapter的连接字符串指向正确的数据库,当前程序对该数据库有写入权限。 - 检查CoverBytes数据
确认GetCoverBytes正确读取了JPG文件,没有返回空数组或损坏的字节数据——二进制字段无效会直接导致插入失败。
内容的提问来源于stack exchange,提问作者Lance Summers
相关产品推荐
相关产品推荐

