• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Tony Qu
我的软件工作室
博客园    首页    新随笔    联系   管理     

关于DataAdapter的UpdateCommand语法的一个问题

我在microsoft newsgroup上发布的原文:
If I generate DataAdapter.UpdateCommand by myself, can I use the following
sql sentence as UpdateCommand

UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName,
ContactName = @ContactName, ContactTitle = @ContactTitle WHERE (CustomerID =
@Original_CustomerID) AND (CompanyName = @Original_CompanyName) AND
(ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND
ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR
@Original_ContactTitle IS NULL AND ContactTitle IS NULL)

because in DeleteCommand,the value of @Original_XXX parameters can be obtain
from database by using timestamp. But in fact, I failed to do so. The mssql
return a error says "Incorrect syntax near CustomerID". Do you know why?
Thanks for your help in advance.

回复:
If you are generating the UpdateCommand by yourself i.e. without using a
SqlCommandBuilder, make sure that you are also setting up the parameters and
mapping them to the dataset.

For e.g. you need to add the following code to Add the @CustomerID and
@Orignal_CustomerID parameters to the command

SqlParameter pcustID = new
SqlParameter("@CustomerID",SqlDbType.Varchar,50,ParameterDirection.Input,fal
se,0,0,"CustomerID",DataRowVersion.Current,null);

SqlParameter porigCustID = new
SqlParameter("@CustomerID",SqlDbType.Varchar,50,ParameterDirection.Input,fal
se,0,0,"CustomerID",DataRowVersion.Orignal,null);

dataadapter.UpdateCommand.Parameters.Add(pcustID);
dataadapter.UpdateCommand.Parameters.Add(porigCustID);

//Notice that the oringal_custID has DataRowVersion.Orignal whereas custID
has DataRowVersion.Current, this is the key difference between the
parameters. The two parameters are used for Optimistic concurrency checking.
You will need to add all the parameters in the update command in a similar
manner.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


版权声明:本文由作者Tony Qu原创, 未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
posted @ 2005-03-18 08:11  找事的狐狸  阅读(1688)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3