如何使用如果存在于c#中 [英] How To Use If exists in c#

查看:52
本文介绍了如何使用如果存在于c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用如果在WFA中存在



我希望当有人插入相同数据时

messagebox .show此数据存在;



存储的Proc。代码是: -







How To Use If exists in WFA

I want when anyone insert the same data
messagebox.show"This Data exist";

the stored Proc. Code is :-



create proc AddCustomer
@name varchar(50),
@address varchar(50),
@phone varchar(50),
@date datetime
as
begin
if (exists(select cphone from Customer where cphone=@phone))
begin
   print 'This Data exist'
End
else
begin
insert into Customer (cname,cadress,cphone,cdate) 
values (@name,@address,@phone,@date)
end
end

推荐答案

string something;
if(something != null)
{
//it exists
}
else
{
//not exist
}


如此创建存储过程(但按照您的喜好格式化缩进):

Create your stored procedure as so (but format the indentation as you''d like):
Create Procedure AddCustomer
	@name varchar(50),
	@address varchar(50),
	@phone varchar(50),
	@date datetime
as
begin
  Set NoCount On
	Declare 
			@CustExists 				Int

	Set @CustExists = 0 -- False
	if (exists(select cphone from Customer where cphone=@phone))
	  Set @CustExists = 1
		
	if @CustExists = 0 
		insert into Customer (cname,cadress,cphone,cdate) 
		values (@name,@address,@phone,@date)
		
	return(@CustExists)
end





然后你可以从你的C#代码执行proc,检查返回代码。

或者,您也可以使用输出参数而不是返回代码,并在调用proc后检查它。



这对您有用吗?



希望有所帮助。



Richard



Then you can execute the proc from your C# code, examining the return code.
Alternatively, you could also use an output parameter instead of the return code, and check that after calling the proc.

Does that work for you?

Hope that helps.

Richard


我认为最好检查条件从前端。停止那里的用户。



使用数据集



计算行数(如果是)> 0然后将您的消息显示为数据已存在。

否则编写插入查询或插入数据的CAll程序。
I think it is better to check the condition from the front-end. Stop the user there itself.

Use Dataset

Count rows, if it is > 0 then give your Message as "Data Already exists."
Else Write your Insert query OR CAll Procedure for Inserting Data.


这篇关于如何使用如果存在于c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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