OleDbDataAdapter 更新Access数据库问题困扰啊
为了更新数据库,做了个测试窗体就两个按钮(显示和更新数据),一个DataGridView1,一个DataSet1,一个OleDbConnection1(连接已经设置),一...
为了更新数据库,做了个测试窗体就两个按钮(显示和更新数据),一个DataGridView1,一个DataSet1,一个OleDbConnection1(连接已经设置),一个OleDbDataAdapter1(SelectCommand已经设置,updatecmd,delcmd等已经自动生成,但发现有问题,里面有很多问号,是怎么回事啊),一个OleDbCommandBuilder1
-----------------------------------------------------
显示按钮事件代码(可以正常显示数据):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.OleDbCommandBuilder1.DataAdapter = Me.OleDbDataAdapter1
Me.OleDbDataAdapter1.Fill(Me.DataSet1, "table")
Me.DataGridView1.DataSource = Me.DataSet1.Tables(0)
End Sub
--------------------------------------------------------
更新数据按钮代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.OleDbCommandBuilder1.DataAdapter = Me.OleDbDataAdapter1
Dim myCount As Integer = Me.OleDbDataAdapter1.Update(Me.DataSet1, "table")
Me.DataGridView1.EndEdit()
Me.DataSet1.Tables(0).AcceptChanges()
MessageBox.Show("成功更新数据天数:" & myCount)
End Sub
-----------------------------------------------------
最后如果更新了几条数据的话,也可以正确的提示更新了几条数据,但就是美更新到数据库中去。
-----------------------------------------------------
另外,我发现OleDbDataAdapter1自动生成的updatecommand等sql语句很奇怪啊,有好多问号。如:
UPDATE IndexSystem
SET ID = ?, ParentLayerID = ?, IndexName = ?, ProjectID = ?, IndexLayer = ?, WeightRecommended = ?, WeightReal = ?, SessionLowerBound = ?,
SessionUpperBound = ?, IsOptimized = ?, notes = ?
WHERE (ID = ?) AND (? = 1 AND ParentLayerID IS NULL OR
ParentLayerID = ?) AND (? = 1 AND IndexName IS NULL OR
IndexName = ?) AND (? = 1 AND ProjectID IS NULL OR
ProjectID = ?) AND (? = 1 AND IndexLayer IS NULL OR
IndexLayer = ?) AND (? = 1 AND WeightRecommended IS NULL OR
WeightRecommended = ?) AND (? = 1 AND WeightReal IS NULL OR
WeightReal = ?) AND (? = 1 AND SessionLowerBound IS NULL OR
SessionLowerBound = ?) AND (? = 1 AND SessionUpperBound IS NULL OR
SessionUpperBound = ?) AND (? = 1 AND IsOptimized IS NULL OR
IsOptimized = ?)
----------------------------------------------------------
求教大家啊。。。。。。。
可急死我了。。。。。。。
表名 IndexSystem 是基本数据表,ID是主键且不是自动编号的。
在更新数据时程序也没有出异常,而且Msgbox提示更新几条数据成功啊,但其实并没有更新到数据库里去啊! 展开
-----------------------------------------------------
显示按钮事件代码(可以正常显示数据):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.OleDbCommandBuilder1.DataAdapter = Me.OleDbDataAdapter1
Me.OleDbDataAdapter1.Fill(Me.DataSet1, "table")
Me.DataGridView1.DataSource = Me.DataSet1.Tables(0)
End Sub
--------------------------------------------------------
更新数据按钮代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.OleDbCommandBuilder1.DataAdapter = Me.OleDbDataAdapter1
Dim myCount As Integer = Me.OleDbDataAdapter1.Update(Me.DataSet1, "table")
Me.DataGridView1.EndEdit()
Me.DataSet1.Tables(0).AcceptChanges()
MessageBox.Show("成功更新数据天数:" & myCount)
End Sub
-----------------------------------------------------
最后如果更新了几条数据的话,也可以正确的提示更新了几条数据,但就是美更新到数据库中去。
-----------------------------------------------------
另外,我发现OleDbDataAdapter1自动生成的updatecommand等sql语句很奇怪啊,有好多问号。如:
UPDATE IndexSystem
SET ID = ?, ParentLayerID = ?, IndexName = ?, ProjectID = ?, IndexLayer = ?, WeightRecommended = ?, WeightReal = ?, SessionLowerBound = ?,
SessionUpperBound = ?, IsOptimized = ?, notes = ?
WHERE (ID = ?) AND (? = 1 AND ParentLayerID IS NULL OR
ParentLayerID = ?) AND (? = 1 AND IndexName IS NULL OR
IndexName = ?) AND (? = 1 AND ProjectID IS NULL OR
ProjectID = ?) AND (? = 1 AND IndexLayer IS NULL OR
IndexLayer = ?) AND (? = 1 AND WeightRecommended IS NULL OR
WeightRecommended = ?) AND (? = 1 AND WeightReal IS NULL OR
WeightReal = ?) AND (? = 1 AND SessionLowerBound IS NULL OR
SessionLowerBound = ?) AND (? = 1 AND SessionUpperBound IS NULL OR
SessionUpperBound = ?) AND (? = 1 AND IsOptimized IS NULL OR
IsOptimized = ?)
----------------------------------------------------------
求教大家啊。。。。。。。
可急死我了。。。。。。。
表名 IndexSystem 是基本数据表,ID是主键且不是自动编号的。
在更新数据时程序也没有出异常,而且Msgbox提示更新几条数据成功啊,但其实并没有更新到数据库里去啊! 展开
1个回答
展开全部
Builder自动生的sql中,所有你传的变量在 Builder 的 updateText属性 中都是问号的。
Me.DataSet1.Tables(0).AcceptChanges()时,更新不进去,可能有三种原因有的列名是sql关键字:
1、Me.OleDbDataAdapter1的问题,获得数据源的适合器和更新数据的适配器不是同一个。此种情况的解决方法是:取数据、更新数据都用同一个OleDbDataAdapter
2、更新数据库时,更新的列名是数据库关键字,比如你的数据库有一个叫User的列名,如果要更新User这个字段的时候,就会报异常。此种情况的解决方法是:初使化Builder时加如下两句
theBuilder.QuotePrefix = "[";
theBuilder.QuoteSuffix = "]";
3、更新的数据类型和数据库中对应列的类型不匹配,比如你要更新的列为 [User],它的类型为int,但是你要更新的数据是string类型赋过去的,就会报异常了,此种情况解决方法是:将类型统一.
补充:你可以试着断点看看更新之前更新的数据源有没有相对更新
Me.DataSet1.Tables(0).AcceptChanges()时,更新不进去,可能有三种原因有的列名是sql关键字:
1、Me.OleDbDataAdapter1的问题,获得数据源的适合器和更新数据的适配器不是同一个。此种情况的解决方法是:取数据、更新数据都用同一个OleDbDataAdapter
2、更新数据库时,更新的列名是数据库关键字,比如你的数据库有一个叫User的列名,如果要更新User这个字段的时候,就会报异常。此种情况的解决方法是:初使化Builder时加如下两句
theBuilder.QuotePrefix = "[";
theBuilder.QuoteSuffix = "]";
3、更新的数据类型和数据库中对应列的类型不匹配,比如你要更新的列为 [User],它的类型为int,但是你要更新的数据是string类型赋过去的,就会报异常了,此种情况解决方法是:将类型统一.
补充:你可以试着断点看看更新之前更新的数据源有没有相对更新
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询