SQL Server命令在vb.net中不起作用,导致所有在一句话中

时间:2022-06-06 23:10:38

This is propably a very easy question.

这可能是一个非常简单的问题。

But i cant fix it. I try an update if @ID = column id, else insert all.

但我不能修好它。我尝试更新@ID =列id,否则插入全部。

Only this one doesn't work if i put it all in one sentence

如果我把它全部放在一个句子里,这个就不行。

Cmd.CommandText = "UPDATE Locatie SET (Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp) WHERE ID=@ID IF @@ROWCOUNT=0 INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp)"

2 个解决方案

#1


4  

This should work just fine

这应该没问题

Cmd.CommandText = "IF EXISTS (SELECT 1 FROM Locatie WHERE Id = @Id) " & Environment.NewLine & _
                  "  BEGIN UPDATE Locatie SET Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp WHERE ID=@Id END " & Environment.NewLine & _
                  "ELSE " & Environment.NewLine & _
                  "   BEGIN INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) END "

Although you are probably better off (for maintainability) using a stored procedure.

尽管使用存储过程(对于可维护性)可能更好。

#2


1  

try the below:

试试以下:

Cmd.CommandText = "UPDATE Locatie SET (Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp) WHERE ID=@ID GO IF @@ROWCOUNT=0 begin INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) end Go"

#1


4  

This should work just fine

这应该没问题

Cmd.CommandText = "IF EXISTS (SELECT 1 FROM Locatie WHERE Id = @Id) " & Environment.NewLine & _
                  "  BEGIN UPDATE Locatie SET Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp WHERE ID=@Id END " & Environment.NewLine & _
                  "ELSE " & Environment.NewLine & _
                  "   BEGIN INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) END "

Although you are probably better off (for maintainability) using a stored procedure.

尽管使用存储过程(对于可维护性)可能更好。

#2


1  

try the below:

试试以下:

Cmd.CommandText = "UPDATE Locatie SET (Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp) WHERE ID=@ID GO IF @@ROWCOUNT=0 begin INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) end Go"