<p>&lt;%@ Page Language='C#' %&gt;
&lt;%@ Import Namespace='System.Data.SqlClient' %&gt;
&lt;%@ Import Namespace='System.Data' %&gt;</p>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'>
    <title>ASP.NET 图片显示页面</title>
</head>
<body>
    <form id='form1' runat='server'>
        <div>
            <asp:Repeater ID='rptImages' runat='server'>
                <ItemTemplate>
                    <img src='<%# Eval("ImagePath") %>' />
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>
<script runat='server'>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindImages();
        }
    }

    private void BindImages()
    {
        string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword";
        string query = "SELECT ImagePath FROM Images";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);

                rptImages.DataSource = dataTable;
                rptImages.DataBind();
            }
        }
    }
</script>
ASP.NET 图片显示页面:从数据库获取图片路径

原文地址: https://www.cveoy.top/t/topic/ozRl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录