Make an SQL query with Entity Framework 4

Asked

Viewed 1,122 times

2

How do I perform a query in the database using Entity Framework 4 passing a string previously stored inside a StringBuilder.

The reason is that the SQL query string is giant. I know it is not a good practice, but for now I will have to do so for lack of time.

That’s the string SQL of the query:

StringBuilder strSql = new StringBuilder();
strSql.AppendLine(string.Format("SELECT * FROM (SELECT TOP intRestante * FROM (SELECT TOP {0}", intTop))[... Continua, a string é gigante];

In Webforms I would do something like this:

objCommand = new SqlCommand();
objCommand.Connection = conn;
objCommand.CommandTimeout = 600;
objCommand.CommandText = strSql.ToString();
objCommand.Parameters.Add("@Parametros", SqlDbType.NVarChar, 100).Value = "%" + strParametros + "%";
SqlDataAdapter adp = new SqlDataAdapter(objCommand);
adp.Fill(dt);

I would like to know how to do exactly the same thing, but in Entity Framework.

  • 4

    Put the example and a snippet of code that can help solve your problem!

  • 2

    If the string is very large, the correct is to fragment the single query into several queries.

  • Gypsy Morrison Mendez I could do so, but the string is already ready and "want" me to use it.

  • 2

    @Good Juniordias, then put the string all here. I can do it, but it’s too bad to use it this way.

  • @Ciganomorrisonmendez I already got it here, I used ctx.Database.Executesqlcommand Thank you very much!

1 answer

1


For Entity Framework executing commands with query (which returns data) you need to have some data types defined for mapping the query result.

If you want un-typed results, I suggest you use Sqlconnection + Dbcommand to run the query and analyze the result via Dbreader object.

Source: http://forums.asp.net/post/5367669.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.