I have the following code and i would like it to be a stored procedure.
How do you create and call a stored procedure so that the following code is just like a method call.
Where are stored procedure stored are they created in Visual studio or in MS SQL Server?
using (var conn = new SqlConnection(dbConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText =
@"use [Email Database]
INSERT INTO Emails_Log
(Email_ID, e_To, e_From, e_Subject, e_Date)
VALUES
(@id, @to, @from, @subject, @date)";
// Writes to database (local) instance
cmd.Parameters.AddWithValue("@id", emailID);
cmd.Parameters.AddWithValue("@to", emailTo);
cmd.Parameters.AddWithValue("@from", emailFrom);
cmd.Parameters.AddWithValue("@subject", emailSubject);
cmd.Parameters.AddWithValue("@date", emailSentDate);
cmd.ExecuteNonQuery();
}
NEW QUESTION!
I have managed to Create a Stored Procedure thanks guys, I’m just left with one more Problem.
cmd.CommandType = CommandType.StoredProcedure;
There is a red line under the second CommandType and the error reads
The name ‘CommandType’ does not exist in the current context
Please help.
On your server Create a procedure using the code below:
after you have created the procedure, in your code try this: