用 CommandBuilder.GetUpdateCommand()方法自动获得 更新的 SQL语句,前提是你的表必须要有主键但我可以确保我的表有\且只有一个主键, 为什么调用这条语句为出现
"沒有傳回任何索引鍵資料行資訊的 SelectCommand 不支援 UpdateCommand 的動態 SQL 產生"
这样的错误!如果我将表名更换为另一个表, 则可以正常得到. (我仔细比较这两个表, 没有发现不一样的地方都是有一个主键)

解决方案 »

  1.   

    Dim selectstr As String
            Dim cnn As SqlConnection
            Dim sqldataset As DataSet
            Dim sqlc As SqlCommand
            Dim sqla As SqlDataAdapter
            Dim sqlb As SqlCommandBuilder        cnn = New SqlConnection
            sqldataset = New DataSet
            cnn.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")        selectstr = "SELECT timetext FROM timetab"
            sqlc = New SqlCommand(selectstr, cnn)
            sqla = New SqlDataAdapter        sqla.SelectCommand = sqlc
            sqlb = New SqlCommandBuilder(sqla)
            sqla.Fill(sqldataset)        sqldataset.Tables(0).Rows(0).Delete()
            sqla.Update(sqldataset)
      

  2.   

    SelectCommand跟你所用的表有没有字段不一样的地方?
      

  3.   

    TO: 楼上各位 
    我用的是 Oracle 数据库, 所以用的是 OleDbDataAdapter
    打开表格用的是 "Select * from AdjustH"
    查阅: OleDbDataAdapter.SelectCommand.CommandText也是"Select * from AdjustH"
    可以肯定AdjustH表中有一个主键, 但还是出错如果打开表格时将 AdjustH 换成 "Select * from Supy", 则一切正常
      

  4.   

    请确定AdjustH表是否和其他的表有关联,DataAdapter不支持有关联的表自动产生UpdateCommand。
      

  5.   

    TO:  2002pine(我爱你江南的雪儿)
    因为我把所有数据库的操作全部封装在一个类中, 所以代码很多, 不好贴TO:  cuike519(marshal(修练中...)) 
    AdjustH的确和其它的表有关联, 但我上表面提到的 Supy表也是有关联的
      

  6.   

    你跟踪一下 看看那个adpter 自动产生得 UpdateCommand 的commandtext是什么?  贴出来看看。
      

  7.   

    Limitations of Automatic Command Generation Logic
    The following limitations apply to automatic command generation.Unrelated Tables Only
    The automatic command generation logic generates INSERT, UPDATE, or DELETE statements for standalone tables without taking into account any relationships to other tables at the data source. As a result you may encounter a failure when calling Update to submit changes for a column that participates in a foreign key constraint in the database. To avoid this exception, do not use the CommandBuilder for updating columns involved in a foreign key constraint and instead, explicitly specify the statements used to perform the operation.Table and Column Names
    Automatic command generation logic fails if column names or table names contain any special characters, such as spaces, periods, quotation s, or other nonalphanumeric characters, even if delimited by brackets. Fully qualified table names in the form of catalog.schema.table are supported.
      

  8.   

    TO: bruce007(蜀人)
    执行到 GetUpdateCommand();就产生异常了, 所以得不到值
    TO: cuike519(marshal(修练中...)) 
    刚才仔细检查了一下: 
    AdjustH表 没有 外键
    AdjustB表 有三个外键
    Supy表 有三个外键结果: AdjustH, AdjustB异常 
          Supy通过而且: 根据 MSDN的解释, 
    As a result you may encounter a failure when calling Update to submit changes for a column that participates in a foreign key constraint in the database.
    如果数据表有外键, 那也只是在 执行 Update()更新时才会捕获导常, 不是像我这样
    GetUpdateCommand()时就产生异常