C# ne命令并使用它执行两个sql语句,并使用sql data reader代替ReadNextResult()方法来填充注释。您使用的是什么sql?如果您使用t-sql,答案将与您使用plsql时的答案不一样。这很好。我不知道该感谢您多少。我希望我能多投几次
C# ne命令并使用它执行两个sql语句,并使用sql data reader代替ReadNextResult()方法来填充注释。您使用的是什么sql?如果您使用t-sql,答案将与您使用plsql时的答案不一样。这很好。我不知道该感谢您多少。我希望我能多投几次,c#,linq,ado.net,linq-to-entities,C#,Linq,Ado.net,Linq To Entities,ne命令并使用它执行两个sql语句,并使用sql data reader代替ReadNextResult()方法来填充注释。您使用的是什么sql?如果您使用t-sql,答案将与您使用plsql时的答案不一样。这很好。我不知道该感谢您多少。我希望我能多投几次票。。感谢您如此详细地解释这一点。我已经下载了linqpad,它帮了我很大的忙。我希望我能为我的项目雇用你。再次感谢 var a = from arow in context.post where arow.post_id == id &
ne命令并使用它执行两个sql语句,并使用sql data reader代替ReadNextResult()方法来填充注释。您使用的是什么sql?如果您使用t-sql,答案将与您使用plsql时的答案不一样。这很好。我不知道该感谢您多少。我希望我能多投几次票。。感谢您如此详细地解释这一点。我已经下载了linqpad,它帮了我很大的忙。我希望我能为我的项目雇用你。再次感谢
var a = from arow in context.post
where arow.post_id == id && arow.post_isdeleted == false
select new
{
arow.post_id,
PostComments = from c in context.comment
where c.CommentPostID == arow.post_id
select new
{
c.id,
c.title
}
}
List<PostType> pt;
foreach (var s in a)
{
pt = new PostType();
pt.PostID = s.post_id;
//how would I use ADO.NET to put this in a custom class?
foreach(var ctl in s.PostComments)
{
ctl.Title = ctl.title;
pt.CommentT.Add(ctl);
}
ptl.Add(pt);
}
-Select * from Posts where post_Id = id AND IsDeleted = 0;
-Select * from Postcomments where id = cid;
foreach(DataRow dr in PostsTable.Rows)
{
//Fill the Post Custom class
SecondTable.DefaultView.RowFilter = string.Format("PostID = {0}",dr["postID"]);
foreach(DataRow r in SecondTable.Rows)
{
//Fill the Comments Custom class
}
}
SELECT
p.post_id
c.id,
c.title
FROM
post p
WHERE
p.id == 'id' and
isdeleted = false
INNER JOIN comment c ON c.commentpostid = p.post_id
select post_id, id, title from postcomments pc
where post_id = @id and exists(
select post_id form post p where p.post_id = pc.post_id and isdeleted = false
)
public class PostType
{
public int PostId { get; set; }
public List<PostComment> PostComments { get; set; }
}
public class PostComment
{
public int CommentId { get; set; }
public string Title { get; set; }
}
SELECT P.post_id, C.id, C.title
FROM post As P, comment As C
WHERE
P.post_id = @PostId
AND P.post_isdeleted = 0 -- 0 is false
AND C.CommentPostID = P.post_id
int postIdInput = 42; // desired post_id to search for
// PostType delcared prior to getting the results
PostType postType = new PostType()
{
PostId = postIdInput,
PostComments = new List<PostComment>()
};
// Database interaction starts here...
// updated SQL statement to use column name aliases for clarity when used by the SqlDataReader
string sqlStatement = @"SELECT P.post_id As PostId, C.id As CommentId, C.title As Title
FROM post As P, comment As C
WHERE
P.post_id = @PostId
AND P.post_isdeleted = 0 -- 0 is false
AND C.CommentPostID = P.post_id";
string sqlConnectionString = "..."; // whatever your connection is... probably identical to your L2S context.Connection.ConnectionString
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
SqlCommand command = new SqlCommand(sqlStatement, conn);
command.Parameters.AddWithValue("@PostId", postIdInput); // use Parameters.Add() for greater specificity
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// postId was set based on input, but could be set here as well although it would occur repeatedly
// if desired, uncomment the 2 lines below and there's no need to initialize it earlier (it'll be overwritten anyway)
//int postId = Int32.Parse(reader["PostId"].ToString());
//postType.PostId = postId;
int commentId = Int32.Parse(reader["CommentId"].ToString());
string title = reader["Title"].ToString();
// add new PostComment to the list
PostComment postComment = new PostComment
{
CommentId = commentId,
Title = title
};
postType.PostComments.Add(postComment);
}
// done! postType is populated...
}
// use postType...
var query = /* your L2S query here */;
string sqlStatement = context.GetCommand(query).CommandText; // voila!
context.Log = Console.Out;
SELECT [t0].[post_id], [t1].[id], [t1].[title], (
SELECT COUNT(*)
FROM [comment] AS [t2]
WHERE [t2].[id] = [t0].[post_id]
) As [value]
FROM [post] As [t0]
LEFT OUTER JOIN [comment] As [t1] ON [t1].[CommentPostID] = [t0].[post_id]
WHERE ([t0].[post_id] = @p0) AND ([t0].[post_isdeleted] = 0)
ORDER BY [t0].[post_id], [t1].[id]
SELECT [P].[post_id] As PostId, [C].[id] As CommentId, [C].[title] As Title--, (
-- SELECT COUNT(*)
-- FROM [comment] AS [t2]
-- WHERE [t2].[id] = [t0].[post_id]
-- ) As [value]
FROM [post] As [P]
LEFT OUTER JOIN [comment] As [C] ON [C].[CommentPostID] = [P].[post_id]
WHERE ([P].[post_id] = @PostId) AND ([P].[post_isdeleted] = 0)
--ORDER BY [t0].[post_id], [t1].[id]
var query = from arow in context.post
from c in context.comment
where arow.post_id == id && arow.post_isdeleted == false
&& c.CommentPostID == arow.post_id
select new
{
arow.post_id,
c.id,
c.title
};
SELECT [t0].[post_id], [t1].[id], [t1].[title]
FROM [post] As [t0], [comment] As [t1]
WHERE ([t0].[post_id] = @p0) AND ([t0].[post_isdeleted] = 0)
AND ([t1].[CommentPostID] = [t0].[post_id])