C# ExecuteStoreQuery和ExecuteStoreCommand之间有什么区别

C# ExecuteStoreQuery和ExecuteStoreCommand之间有什么区别,c#,entity-framework,executestorequery,C#,Entity Framework,Executestorequery,在entityframework中,我们可以使用ExecuteStoreQuery或ExecuteStoreCommand执行sql查询。那么它们之间有什么区别(哪种情况不同) 谢谢。从 ExecuteStoredQuery 直接对返回 键入结果的序列 及 执行建议 使用直接对数据源执行任意命令 现有的连接 : 使用(学校实体)上下文= 新学校实体() { //以下三个查询演示 //传递参数的三种不同方式。 //查询返回字符串结果类型。 //使用参数替换模式。 foreach(context.E

在entityframework中,我们可以使用ExecuteStoreQuery或ExecuteStoreCommand执行sql查询。那么它们之间有什么区别(哪种情况不同)


谢谢。

ExecuteStoredQuery

直接对返回 键入结果的序列

执行建议

使用直接对数据源执行任意命令 现有的连接

:

使用(学校实体)上下文=
新学校实体()
{
//以下三个查询演示
//传递参数的三种不同方式。
//查询返回字符串结果类型。
//使用参数替换模式。
foreach(context.ExecuteStoreQuery中的字符串名称
(“从部门ID<{0}所在的部门中选择名称,5))
{
Console.WriteLine(名称);
}
//对对象值使用参数语法。
foreach(context.ExecuteStoreQuery中的字符串名称
(“从部门ID<@p0',5)所在的部门中选择名称)
{
Console.WriteLine(名称);
}
//使用显式SqlParameter。
foreach(context.ExecuteStoreQuery中的字符串名称
(“从部门ID<@p0]所在的部门中选择名称”,
新的SqlParameter{ParameterName=“p0”,Value=5})
{
Console.WriteLine(名称);
}
}

公共类部门信息
{
私人日期时间(startDate);;
私有字符串\u名称;
私人Int32_部门ID;
公共Int32部门ID
{
得到
{
返回部门ID;
}
设置
{
_部门ID=值;
}
}
公共字符串名
{
得到
{
返回_name;
}
设置
{
_名称=值;
}
}
公共日期时间起始日期
{
得到
{
返回开始日期;
}
设置
{
_startDate=值;
}
}
}
公共静态void ExecuteStoreCommands()
{
使用(学校实体)上下文=
新学校实体()
{
内部部门ID=21;
//在Department表中插入行。使用参数替换模式。
int rowsAffected=context.ExecuteStoreCommand(“插入部门值({0},{1},{2},{3},{4})”,
部门ID,“工程”,350000.00,“2009-09-01”,2);
WriteLine(“受影响的行数:{0}”,rowsAffected);
//获取DepartmentTest对象。
DepartmentInfo department=context.ExecuteStoreQuery
(“从DepartmentID={0}的部门中选择*,DepartmentID).FirstOrDefault();
WriteLine(“ID:{0},Name:{1}”,department.DepartmentID,department.Name);
rowsAffected=context.ExecuteStoreCommand(“从部门中删除,其中部门ID={0}”,部门ID);
WriteLine(“受影响的行数:{0}”,rowsAffected);
}
}

所以。。。前者返回类型化结果,就好像它们是EF定义的类的组成部分,后者只执行一些代码?@MVCDS:-为了简单起见,我想说ExecuteStoreQuery方法相当于调用ExecuteReader,但它返回实体:)当我使用ExecuteStoreQuery和executestorecommand时?你能给我举个例子吗?@manaskumar:-检查一下:我能用executestrequery更新我数据库中的一些东西吗?
using (SchoolEntities context =
    new SchoolEntities())
{
    // The following three queries demonstrate 
    // three different ways of passing a parameter.
    // The queries return a string result type.

    // Use the parameter substitution pattern.
    foreach (string name in context.ExecuteStoreQuery<string>
        ("Select Name from Department where DepartmentID < {0}", 5))
    {
        Console.WriteLine(name);
    }

    // Use parameter syntax with object values.
    foreach (string name in context.ExecuteStoreQuery<string>
        ("Select Name from Department where DepartmentID < @p0", 5))
    {
        Console.WriteLine(name);
    }
    // Use an explicit SqlParameter.
    foreach (string name in context.ExecuteStoreQuery<string>
        ("Select Name from Department where DepartmentID < @p0",
            new SqlParameter { ParameterName = "p0", Value = 5 }))
    {
        Console.WriteLine(name);
    }
}
public class DepartmentInfo
{
    private DateTime _startDate;
    private String _name;
    private Int32 _departmentID;

    public Int32 DepartmentID
    {
        get
        {
            return _departmentID;
        }
        set
        {
            _departmentID = value;
        }
    }
    public String Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
    public DateTime StartDate
    {
        get
        {
            return _startDate;
        }
        set
        {
            _startDate = value;
        }
    }
}

public static void ExecuteStoreCommands()
{
    using (SchoolEntities context =
        new SchoolEntities())
    {

        int DepartmentID = 21;
        // Insert the row in the Department table. Use the parameter substitution pattern.
        int rowsAffected = context.ExecuteStoreCommand("insert Department values ({0}, {1}, {2}, {3}, {4})",
                        DepartmentID, "Engineering", 350000.00, "2009-09-01", 2);
        Console.WriteLine("Number of affected rows: {0}", rowsAffected);

        // Get the DepartmentTest object. 
        DepartmentInfo department = context.ExecuteStoreQuery<DepartmentInfo>
            ("select * from Department where DepartmentID= {0}", DepartmentID).FirstOrDefault();

        Console.WriteLine("ID: {0}, Name: {1} ", department.DepartmentID, department.Name);

        rowsAffected = context.ExecuteStoreCommand("delete from Department where DepartmentID = {0}", DepartmentID);
        Console.WriteLine("Number of affected rows: {0}", rowsAffected);
    }
}