问题描述
|
我想从数据库表中获取数据,对其进行修改和更新,而不必依赖于数据库提供程序和数据库结构,因为它将成为库的一部分。
我的第一次尝试如下:
\'factory defined somewhere else as DbProviderFactory
\'connection is a working DbConnection
Dim selectcmd As DbCommand = connection.CreateCommand()
Dim adapter As DbDataAdapter = factory.CreateDataAdapter()
Dim builder As DbCommandBuilder = factory.CreateCommandBuilder()
selectcmd.Connection = connection
selectcmd.CommandText = \"SELECT * FROM tbl1\"
adapter.SelectCommand = selectcmd
builder.DataAdapter = adapter
Dim ds As New DataSet
\'Fetch data
adapter.Fill(ds)
\'Change something in ds
adapter.Update(ds) \'<- exception occurs
这是行不通的,因为CommandBuilder
不会自动生成Update-
,Insert-
和DeleteCommand
,尽管文档说应该这样做。
但是即使我添加了
adapter.InsertCommand = builder.GetInsertCommand()
adapter.UpdateCommand = builder.GetUpdateCommand()
adapter.DeleteCommand = builder.GetDeleteCommand()
它不起作用。经过进一步研究,我发现生成的CommandBuilder
6毫无用处,因为它对所有ѭ10都插入了?
而不是@param1
。它为ѭ12生成了类似于INSERT INTO tbl1 (field1,field2,field3) VALUES (?,?,?)
的内容
我真的很想使用CommandBuilder,因为它很简单,并且我没有诸如ѭ13complicated之类的复杂东西。
解决方法
也许您的表包含列名,这些列名是访问中的保留字,这是:
http://support.microsoft.com/kb/248738
您应该更改列名,甚至最好使用select子句列出它们,并使用[和],类似这样
select [username],[password],[time] from tbl1