问题描述
如何在下列指定的代码中插入为SqlDataAdapter定义更新逻辑?Dimstrconn,strsqlAsStringstrconn="datasource=26188CB0FE2A46E;"&"initialcatalog=northwind;trusted_connection=yes;"strsql="selectorderid,productid,quantity,unitprice"&"from[orderdetails]whereorderid=@orderid"&"orderbyproductid"DimtblAsNewDataTableDimcnAsNewSqlClient.SqlConnection(strconn)DimdaAsNewSqlClient.SqlDataAdapter(strsql,cn)da.SelectCommand.Parameters.AddWithValue("@orderid",10503)'--------------------'为SqlDataAdapter定义更新逻辑--------------------------'---------------------cn.Open()da.Fill(tbl)tbl.Rows(0).Delete()tbl.Rows(1)("quantity")=CShort(tbl.Rows(1)("quantity"))*2tbl.Rows.Add(NewObject(){10503,1,24,18})UsingtxnAsSqlClient.SqlTransaction=cn.BeginTransactionda.UpdateCommand.Transaction=txnda.InsertCommand.Transaction=txnda.DeleteCommand.Transaction=txnda.Update(tbl)txn.Commit()EndUsingcn.Close()
解决方案
解决方案二:
在da.Update(tbl)前定义UpdateCommand的参数,具体需要更新哪几个字段da.UpdateCommand.Connection=cnda.UpdateCommand.CommandText="UPDATE的SQL语句"Dimparam1AsSqlClient.SqlParameter=NewSqlClient.SqlParameter("@column1",System.Data.SqlDbType.NVarChar,200,"ZJ_column1")param1.SourceVersion=DataRowVersion.Originalparam1.Direction=ParameterDirection.Inputda.UpdateCommand.Parameters.Add(param5)