[英]ASP.NET MVC Model How to get multiple rows from SQL Server
public static Article GetJournalById(string ConnectionString,int Id)
{
using(SqlConnection sql = new SqlConnection(ConnectionString))
{
using(SqlCommand cmd = new SqlCommand("GetJournalById", sql))
{
Article article = new Article();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", Id);
sql.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
article.Id = Id;
article.Title = reader.GetString(1).ToString();
article.Summary = reader.IsDBNull(2) ? string.Empty : reader.GetString(2).ToString();
article.Tag = reader.GetString(3).ToString();
article.Author = reader.GetString(4).ToString();
article.Ban = reader.GetByte(5);
article.BanReason = reader.IsDBNull(6) ? string.Empty : reader.GetString(6).ToString();
article.Date = reader.GetDateTime(7);
}
}
return article;
}
}
}
How to get multiple rows?如何获得多行? I know that if need get single row and multiple columns I can do like above.我知道如果需要单行多列,我可以像上面那样做。 But how do I get multiple rows?但是如何获得多行? I'd like to get multiple rows and return them from a function.我想获取多行并从 function 返回它们。 I also want to display the returned value on the view page.我还想在视图页面上显示返回值。 But I don't know how, and I don't know what search terms to search for.但我不知道怎么做,也不知道要搜索什么搜索词。 Please help me.请帮我。
Here is the article model.这是文章 model。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace News.Models
{
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public string Tag { get; set; }
public string Author { get; set; }
public byte Ban { get; set; }
public string BanReason { get; set; }
public DateTime Date { get; set; }
}
}
The following is the code to get multiple posts from a stored procedure.以下是从存储过程中获取多个帖子的代码。 (Incomplete, unresolved) (不完整,未解决)
public static int GetJournalByDate(string ConnectionString,DateTime date)//must be fix
{
using (SqlConnection sql = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("GetJournalByDate", sql))
{
try
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime2, 7).Value = date;
sql.Open();
cmd.ExecuteReader();
return 1;
}
catch
{
return 0;
}
}
}
}
you can do it the same way, only instead of Article, use List< Article >你可以用同样的方法,只是用 List< Article > 代替 Article
public List<Article> GetJournalByDate(string ConnectionString,DateTime date)
{
......
List<Article> articles = new List<Article>();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@Date", System.Data.SqlDbType.DateTime2, 7).Value = date;
sql.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var article= new Article();
article.Id = Id;
article.Title = reader.GetString(1).ToString();
article.Summary = reader.IsDBNull(2) ? string.Empty : reader.GetString(2).ToString();
article.Tag = reader.GetString(3).ToString();
article.Author = reader.GetString(4).ToString();
article.Ban = reader.GetByte(5);
article.BanReason = reader.IsDBNull(6) ? string.Empty :
reader.GetString(6).ToString();
article.Date = reader.GetDateTime(7);
articles.Add(article);
}
}
return articles;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.