使用VB.Net将记录添加到MS Access数据库

问题描述:

我在写一个简单的应用程序来将记录添加到由MS Access支持的数据库。以下是我的代码示例。我不确定它为什么不起作用。有什么建议么?使用VB.Net将记录添加到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: 
+1

您的数据表没有任何定义的列,所以我怀疑'Add'会失败。 – vcsjones 2012-08-04 02:19:23

+0

嗨Mogul,请包括您收到的任何错误消息。谢谢 – jmort253 2012-08-04 02:19:48

+0

加@ jmort253 – TheMogul 2012-08-04 02:30:22

的这个错误告诉你什么是错的:

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

这意味着两件事情:

  1. 列被设置为只有唯一值(所以你不能有2行t帽子在它们的每个Name字段中都有相同的数据)
  2. d已存储在某个地方的Name列中,并且该唯一约束不允许再次添加它。

你必须弄清楚如何删除列上的唯一约束。我不同意的MS Access工作,所以我不能帮助不大有,但这应该让你开始:

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

我建议你上列出约束或快满表模式(check here)第一工作...

希望这有助于!

好运