错误:EXECUTENONQUERY在C#中返回-1值? [英] ERROR: EXECUTENONQUERY return -1 value in C# ?
问题描述
以下是我的方法 - >
<前lang =cs> public static bool SaveXmlInfo(Sales1 sales1)
{
int recordsAffected = 0 ;
XElement planPropertyElement = XElement.Parse(orderXml);
使用(SqlConnection conn = new SqlConnection(ConnectionString1))
{
conn.Open();
SqlParameter [] parms = new SqlParameter [ 2 ];
parms [ 0 ] = new SqlParameter( @ RCustId,sales1.RCustId);
parms [ 1 ] = new SqlParameter( @ SesId,sales1.SesId);
parms [ 2 ] = new SqlParameter
{ParameterName = @ CenXml,
SqlDbType = SqlDbType.Xml,
Value = new SqlXml(planPropertyElement.CreateReader())
};
recordsAffected = SqlHelper.ExecuteNonQuery(conn,CommandType.StoredProcedure, usp_PurchXml,parms) ;
salesPurchase = null ;
}
返回 recordsAffected > 0 ? true : false ;
}
在我的存储过程中,我正在检查SesId是否可用。如果它可用,那么我正在更新记录,否则我将新记录插入表中。
以下是我的存储过程
IF EXISTS ( SELECT * FROM Cust1 WITH ( NOLOCK ) WHERE Cust1.SesId =@SesId)
BEGIN
更新 Cust1
SET Cust1.OrderXml=@CenXml
FROM Cust1 INNER JOIN Cust ON Cust1.CustomerId = Cust.RepCustId
WHERE < /跨度> Cust1.CustomerId=@RCustId
END
ELSE
BEGIN
INSERT INTO Cust1(CustomerId ,SessionId,OrderDateTime,colXml)
VALUES ( @ RCustId , @ SesId ,GETDATE(), @ CenXml );
END
错误:
每次变量记录受影响设置为 - 1如果已进行任何交易,我想将其设置为1。
添加代码块,大喊删除[/编辑]
< blockquote>
看看这里:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspxtarget =_ blanktitle =New Window> ^ ]
对于UPDATE,INSERT和DELETE语句,返回值是数字受命令影响的行。当插入或更新的表上存在触发器时,返回值包括插入或更新操作影响的行数以及受触发器或触发器影响的行数。 对于所有其他类型的语句,返回值为-1。如果发生回滚,则返回值也为-1。
存储过程是另一种类型的语句,所以返回值为-1。
你可以添加一个简单的IF @@ ERROR <> 0 BEGIN RETURN @@ ERROR END ELSE RETURN 1语句在成功时强制返回1.
Hi,
Below are my method-->
public static bool SaveXmlInfo(Sales1 sales1)
{
int recordsAffected = 0;
XElement planPropertyElement = XElement.Parse(orderXml);
using (SqlConnection conn = new SqlConnection(ConnectionString1))
{
conn.Open();
SqlParameter[] parms = new SqlParameter[2];
parms[0] = new SqlParameter("@RCustId", sales1.RCustId);
parms[1] = new SqlParameter("@SesId", sales1.SesId);
parms[2] = new SqlParameter
{ ParameterName = "@CenXml",
SqlDbType = SqlDbType.Xml,
Value = new SqlXml(planPropertyElement.CreateReader())
};
recordsAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "usp_PurchXml", parms);
salesPurchase = null;
}
return recordsAffected > 0 ? true : false;
}
In my Stored Procedure, I am checking SesId available or not.If it is available then i am updating record else I am inserting new record into table.
Below are my Stored Procedure
IF EXISTS(SELECT * FROM Cust1 WITH(NOLOCK) WHERE Cust1.SesId=@SesId)
BEGIN
UPDATE Cust1
SET Cust1.OrderXml=@CenXml
FROM Cust1 INNER JOIN Cust ON Cust1.CustomerId=Cust.RepCustId
WHERE Cust1.CustomerId=@RCustId
END
ELSE
BEGIN
INSERT INTO Cust1(CustomerId,SessionId,OrderDateTime,colXml)
VALUES(@RCustId,@SesId,GETDATE(),@CenXml);
END
Error:
Everytime variable recordsAffected set to -1 I want to set it 1 if any transaction has been made.
[Edit]Code block added, shouting removed[/Edit]
Hi,
Have a look here:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx[^]
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
A Stored Procedure is another type of statement, so the return value is -1.
You could add a simpleIF @@ERROR <> 0 BEGIN RETURN @@ERROR END ELSE RETURN 1statement to force a return of 1 when successful.
这篇关于错误:EXECUTENONQUERY在C#中返回-1值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!