Is there any possible way to execute a non query without having to assign it to a LINQ to SQL class?
Let's say I have this procedure and I want to INSERT only if the Town_Name (UNIQUE CONSTRAINT) is non existent. Otherwise, the UPDATE is executed. This procedure works well and does the job when executed through SQL Server. However, I can't manage to execute it through C# code.
I am trying to avoid using SQL client, as part of my coursework, my little application has to be capable of selecting, inserting / updating and deleting data using LINQ.
The approach I'm taking always results in 0 rows affected.
// Call Procedure.
db.P_SaveClient("000000001M", "Test", "Dummy", null, "+35699999999");
-- Procedure to be executed.
CREATE PROCEDURE Appointment.SaveClient
(
@id NVARCHAR(10),
@firstName NVARCHAR(35),
@lastName NVARCHAR(35),
@tel NVARCHAR(12),
@mob NVARCHAR(12)
)
AS
BEGIN
DECLARE @clientCount TINYINT
SELECT @clientCount = COUNT(Client_ID)
FROM Appointment.Client
WHERE Client_ID = @id
IF @clientCount = 0
BEGIN
INSERT INTO Appointment.Client
VALUES (
@id
, @firstName
, @lastName
, @tel
, @mob
)
END
ELSE
BEGIN
UPDATE Appointment.Client
SET Client_ID = @id
, First_Name = @firstName
, Last_Name = @lastName
, Telephone = @tel
, Mobile = @mob
END
END
Some tutorials I found:
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!UPDATEdoes nothing since it looks for the town with that town name, then "updates" it to the same name. If you want to "change the town name you need to pass in the original name or ID and the new name as separate parameters.SqlCommand, etc.) or by using theExecuteCommandmethod on the data context.