我已经解决了这个问题,但无法确定我的更新失败的原因。我对SQL并不陌生。我写了一个简单的存储过程。
以下是代码:
CREATE PROCEDURE [dbo].sp_UpdateTeamMemberProfile
@TeamMemberId int OUTPUT,
@FirstName varchar(25),
@LastName varchar(30),
@ContactNumber varchar (20),
@ContactExt varchar (20) ,
@EmailAddress varchar(100),
@Fax varchar (25),
@LocationCity varchar (20),
@LocationState varchar (20),
@BeginWorkHrs int,
@BeginWorkTime varchar (20) ,
@EndWorkHrs int ,
@EndWorkTime varchar (20) ,
@ZONE Varchar (20),
@TeamMemberLanId varchar (20),
@ManagerId varchar (20),
@UpdateDate datetime
AS
BEGIN
SET NOCOUNT ON;
BEGIN
UPDATE dbo.LoanOwnerStamp
SET FirstName = @FirstName,
LastName = @LastName,
ContactNumber = @ContactNumber,
ContactExt = @ContactExt ,
EmailAddress = @EmailAddress,
Fax = @Fax,
LocationCity = @LocationCity,
LocationState = @LocationState,
BeginWorkHrs = @BeginWorkHrs,
BeginWorkTime = @BeginWorkTime,
EndWorkHrs = @EndWorkHrs,
EndWorkTime = @EndWorkTime,
Zone = @Zone,
TeamMemberLanId = @TeamMemberLanId,
ManagerId = @ManagerId ,
UpdatedDate = @UpdateDate
WHERE
TeamMemberLanId = @TeamMemberLanId
AND TeamMemberId = @TeamMemberId
END
SET @TeamMemberId = SCOPE_IDENTITY()
RETURN @TeamMemberId
END
现在背后的代码是......
using (SqlCommand updatetLSTeamMember = new SqlCommand("sp_UpdateTeamMemberProfile", myConnection))
{
updatetLSTeamMember.CommandType = CommandType.StoredProcedure;
updatetLSTeamMember.Parameters.Add("@FirstName", SqlDbType.VarChar, 25).Value = txtFirstName.Text;
updatetLSTeamMember.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value = txtLastName.Text;
updatetLSTeamMember.Parameters.Add("@ContactNumber", SqlDbType.VarChar, 20).Value = txtContactNumber.Text;
updatetLSTeamMember.Parameters.Add("@ContactExt", SqlDbType.VarChar, 20).Value = txtContactExt.Text;
updatetLSTeamMember.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 100).Value = txtEmailAddress.Text;
updatetLSTeamMember.Parameters.Add("@Fax", SqlDbType.VarChar, 25).Value = txtFax.Text;
updatetLSTeamMember.Parameters.Add("@LocationCity", SqlDbType.VarChar, 20).Value = txtLocationCity.Text;
updatetLSTeamMember.Parameters.Add("@LocationState", SqlDbType.VarChar, 20).Value = txtState.Text;
updatetLSTeamMember.Parameters.Add("@BeginWorkHrs", SqlDbType.Int).Value = ddlBeginHrs.SelectedValue.ToString();
updatetLSTeamMember.Parameters.Add("@BeginWorkTime", SqlDbType.VarChar, 20).Value = ddlBeginTime.SelectedValue.ToString();
updatetLSTeamMember.Parameters.Add("@EndWorkHrs", SqlDbType.Int).Value = ddlEndHrs.SelectedValue.ToString();
updatetLSTeamMember.Parameters.Add("@EndWorkTime", SqlDbType.VarChar, 20).Value = ddlEndTime.SelectedValue.ToString();
updatetLSTeamMember.Parameters.Add("@Zone", SqlDbType.VarChar, 20).Value = ddlZone.SelectedItem.Text;
updatetLSTeamMember.Parameters.Add("@TeamMemberLanId", SqlDbType.VarChar, 20).Value = txtTeamMemberLaniId.Text;
updatetLSTeamMember.Parameters.Add("@ManagerId", SqlDbType.VarChar, 20).Value = gblUserId;
updatetLSTeamMember.Parameters.Add("@UpdateDate", SqlDbType.DateTime).Value = DateTime.Now.ToLocalTime();
pID = updatetLSTeamMember.Parameters.Add("@TeamMemberId", SqlDbType.Int); // I'm thinking this is a culprit
pID.Direction = ParameterDirection.Output; // are these assignment correct.
try
{
if (updatetLSTeamMember.Connection.State == ConnectionState.Closed)
{
updatetLSTeamMember.Connection.Open();
}
updatetLSTeamMember.ExecuteNonQuery(); // code bombs out here
string idcat = updatetLSTeamMember.Parameters["@TeamMemberId"].Value.ToString();
现在我的问题出现在几个地方,我的Parameter.Direction
输出是否正确,从存储过程的角度来看是否正确?
感谢任何帮助。
答案 0 :(得分:0)
如果您需要在成功的情况下返回某些内容 - 您可以使用@@ROWCOUNT
代替SCOPE_IDENTITY()
- 它将返回受更新影响的行数。
因此,如果返回0,您将知道没有更新记录。
答案 1 :(得分:0)
我可能错了,但您根据您使用TeamMemberId(输出参数)的条件更新记录,但不在c#侧为其提供任何值。输出参数的语法si corect但你在where子句中使用它,值是什么,... Scope Identity部分将在执行更新命令
后执行答案 2 :(得分:0)
如果要返回值,我建议在存储过程中创建另一个变量,而不是使用您用于输入的值。
这就是我要做的事情:
@returnValue int OUTPUT
@TeamMemberId int,
@FirstName varchar(25),
@LastName varchar(30),
@ContactNumber varchar (20),
@ContactExt varchar (20) ,
@EmailAddress varchar(100),
@Fax varchar (25),
@LocationCity varchar (20),
@LocationState varchar (20),
@BeginWorkHrs int,
@BeginWorkTime varchar (20) ,
@EndWorkHrs int ,
@EndWorkTime varchar (20) ,
@ZONE Varchar (20),
@TeamMemberLanId varchar (20),
@ManagerId varchar (20),
@UpdateDate datetime
AS
BEGIN
SET NOCOUNT ON;
UPDATE dbo.LoanOwnerStamp
SET FirstName = @FirstName,
LastName = @LastName,
ContactNumber = @ContactNumber,
ContactExt = @ContactExt ,
EmailAddress = @EmailAddress,
Fax = @Fax,
LocationCity = @LocationCity,
LocationState = @LocationState,
BeginWorkHrs = @BeginWorkHrs,
BeginWorkTime = @BeginWorkTime,
EndWorkHrs = @EndWorkHrs,
EndWorkTime = @EndWorkTime,
Zone = @Zone,
TeamMemberLanId = @TeamMemberLanId,
ManagerId = @ManagerId ,
UpdatedDate = @UpdateDate
WHERE
TeamMemberLanId = @TeamMemberLanId
AND TeamMemberId = @TeamMemberId
SET @returnValue= SCOPE_IDENTITY()
RETURN @returnValue
END
关于你背后的代码:
编辑:您还需要添加:
updatetLSTeamMember.Parameters.AddWithValue("@TeamMemberId", int.Parse(txtTeamMemberId.Text)); // or wherever you are getting the team member id from.
pID = updatetLSTeamMember.Parameters.Add("@returnValue", SqlDbType.Int);
pID.Direction = ParameterDirection.Output;
string idcat = updatetLSTeamMember.Parameters["@returnValue"].Value.ToString();
注意:我从未在更新上使用SCOPE_IDENTITY()。你可能想看一下。