我正在编写一个简单的应用程序来将记录添加到由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:
答案 0 :(得分:0)
错误告诉你错误:
Message=Column 'Name' is constrained to be unique. Value 'd' is already present.
这意味着两件事:
Name
个字段中拥有2行具有相同数据的行d
已存储在某个地方的Name
列中,并且唯一约束不允许您再次添加。您必须弄清楚如何删除列上的唯一约束。我不使用MS Access,所以我在那里帮不了多少,但这应该让你开始:
http://msdn.microsoft.com/en-us/library/office/bb177883(v=office.12).aspx
我建议您首先列出约束或获取完整的表架构(check here)...
希望这有帮助!
祝你好运