asp.net EF框架执行原生SQL语句

时间:2024-11-02 11:05:37

1.执行无参数sql:

string sql = "select * from IntegralInfo where convert(nvarchar,getdate(),23)='{0}' and status=1 and userinfoid='{1}'";
sql = string.Format(sql, DateTime.Now.ToString("yyyy-MM-dd"), uid);
var IntegralInfoObj = db.Database.SqlQuery<IntegralInfo>(sql).FirstOrDefault();

2.执行有参数sql:

 var IntegralInfoObj = db.Database.SqlQuery<IntegralInfo>("select * from IntegralInfo where convert(nvarchar,getdate(),23)=@time and status=1 and userinfoid=@uid", new SqlParameter("@time", DateTime.Now.ToString("yyyy-MM-dd")), new SqlParameter("@uid", uid)).FirstOrDefault();