Execute sql statement in asp.net mvc3 (C #)

How to execute sql statement in asp.net mvc3 (c #)? I am using Entity Data Model for my asp.net mvc application

I need to execute a sql query (" select * from users where EmailAddress like '%@gmail.com'").

+3
source share
1 answer

Is your object Usermapped? In this case, yo can use

var users = from u in context.Users
            where u.EmailAddress.EndsWith("@gmail.com")
            select u;

If the table Userdoes not appear, but you have a class Userwith a constructor without parameters and public custom properties with the same names as the columns in the result set, you can use:

var users = context.ExecuteStoreQuery<User>("select * from users where EmailAddress like '%@gmail.com'");

in case of ObjectContext API or:

var users = context.Database.SqlQuery<User>("select * from users where EmailAddress like '%@gmail.com'");

, ADO.NET, SqlConnection, SqlCommand ExecuteReader .

+5

All Articles