VB.NET / Access DB - 在运行时创建的列未更新
感谢大家拒绝我的问题而不是帮助我。这个网站太棒了。
我的程序使用以下代码在运行时创建列:
Cmd = New OleDb.OleDbCommand("ALTER TABLE [Parent] ADD [" & ColumnDate & "] int", con)
objCmd = New OleDb.OleDbCommand("ALTER TABLE [Parent] ADD [" & ColumnDate & "] int", con)
objCmd.ExecuteNonQuery()
我使用以下代码将数据添加到新插入的列中:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(ColumnDate) = Hours * Num
da.Update(ds, "SchoolMaticsDatabase")
以上所有工作正常;当我尝试编辑最初放置在新添加的列中的信息时,就会出现问题。这些是我采取的方法。 (它们都没有给出错误消息;它根本不会在数据库内更新。)
方法 1:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For Each column As DataColumn In ds.Tables("SchoolMaticsDatabase").Columns
If IsDate(column.ColumnName) = True Then
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(column.ColumnName) = DataGridView3.Item(column.ColumnName, 0).Value
End If
Next
da.Update(ds, "SchoolMaticsDatabase")
方法 2:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For count = 13 To MaxColumns - 1
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(count) = DataGridView3.Item(count, 0).Value
Next
da.Update(ds, "SchoolMaticsDatabase")
方法 3:
For Each column As DataColumn In ds.Tables("SchoolMaticsDatabase").Columns
If IsDate(column.ColumnName) Then
Cmd = New OleDb.OleDbCommand("UPDATE [Parent] SET [" & column.ColumnName & "]=" & DataGridView3.Item(column.ColumnName, 0).Value & " WHERE [ID]=" & inc + 1, con)
objCmd = New OleDb.OleDbCommand("UPDATE [Parent] SET [" & column.ColumnName & "]=" & DataGridView3.Item(column.ColumnName, 0).Value & " WHERE [ID]=" & inc + 1, con)
objCmd.ExecuteNonQuery()
End If
Next
我通过打开 Access 数据库手动向表添加一列,并且所有上述方法都可以进行编辑存储在该列中的数据。所以我相信这与列是在运行时创建的这一事实有关。
Thanks everyone for thumbing down my question and not helping me out. This website is amazing.
My program creates columns at runtime with the following code:
Cmd = New OleDb.OleDbCommand("ALTER TABLE [Parent] ADD [" & ColumnDate & "] int", con)
objCmd = New OleDb.OleDbCommand("ALTER TABLE [Parent] ADD [" & ColumnDate & "] int", con)
objCmd.ExecuteNonQuery()
I add data into the newly inserted column with the following code:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(ColumnDate) = Hours * Num
da.Update(ds, "SchoolMaticsDatabase")
All of the above works fine; the issue arises when I try to edit the information originally placed in the newly added column. These are the approaches that I have taken. (None of them give an error message; it simply won't update within the database.)
Approach 1:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For Each column As DataColumn In ds.Tables("SchoolMaticsDatabase").Columns
If IsDate(column.ColumnName) = True Then
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(column.ColumnName) = DataGridView3.Item(column.ColumnName, 0).Value
End If
Next
da.Update(ds, "SchoolMaticsDatabase")
Approach 2:
da.SelectCommand = New OleDb.OleDbCommand(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For count = 13 To MaxColumns - 1
ds.Tables("SchoolMaticsDatabase").Rows(inc).Item(count) = DataGridView3.Item(count, 0).Value
Next
da.Update(ds, "SchoolMaticsDatabase")
Approach 3:
For Each column As DataColumn In ds.Tables("SchoolMaticsDatabase").Columns
If IsDate(column.ColumnName) Then
Cmd = New OleDb.OleDbCommand("UPDATE [Parent] SET [" & column.ColumnName & "]=" & DataGridView3.Item(column.ColumnName, 0).Value & " WHERE [ID]=" & inc + 1, con)
objCmd = New OleDb.OleDbCommand("UPDATE [Parent] SET [" & column.ColumnName & "]=" & DataGridView3.Item(column.ColumnName, 0).Value & " WHERE [ID]=" & inc + 1, con)
objCmd.ExecuteNonQuery()
End If
Next
I added a column to the table manually via opening the access database and all the above approaches work for editing data stored in that column. So I believe it is something to do with the fact that the columns are created at run time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您的数据集(ds)不同步。
首先,确认数据集中存在新列:对于三种方法中的任何一种,在循环开始之前放置一个断点,然后查看 ds.Tables("SchoolMaticsDatabase").Columns 并确认新列实际上已列在那里。或者,将 Debug.Print column.ColumnName 放入循环内并在“输出”窗口中查找它。
其次,假设新专栏位于专栏成员中,我建议您另外做一个小项目来进一步探讨您的问题。让它变得简单,没有什么花哨的。让它创建一个列(首先避免使用日期作为名称),给它一个值,更新它的值,看看它怎么样。
祝你好运!
I suspect that your DataSet (ds) is out of sync.
First, confirm that the new column is present within the DataSet: For any one of your three approaches, put a break-point just before the loop starts, and take a look at ds.Tables("SchoolMaticsDatabase").Columns and confirm that the new column is in fact listed there. Alternatively, put a
Debug.Print column.ColumnName
inside the loop and look for it in the Output window.Second, assuming that the new column is in the Columns member, I would recommend making a little project on the side to explore your issue further. Make it simple, nothing fancy. Let it create a column (avoid using dates as names at first), give it a value, update its value, see how it goes.
Good luck!