I am using Visual Studio 2013 in C#. I am trying to use SqlCommand
to insert data similar to stored procedures. The code below works if I substitute the values with real data instead of variables but when I try to use variables I get this error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.SqlServer.dll
Additional information: Must declare the scalar variable "@cashmemo".
This is my code:
dbContext.Database.ExecuteSqlCommand("INSERT INTO CashAccounts(CashDate,CashMemo,CashDebit,CashCredit)VALUES(Getdate(),@cashmemo,@cashcredit,@cashdebit)");
SqlCommand sqlCom = new SqlCommand();
sqlCom.Parameters.Add("@cashmemo", SqlDbType.NVarChar);
sqlCom.Parameters.Add("@cashcredit", SqlDbType.Decimal);
sqlCom.Parameters.Add("@cashdebit", SqlDbType.Decimal);
sqlCom.ExecuteNonQuery();