如何在C#的bool方法中调用存储过程 [英] How to call stored procedure in bool method of c#
本文介绍了如何在C#的bool方法中调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在bool方法中使用所需数量的参数调用存储过程
我创建了带有参数的存储过程,例如
How to call stored procedure with required number of parameters in bool method
I have created stored procedure with parameters like
CREATE PROCEDURE [dbo].[sp_UPerson]
(
@PersonID INT,
@PersonEmailID varchar(50),
@PhoneNumber varchar(50)=null,
@Source varchar(50)=null
)
在bool方法中
in bool method
public bool GetPerson(int PersonID,string PersonEmailID )
{
try
{
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "sp_UPerson";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PersonID", PersonID);
cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
cmd.ExecuteNonQuery()
}
catch
{
}
finally
{
conn.Close();
}
}
推荐答案
嗨普拉萨纳拉,
您应该像下面这样更改您的声明,
Hi Prasannala,
You should change you statement as like below,
public bool GetPerson(int PersonID,string PersonEmailID )
{
try
{
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "sp_UPerson";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PersonID", PersonID);
cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
cmd.ExecuteNonQuery();
return true; }
catch
{
return false; }
finally
{
conn.Close();
}
}
cmd.ExecuteNonQuery()方法返回整数,该整数受行数的影响.
因此,如果count大于0,则应该返回true,否则返回false.
显示错误并不能获取所有路径值
为避免出现上述错误,应在catch块中返回false.
Hi,
cmd.ExecuteNonQuery() method returns integer which its affected by number of rows.
So you should be return true, if count is greater than 0 otherwise returns false.
it showing error does not get all path value
For avoid this above error, you should return false in catch block.
您可以尝试
You can try
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "sp_UPerson";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PersonID", PersonID);
cmd.Parameters.AddWithValue("@PersonEmailID ", PersonEmailID );
if( cmd.ExecuteNonQuery() > 0)
{
return true;
}
else
{
return false;
}
这篇关于如何在C#的bool方法中调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文