1

i have a problem with an sqlQuery/Exec. My Stored Procedure returns a list of entitys. But i need to append the parameterlist dynamicly.

        using (MyContext db = new MyContext ()) 
        {
            var queryParam = new Dictionary<String, String>();
            var arg = String.Empty;
            var argList = new List<ObjectParameter>();
            var sql = String.Empty;

            foreach(var line in param.Split(','))
            {    
                var obj = line.Split(':');
                if (obj.Length < 2)
                {
                    continue;
                }

                if (obj[1].Equals(String.Empty))
                {
                    continue;
                }

                // arg = arg + "@" + obj[0] + "=" + "'" + obj[1] + "' ";
                argList.Add(new ObjectParameter(obj[0], obj[1]));
            }

            object[] prm = argList.ToArray();

            // Stored procedure ausführen und ergebniss erhalten.
            var list = db.Database.SqlQuery<Dokument>("EXEC dbo.spDokumente", prm).ToList();
            return AppServerHelper.AppResponse(list);
        }

On runtime the Application returns that the object type does not match ne internal providers. I dont have a clue to solve this problem!

1 Answer 1

1

Try using SqlParameter instead of ObjectParameter. Follow the sample code below:

public List<MyCustomObject> SelectLoad(DateTime parameter1, DateTime parameter2)
{          
       return Execute("MyStoredProcedure @Parameter1, @Parameter2",
           new object[]{new SqlParameter("@Parameter1", parameter1),
           new SqlParameter("Parameter2",parameter2)});
}

private List<MyCustomObject> Execute(string query, object[] parameters)
{
    var output = new List<MyCustomObject>();
    using (var dbContext = new MyEntityFrameworkDataContext())
    {
    output = dbContext.Database.SqlQuery<MyCustomObject>(query, parameters).ToList();
    }
    return output;        
}

You can create the parameters object array dynamically within the first method.

Sign up to request clarification or add additional context in comments.

Comments

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.