插入后的SQL更新 [英] Sql Update after insert

查看:94
本文介绍了插入后的SQL更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在INSERT SQL命令之后发出更新.更新的目的是将自动编号ID字段添加到年份并将其存储为文本.

这是INSERT SQL with SELECT SCOPE_IDENTITY()语句之后的代码的一部分(效果很好):


I am trying to issue an Update after a INSERT SQL command. The purpose behind the update is to add the auto number ID field to a year and store it as text.

Here is part of my code after the INSERT SQL with SELECT SCOPE_IDENTITY() statement (which works fine):


identity = Integer.Parse(SQLCmd.ExecuteScalar().ToString())
objConnection.Close()
Dim txtAssessmentID As String
'' Set Assessment ID
txtAssessmentID = identity.ToString
txtAssessmentID = Year(Today) & "-" & txtAssessmentID.PadLeft(5, "0"c)
strSQL = "UPDATE tblAssessment SET assessmentID = @assessmentID WHERE ID = " & identity
SQLCmd = New SqlCommand() ''The SQL Command
objConnection.Open() ''Open the connection
SQLCmd.Connection = objConnection ''Sets the Connection to use with the SQL Command
SQLCmd.CommandText = strSQL ''Sets the SQL String
SQLCmd.Parameters.AddWithValue("@assessmentID", txtAssessmentID)
Try
    objCommand.ExecuteNonQuery()
    objConnection.Close()
    '' Load Personell Evaluations
Catch ex As Exception
    lblAlert.Text = "Problem updaaating record"
End Try




任何帮助将不胜感激.




Any help would be appreciated.

推荐答案

为什么要使用objCommand对象,而应该使用SQLCmd对象. 这可能是这里的问题...请清楚地说明问题,我认为这是这里唯一的问题.
Why are you using the object objCommand, you should be using SQLCmd object.
That could be the problem here...please state the question clearly, I see this is the only issue here.


这篇关于插入后的SQL更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆