在EF中使用原生SQL,首先要建立上下文物件

2022-06-22 22:24:17 字數 1555 閱讀 9801

using (var db = new entities())

string sql = "insert into userinfo values('zhangsan','123456')";

db.database.executesqlcommand(sql);

引數化新增

sql = "insert into userinfo values(@username,@userpass)";

var param = new sqlparameter

;db.database.executesqlcommand(sql, param);

sql = "delete from userinfo where username='zhangsan'";

db.database.executesqlcommand(sql);

引數化刪除

sql = "delete from userinfo where username=@username";

db.database.executesqlcommand(sql, new sqlparameter("@username", "lisi"));

sql = "update userinfo set username='wangwu'";

db.database.executesqlcommand(sql);

sql = "select * from userinfo";

var users = db.database.sqlquery(sql);

foreach (var user in users)

完整案例:

//1.建立上下文物件

using (var db = new entities())

;db.database.executesqlcommand(sql, param);

//刪sql = "delete from userinfo where username='zhangsan'";

db.database.executesqlcommand(sql);

//引數化刪除

sql = "delete from userinfo where username=@username";

db.database.executesqlcommand(sql, new sqlparameter("@username", "lisi"));

//改sql = "update userinfo set username='wangwu'";

db.database.executesqlcommand(sql);

//原生sql查詢

sql = "select * from userinfo";

var users = db.database.sqlquery(sql);

foreach (var user in users)

}

在Django中使用原生sql

raw row方法 摻雜著原生sql和orm來執行的操作 res cookbook.objects.raw select id as nid from epos cookbook where id s params 1,print res.columns nid print type res 在se...

sql原生語句在tp中使用

tp的模型可以支援原生sql操作,提供了query和execute兩個方法,為什麼原生sql還要區分兩個方法呢,原因有兩個 1 返回型別不同 query用於查詢,返回的是資料集,和select或者findall一樣,所以可以直接在模板裡面使用volist標籤輸出query的查詢結果 execute用...

在django中使用原生sql語句

row方法 摻雜著原生sql和orm來執行的操作 res cookbook.objects.raw select id as nid from epos cookbook where id s params 1,print res.columns nid print type res 在select...