选择使用Asp.Net C#的所有图片 [英] select all images using Asp.Net C#

查看:112
本文介绍了选择使用Asp.Net C#的所有图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET和C#。我想从文件夹中检索所有的图像,并显示页面上,但它只能选择一个图像。

我的ASP.NET code:

 <表ID =form1的=服务器级=COL-LG-5>
    < ASP:图片ID =图像=服务器/>
< /表及GT;

我的C#code:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data.SqlClient的;
使用System.Data这;
使用System.Configuration;博客空间
{
    公共部分类指标:System.Web.UI.Page
    {
        SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [blogconnection]的ToString());
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            con.Open();
            串allimage;
            字符串QRY =选择图像*;
            CMD的SqlCommand =新的SqlCommand(QRY,CON);
            SqlDataReader的博士= cmd.ExecuteReader();            如果(dr.HasRows)
            {
                而(dr.Read())
                {
                   如果(!string.IsNullOrEmpty(Convert.ToString(DR [IMAGE_PATH])))
                   {
                        Image.ImageUrl = Convert.ToString(DR [IMAGE_PATH]);
                   }
                }
           }
           con.Close();
       }
    }
}

我要什么:我要选择所有的图像的路径是存储在SQL表

其他:有没有办法来选择文件夹的路径是存储在SQL视频,意味着选择不同的文件夹中的视频和图像,并通过指定日期或最近上传的同时显示同一页上等等。

任何帮助将AP preciated。

编辑#1

在PHP中我用下面的code获得的所有图像,并显示它,没有任何东西等同于ASP.NET下面code?

PHP code

 < PHP
包括conn.php';
$ SMT = $ conn-> prepare(SELECT * FROM后');
$ SMT-GT&;执行();?>
<?PHP的包括header.php文件';?>
< D​​IV CLASS =>
< PHP
如果(使用isset($ _ SESSION ['用户']))
{
包括nav.php';
}
其他
{
包括NAV-simple.php';
}
?>
<?PHP的包括slider.php';?><?PHP的包括right_sidebar.php';?>    < D​​IV CLASS =COL-MD-1 top_space>< / DIV>
< D​​IV CLASS =集装箱COL-MD-8主要容器顶>
    < BR />< D​​IV CLASS =>
    < PHP而($ GDATA = $ SMT-GT&;取(PDO :: FETCH_OBJ)):??>
        < A HREF =#类=COL-MD-4>< IMG SRC =的帖子/< PHP的echo $ gdata-> Post_Path;>中ALT =形象CLASS =后映像/>< / A>
        < D​​IV CLASS =媒体体COL-MD-8后拉左>
            < D​​IV CLASS = - 概述后>
                < UL>
                    <李班=后类>< PHP的echo $ gdata->分类; ?>< /李>
                    <李班=后timestemp>发布针对<?PHP的echo $ gdata-> POST_DATE; ?>< /李>
                < / UL>
                < A HREF =?description.php后ID =< PHP的echo $ gdata-> ID和GT;>< H4
                        类=媒体标题H4>< PHP的echo $ gdata->标题; ?>&下; / H4>&下; / A>                < p =类文本后>< PHP的echo $ gdata->邮政; ?>< / P>< BR />
            < / DIV>
        < / DIV>
< D​​IV CLASS =后期图像空间>< / DIV>
    < PHP ENDWHILE;>


解决方案

在code后面写你的收藏()方法来检索图像作为一个 字符串像这样(的列表此外,它是更好地使用的 使用 语句的):

 保护了IEnumerable<串GT;采集()
{
     。字符串地址= ConfigurationManager.ConnectionStrings [blogconnection]的ToString();
     使用(SqlConnection的CON =新的SqlConnection(地址))
     {
         con.Open();
         字符串QRY =选择图像*;
         CMD的SqlCommand =新的SqlCommand(QRY,CON);
         使用(SqlDataReader的博士= cmd.ExecuteReader())
         {
             如果返回allimage(dr.HasRows!);
             而(dr.Read())
             {
                 如果(!string.IsNullOrEmpty(Convert.ToString(DR [IMAGE_PATH])))
                 {
                     回报收益率(DR [IMAGE_PATH]的ToString());
                 }
              }
          }
      }
}

然后要么你可以使用 ASP:直放站是这样的:

 < ASP:直放站ID =Repeater1=服务器的DataSourceID =imgCats>
      <&ItemTemplate中GT;
         < D​​IV>
            < IMG SRC ='<%#Container.DataItem.ToString()%GT;' ALT =/>
         < / DIV>
      < / ItemTemplate中>
< / ASP:直放站>
< ASP:ObjectDataSource控件ID =imgCats=服务器SelectMethod =宝典
          类型名=WebApplication1.WebForm8>
< / ASP:ObjectDataSource控件>

或者你也可以做这样的:

 <表ID =form1的=服务器级=COL-LG-5>
  < D​​IV>
    < UL>
        <%VAR DRC =集();
           的foreach(在刚果民主共和国VAR项)
           {%GT;
            <立GT;
                < IMG SRC =<%:项%GT;/>
            < /李>
        <%}%GT;
    < / UL>
  < / DIV>
< /表及GT;

I am new to ASP.NET and C#. I am trying to retrieve all images from folder and show it on page, but it's only selecting one image.

My ASP.NET code:

<form id="form1" runat="server" class="col-lg-5">            
    <asp:Image ID="Image" runat="server" />
</form>

My C# code:

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();
       }               
    }
}

What I want: I want to select all image which path is store in sql table.

Additional: is there a way to select videos from folder which path is store in sql, mean to select videos and images from different folder and show both on same page by date specified or by latest upload etc.

Any help will be appreciated.

Edit #1

In php i use the below code to get all images and show it, is there any thing equivalent to the below code in ASP.NET?

PHP Code

<?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;?>

解决方案

In the code behind write your Collection() method to retrieve images as a List of Strings like this (Also it is better to use Using statements):

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());
                 }
              }
          }
      }
}

Then either you can use asp:Repeater like this:

<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>

Or you can do it like this:

<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>

这篇关于选择使用Asp.Net C#的所有图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆