在EF中
执行Sql语句
。
using
(
var
context =
new
EFRecipesEntities())
{
string
sql =
@”insert into Chapter3.Payment(Amount, Vendor)
values (@Amount, @Vendor)”
;
var
args =
new
DbParameter[] {
new
SqlParameter { ParameterName =
“Amount”
, Value =
99.97
M},
new
SqlParameter { ParameterName =
“Vendor”
, Value=
“Ace Plumbing”
}
};
int
rowCount = context.ExecuteStoreCommand(sql, args);
}
select all
code
:
code
:
using
(
var
context =
new
EFRecipesEntities())
{
string
sql =
“select * from Chapter3.Student where Degree = @Major”
;
var
args =
new
DbParameter[] {
new
SqlParameter {ParameterName =
“Major”
, Value =
“Masters”
}};
var
students = context.ExecuteStoreQuery<Student>(sql, args);
Console.WriteLine(
“Students…”
);
foreach
(
var
student
in
students)
{
Console.WriteLine(
“{0} {1} is working on a {2} degree”
,
student.FirstName, student.LastName, student.Degree);
}
}
select specific column
using
(
var
conn =
new
EntityConnection(
“name=EFRecipesEntities”
))
{
var
cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText =
@”select c.Name, C.Email from
EFRecipesEntities.Customers as c”
;
using
(
var
reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while
(reader.Read())
{
Console.WriteLine(
“{0}’s email is: {1}”
,
reader.GetString(
0
), reader.GetString(
1
));
}
}
}