1

我正在编写一个简单的应用程序来将记录添加到由 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: 
4

1 回答 1

0

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

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

我建议您首先处理列出约束或获取完整表模式(检查此处)...

希望这可以帮助!

祝你好运

于 2012-08-30T14:19:50.313 回答