Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'").

like image 573
Prasad Avatar asked Jun 06 '26 21:06

Prasad


1 Answers

Is your User entity mapped? In such case yo can use

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

If you don't have User table mapped but you have User class with parameterless constructor and public settable properties with same names as columns in 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'");

If you don't have entity you can execute it as any other ADO.NET command by creating SqlConnection, SqlCommand and calling ExecuteReader on the command.

like image 85
Ladislav Mrnka Avatar answered Jun 08 '26 17:06

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!