【问题标题】:Adding Records to MS Access database with VB.Net使用 VB.Net 将记录添加到 MS Access 数据库
【发布时间】:2012-08-04 02:16:25
【问题描述】:

我正在编写一个简单的应用程序来将记录添加到由 MS Access 提供支持的数据库中。以下是我的代码示例。我不确定为什么它不起作用。有什么建议吗?

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim dsTable As DataTable
        Dim dsNewRow As DataRow

        dsTable = New DataTable("Customers")
        dsNewRow = dsTable.NewRow()
        CustomersDataSet.Customers.Rows.Add(NameTextBox.Text, AddrTextBox.Text, ZipTextBox.Text, "", "", BalanceTextBox.Text, CreditLimitTextBox.Text, StatusTextBox.Text)


    End Sub

这是我得到的异常的副本:

System.Data.ConstraintException was unhandled
  Message=Column 'Name' is constrained to be unique.  Value 'd' is already present.
  Source=System.Data
  StackTrace:
       at System.Data.UniqueConstraint.CheckConstraint(DataRow row, DataRowAction action)
       at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent)
       at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Int32 position, Boolean fireEvent, Exception& deferredException)
       at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent)
       at System.Data.DataRowCollection.Add(Object[] values)
       at IS349_FP_Hill.addCustomer.Button1_Click(Object sender, EventArgs e) in C:\Users\Monty\Documents\IS349-FP-Hill\IS349-FP-Hill\IS349-FP-Hill\addCustomer.vb:line 19
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at IS349_FP_Hill.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

【问题讨论】:

  • 您的数据表没有任何定义的列,所以我怀疑Add 会失败。
  • 嗨,Mogul,请附上您收到的任何错误消息。谢谢
  • 错误表明该值已存在于您的表中。该列Name 只能采用一个唯一值。如果John 在表中,则不能再次添加John。错误居然说Value "d"
  • 这不是真的,因为当我打开数据库时,“John”不在那里。 @codingbiz
  • 您有“”、“”作为要添加的字段。 MS Access 通常不设置为允许字段(列)中的零长度字符串,包含它们的查询会失败。

标签: vb.net visual-studio exception ms-access


【解决方案1】:

错误告诉你出了什么问题:

Message=Column 'Name' is constrained to be unique.  Value 'd' is already present.

这意味着两件事:

  1. 该列设置为只有唯一值(因此您不能有 2 行在它们的每个 Name 字段中具有相同的数据)
  2. d 已存储在某处的 Name 列中,并且唯一约束不允许您再次添加它。

您必须弄清楚如何删除列上的唯一约束。我不使用 MS Access,所以我帮不上什么忙,但这应该可以帮助您入门:

http://msdn.microsoft.com/en-us/library/office/bb177883(v=office.12).aspx

我建议您首先列出约束或获取完整的表架构 (check here)...

希望这会有所帮助!

祝你好运

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多