1

I have been trying to incorporate prepared statements into my application which is using EF 5.0. Kindly look at my script below.

int parameterValue = 100000;
var addParameters = new List<MySqlParameter>();
var param1 = new MySqlParameter();
param1.Direction = System.Data.ParameterDirection.Input;
param1.Value = parameterValue;
param1.ParameterName = "user_id";
addParameters.Add(param1);
db.Database.ExecuteSqlCommand(@"delete FROM tbl_user where user_id = @user_id", addParameters);

it produces this error

Exception: {"Fatal error encountered during command execution."}
InnerException: {"Parameter '@user_id' must be defined."}

so based on InnerException description i changed the parameter nae to "@user_id"

int parameterValue = 100000;
var addParameters = new List<MySqlParameter>();
var param1 = new MySqlParameter();
param1.Direction = System.Data.ParameterDirection.Input;
param1.Value = parameterValue;
param1.ParameterName = "@user_id";
addParameters.Add(param1);
db.Database.ExecuteSqlCommand(@"delete FROM tbl_user where user_id = @user_id", addParameters);

but still the same error.

What could i be doing wrong?

Thanks in advance.

============================================== Additional info

<package id="EntityFramework" version="5.0.0" targetFramework="net40" />
<package id="MySql.Data" version="6.7.4" targetFramework="net40" />
<package id="MySQL.Data.Entities" version="6.7.4.1" targetFramework="net40" />

2 Answers 2

1

Since you are using EF I think you should use something like this:

int parameterValue = 100000;
db.Database.ExecuteSqlCommand(@"delete FROM tbl_user where user_id = {0}", parameterValue);
Sign up to request clarification or add additional context in comments.

Comments

0

Let me just show you good practice code that show what you want + recommendation on resource releasing:

using (SqlCommand comm = conn.CreateCommand()) //release good practice
{
    comm.CommandText = "delete FROM tbl_user where user_id = @user_id";
    comm.CommandType = CommandType.Text; //can be skipped for your case
    comm.Parameters.Add(new SqlParameter("@user_id", SqlDbType.Int)).Value =
        parameterValue;
    comm.ExecuteNonQuery();
}

6 Comments

but im using EF, shouldnt i be using db.Database.ExecuteSqlCommand?
Well, you can safely replace SqlCommand with DbCommand. But actually if you would look at class hierarchy(msdn.microsoft.com/en-us/library/…): then you can see that SqlCommand is inherited from DbCommand. If you deal with some real abstraction - then better use DbXXX, but you explicitly specify SQL. Keep in mind explicit better than implicit
i stumbled upon a blogpost saying that i should add this in the connection string which i did and finally i wasnt encountering the errors anymore Allow User Variables=True; now my problem is that there are no errors anymore but the records do not get deleted... int parameterValue = 2; var addParameters = new List<MySqlParameter>(); var param1 = new MySqlParameter("@userid", parameterValue); addParameters.Add(param1); db.Database.ExecuteSqlCommand("delete FROM tbl_user where user_id = @userid", addParameters); db.SaveChanges(); no errors but no deletion occurs
doesnt work int parameterValue = 2; var addParameters = new List<MySqlParameter>(); var param1 = new MySqlParameter("@userid", parameterValue); addParameters.Add(param1); db.Database.ExecuteSqlCommand("delete FROM tbl_user where user_id = {0}", addParameters); db.SaveChanges();
doesnt work also <br> <br> int parameterValue = 2; var addParameters = new List<MySqlParameter>(); var param1 = new MySqlParameter("@userid", parameterValue); addParameters.Add(param1); db.Database.ExecuteSqlCommand("delete FROM tbl_user where user_id = {0}", addParameters); db.SaveChanges(); no int parameterValue = 2; var param1 = new MySqlParameter("@userid", parameterValue); db.Database.ExecuteSqlCommand("delete FROM tbl_user where user_id = {0}", param1 ); db.SaveChanges();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.