如何解决插入查询输出 [英] how to tackle insert query out put

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

问题描述

IF (@Mode='Insert')
Begin
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName) 
Insert into tblProduct
(
ProductName ,
ProductCode )
Values
(

@ProductName ,
@ProductCode )



这是我现在使用C#的插入代码我想要解决我的意思是代码插入或不插入,即我想解决这三件事

1.代码插入成功

2.产品名称已经存在

3.发生sql异常错误



而我的C#代码是


this is my insert code now using C# i want to tackle the out put i mean that code is inserted or not i.e., i want to tackle these three thing
1. code insert succefully
2. Product name already exsit
3. sql exception error occured

while my C# code is

int output= objProduct.Insert()



输出变量将根据上述三个条件返回的内容


what will output variable will return in according to above three condition

推荐答案

查询这样的查询是否通过消息查看数据是否已插入

Make query like this see data inserted or not by message
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName)
 begin Insert into tblProduct
( ProductName , ProductCode ) Values ( @ProductName , @ProductCode )
Select 'Data Inserted Successfully' end
else
Select 'Data already exists'


For 1和2:


$ C $ b in C#



For 1 and 2:

in C#

int rowCount = cmd.ExecuteNonQuery()
if rowCount >0 
{
Record is inserted
}
else
{
	Record already exist.
}





3:







For 3:


Declare @Error VARCHAR(1000) output
Begin Try
Begin Transaction
IF (@Mode='Insert')
Begin
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName) 
Insert into tblProduct
(
ProductName ,
ProductCode )
Values
(
 
@ProductName ,
@ProductCode )
Commit

End Try

Begin Catch
SET @Error=(SELECT ERROR_MESSAGE())
ROLLBACK TRANSACTION
RAISERROR(@Error,16,1)
End Catch


这篇关于如何解决插入查询输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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