在 LINQ to SQL 中执行查询
如何使用 LINQ to SQL 执行查询?询问事情是这样的。
假设我有这张表
CREATE TABLE dbo.Students
(
StudentID INT IDENTITY(1,1) PRIMARY KEY,
Name SYSNAME
);
CREATE TABLE dbo.StudentLoans
(
LoanID INT IDENTITY(1,1) PRIMARY KEY,
StudentID INT FOREIGN KEY REFERENCES dbo.Students(StudentID),
Amount BIGINT -- just being funny
);
然后我想执行这个查询。
DECLARE
@Name SYSNAME = N'user962206',
@LoanAmount BIGINT = 50000,
@StudentID INT;
INSERT dbo.Students(Name)
SELECT @Name;
SELECT @StudentID = SCOPE_IDENTITY();
INSERT dbo.StudentLoans(StudentID, Amount)
SELECT @StudentID, @LoanAmount;
这可能吗?即使您的行和列已映射?如何使用 LINQ to SQL 执行该查询?
How do I execute a query using LINQ to SQL? a query that something goes like this.
Let's say I have this table
CREATE TABLE dbo.Students
(
StudentID INT IDENTITY(1,1) PRIMARY KEY,
Name SYSNAME
);
CREATE TABLE dbo.StudentLoans
(
LoanID INT IDENTITY(1,1) PRIMARY KEY,
StudentID INT FOREIGN KEY REFERENCES dbo.Students(StudentID),
Amount BIGINT -- just being funny
);
Then I wanted to execute this query.
DECLARE
@Name SYSNAME = N'user962206',
@LoanAmount BIGINT = 50000,
@StudentID INT;
INSERT dbo.Students(Name)
SELECT @Name;
SELECT @StudentID = SCOPE_IDENTITY();
INSERT dbo.StudentLoans(StudentID, Amount)
SELECT @StudentID, @LoanAmount;
Is that possible? even if your Rows and Columns are mapped? how can I execute that query with LINQ to SQL ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已经有一段时间了,但不会是这样的吧?
假设您已将所有表拖到 Linq2Sql 设计器上,只需创建一个
Student
对象,将StudentLoan
添加到其StudentLoans
集合,然后添加使用myContextInstance.Students.InsertOnSubmit(newStudent)
将Student
添加到Context.Students
集合中,并且通过调用myContextInstance.SubmitChanges
写出更改。因此,将它们放在一起:
如果您的 DataContext 如下所示,则代码片段有效:
这就是结果只需将表拖到 dbml 文件的设计界面即可。
It's been a while, but wouldn't it be something like this?
Assuming you've dragged all your tables onto the Linq2Sql designer, simply create a
Student
object, add aStudentLoan
to itsStudentLoans
collection, and add theStudent
to theContext.Students
collection withmyContextInstance.Students.InsertOnSubmit(newStudent)
and write out the changes with a call tomyContextInstance.SubmitChanges
.So, putting it all together:
The code snippet works if your DataContext looks like this:
This is the result of just dragging your tables to the design surface of the dbml file.
如果您的问题的意思是“如何使用 LINQ to SQL 执行原始 SQL 查询”,那么请查看 ExecuteCommand 和 ExecuteQuery 方法:
http://msdn.microsoft.com/en -us/library/system.data.linq.datacontext.executecommand.aspx
所有这些方法都采用原始 SQL 查询(如您的查询),然后针对数据库运行它
如果您的意思是通过你的问题“我如何将此 SQL 查询编写为 LINQ 查询”,那么请澄清你的问题。
If by your question you mean "how do a execute a raw SQL query with LINQ to SQL," then look at the
ExecuteCommand
andExecuteQuery
methods:http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executecommand.aspx
All these methods take a raw SQL query, like yours, and runs it against the database
If you meant by your question "how do I write this SQL query as a LINQ query" then please clarify your question.