日期:2014-05-20  浏览次数:20907 次

Entity Framework 4.0怎么直接执行sql查询语句
sql命令是针对oracle数据库的,,可以直接调用entities中的方法执行sql么,

倒是看到一个ExecuteStoreQuery方法,但是不会用,不知道是不是这个方法

没分了,见谅

------解决方案--------------------
ExecuteStoreQuery这个方法应该是执行存储过程的!
------解决方案--------------------
用Entity SQL吧。虽然没用EF访问过Oracle,但原理上Entity SQL应该是数据库无关的。Entity SQL只是语法上像极了SQL,但实际上还是需要对应的LINQ Provider把它转换为具体数据库的native SQL语句。
------解决方案--------------------
如果用hibernate,就使用HQL解决最方便。
------解决方案--------------------
C# code
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);
    }
}

------解决方案--------------------
要不你就把SQL查询封装在存储过程里,再通过EF访问这个存储过程。