如何在LINQ上执行SQL查询? [英] How to execute SQL queries on LINQ?
本文介绍了如何在LINQ上执行SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过ExecuteQuery()和ExecuteCommand()对Linq进行dml操作。发生的错误是在执行后它不会影响数据库中的记录。
例如:要在数据库中向表中插入新记录,我使用了ExecuteCommand()。
发生错误后我使用Submitchanges()编码,但没有我期望的结果。
示例代码将数据插入表格:
I'm trying to do dml operations on Linq by ExecuteQuery() and ExecuteCommand(). The error that occurred is after execution it doesn't affects the record in database.
For eg: To insert a new record to table in database, I used ExecuteCommand().
After the error occurred i coded with Submitchanges() but there is no result of what i expect.
A Sample code for inserting data to a table:
using(StudentsDbDataContext StudentContext = new StudentsDbDataContext())
{
int r = StudentContext.ExecuteCommand("insert into tblStudents values ({0},{1},{2})", firstname, lastname, gender);
if (r > 0) {
Console.WriteLine("{0} row added.", r);
Console.ReadKey();
}
}
我的尝试:
What I have tried:
using(StudentsDbDataContext StudentContext = new StudentsDbDataContext())
{
int r = StudentContext.ExecuteCommand("insert into tblStudents values ({0},{1},{2})", firstname, lastname, gender);
StudentContext.SubmitChanges();//<--- ADDED
if (r > 0) {
Console.WriteLine("{0} row added.", r);
Console.ReadKey();
}
}
如何解决这个问题?
How to solve this?
推荐答案
Try with below code, may be it help you
StudentContext.Database.SqlQuery<int>("insertProcedure param1 param2 param3");
这篇关于如何在LINQ上执行SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文