我使用C#winform创建了数据更新表单。我需要添加如果数据不存在插入数据。如果存在则检查已插入的数据库显示消息数据。 这是我的代码和winfirm图像。
请帮我这样做。
我尝试使用IF EXISTS(SELECT * FROM WHERE),但它没有正常工作
当我添加if Exists代码
string sql = IF NOT EXISTS (SELECT * FROM UserInfo WHERE User_Name = @Name)
BEGIN
INSERT INTO UserInfo (User_Name, Email,EPF_Number, Department, Laptop_Model, Laptop_Serial, Issu_Date,Hostname, Image) VALUES ( @Name,@Email, @EPFNumber, @DPT, @Model,@Serial,@Date, @Host, @Photo)
END
显示以下错误
答案 0 :(得分:0)
这不是正确的语法。试试这个。
IF NOT EXISTS (SELECT *
FROM UserInfo
WHERE User_Name = @Name
AND Email = @Email
AND EPF_Number = @EPFNumber
AND Department = @DPT
AND Laptop_Model = @Model
AND Laptop_Serial = @Serial
AND Issu_Date = @Date
AND Hostname = @Host
AND Image = @Photo)
BEGIN
INSERT INTO UserInfo
(User_Name,
Email,
EPF_Number,
Department,
Laptop_Model,
Laptop_Serial,
Issu_Date,
Hostname,
Image)
VALUES ( @Name,
@Email,
@EPFNumber,
@DPT,
@Model,
@Serial,
@Date,
@Host,
@Photo)
END