我是ASP.NET和C#的新手.我正在尝试从文件夹检索所有图像,并显示在页面上,但它只是选择一个图像.
我的ASP.NET代码:
<form id="form1" runat="server" class="col-lg-5"> <asp:Image ID="Image" runat="server" /> </form>
我的C#代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.sqlClient; using System.Data; using System.Configuration; namespace Blog { public partial class index : System.Web.UI.Page { sqlConnection con = new sqlConnection(ConfigurationManager.ConnectionStrings["blogconnection"].ToString()); protected void Page_Load(object sender,EventArgs e) { con.Open(); string allimage; string qry="select * from images"; sqlCommand cmd = new sqlCommand(qry,con); sqlDataReader dr =cmd.ExecuteReader(); if (dr.HasRows) { while(dr.Read()) { if (!string.IsNullOrEmpty(Convert.ToString(dr["Image_Path"]))) { Image.ImageUrl = Convert.ToString(dr["Image_Path"]); } } } con.Close(); } } }
我想要的:我想选择所有图像的路径存储在sql表中.
另外:有没有办法从sql中存储哪个路径的文件夹中选择视频,这意味着从不同的文件夹中选择视频和图像,并且在指定的日期或最近的上传的同一页面上显示.
任何帮助将不胜感激.
编辑#1
在PHP中,我使用下面的代码来获取所有图像并显示它,ASP.NET中是否有任何相当于以下代码的东西?
<?PHP include 'conn.PHP'; $smt=$conn->prepare('SELECT * FROM post'); $smt->execute(); ?> <?PHP include 'header.PHP'; ?> <div class=""> <?PHP if(isset($_SESSION['user'])) { include 'nav.PHP'; } else { include 'nav-simple.PHP'; } ?> <?PHP include 'slider.PHP';?> <?PHP include 'right_sidebar.PHP';?> <div class="col-md-1 top_space"></div> <div class="container col-md-8 main-container-top"> <br/> <div class=""> <?PHP while ($gdata = $smt->fetch(PDO::FETCH_OBJ)): ?> <a href="#" class="col-md-4"><img src="posts/<?PHP echo $gdata->Post_Path; ?>" alt="image" class="post-image"/></a> <div class="media-body col-md-8 post pull-left"> <div class="post-overview"> <ul> <li class="post-category"><?PHP echo $gdata->Category; ?></li> <li class="post-timestemp">Post on <?PHP echo $gdata->Post_Date; ?></li> </ul> <a href="post-description.PHP?id=<?PHP echo $gdata->Id ?>"><h4 class="media-heading h4"><?PHP echo $gdata->Title; ?></h4></a> <p class="post-text"><?PHP echo $gdata->Post; ?></p><br/> </div> </div> <div class="post-image-space"></div> <?PHP endwhile;?>
解决方法
在后面的代码中编写你的Collection()方法来检索像这样的字符串列表的图像(最好使用
Using
语句):
protected IEnumerable<string> Collection() { string address = ConfigurationManager.ConnectionStrings["blogconnection"].ToString(); using (sqlConnection con = new sqlConnection(address)) { con.Open(); string qry = "select * from images"; sqlCommand cmd = new sqlCommand(qry,con); using (sqlDataReader dr = cmd.ExecuteReader()) { if (!dr.HasRows) return allimage; while (dr.Read()) { if (!string.IsNullOrEmpty(Convert.ToString(dr["Image_Path"]))) { yield return (dr["Image_Path"].ToString()); } } } } }
那么你可以使用asp:Repeater这样:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="imgCats"> <ItemTemplate> <div> <img src='<%# Container.DataItem.ToString() %>' alt="" /> </div> </ItemTemplate> </asp:Repeater> <asp:ObjectDataSource ID="imgCats" runat="server" SelectMethod="Collection" TypeName="WebApplication1.WebForm8"> </asp:ObjectDataSource>
或者你可以这样做:
<form id="form1" runat="server" class="col-lg-5"> <div> <ul> <% var drc = Collection(); foreach (var item in drc) { %> <li> <img src="<%: item %>"/> </li> <% } %> </ul> </div> </form>