foreach循环遍历数据库表.net c#
这是漫长的一天,我似乎对当前的问题一片空白。下面是我的 HomeController 中包含的代码:
public ActionResult About()
{
SqlDataReader rdr;
string fileName = "";
const string connect = @"Server=localhost;Database=Images;user id=user; password=password;";
using (var conn = new SqlConnection(connect))
{
var qry = "SELECT FileName FROM FileStore";
var cmd = new SqlCommand(qry, conn);
conn.Open();
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
fileName = rdr["FileName"].ToString();
}
}
return View();
}
我只想在视图中显示数据库中的文件名列表。我记得如何做到这一点,但我坚持如何编写将循环遍历我的 sql 表的循环语句。
有人可以指出我正确的方向吗?
It's been a long day and I seem to have drawn a blank with my current issue. Below is code contained in my HomeController:
public ActionResult About()
{
SqlDataReader rdr;
string fileName = "";
const string connect = @"Server=localhost;Database=Images;user id=user; password=password;";
using (var conn = new SqlConnection(connect))
{
var qry = "SELECT FileName FROM FileStore";
var cmd = new SqlCommand(qry, conn);
conn.Open();
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
fileName = rdr["FileName"].ToString();
}
}
return View();
}
I simply want to display a list of the fileNames from the database in a view. I remember how to do this but I'm stuck on how to write the loop statement that will loop through my sql table.
Can someone point me in the right direction please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的意思是像
while (rdr.Read())
中那样吗?注意:使用此模式,您不需要
.HasRows
。Do you mean as in
while (rdr.Read())
?NOTE: Using this pattern, you don't need
.HasRows
.