从数据库中读取图像并显示在视图中
我正在尝试将旧的 ASP.NET 应用程序转换为 MVC(我刚刚学习 MVC)。我需要在 Gridview 中显示图像。图像本身作为数据类型图像存储在 SQL Server 表中。之前使用的代码如下。有人可以建议一种使用 MVC 的方法吗?我正在考虑创建一个可以嵌入标准视图的部分页面,但不确定这是否是正确的实现设计。
感谢是提前!
` string sqlText = "SELECT * FROM Images WHERE img_pk = " + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
SqlCommand command = new SqlCommand(sqlText, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
//Response.Write("test");
Response.BinaryWrite((byte[])dr["img_data"]);
}
connection.Close();
}
然后可以使用这个图像标签来引用它:
<asp:Image Height="73" Width="80" ID="Image1" ImageAlign="Middle" ImageUrl='<%#"viewimage.aspx?id=" + Eval("ImageId") %>' runat="server"/></a></td>
I'm trying to convert an older ASP.NET application to MVC (I am just learning MVC). and I have a need to display an image in a Gridview. The image itself is stored in a SQL Server table as datatype image. The code that was used previously is below. Can someone suggest an approach using MVC? I was thinking of creating a partial page that I could embed in a standard view, but not sure if that is the right design to implement.
Thanks is advance!
` string sqlText = "SELECT * FROM Images WHERE img_pk = " + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
SqlCommand command = new SqlCommand(sqlText, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
//Response.Write("test");
Response.BinaryWrite((byte[])dr["img_data"]);
}
connection.Close();
}
Then it can be referenced using this image tag:
<asp:Image Height="73" Width="80" ID="Image1" ImageAlign="Middle" ImageUrl='<%#"viewimage.aspx?id=" + Eval("ImageId") %>' runat="server"/></a></td>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一件事是忘记 ASP.NET MVC 应用程序中的 GridView。服务器端控制、回发、视图状态、事件……所有这些概念都不再存在。
在 ASP.NET MVC 中,您使用模型、控制器和视图。
因此,您可以编写一个控制器操作,该操作将从数据库中获取图像并为其提供服务:
然后在您的视图中简单地说:
当然,所有这些控制器操作都很好,但您应该真正将所有数据访问抽象到存储库中:
然后为您正在使用的数据提供者实现此方法:
最后您将使您的控制器变得与数据库无关。应用程序中的层现在在它们之间弱耦合,这将允许您单独重用和单元测试它们:
最后一部分是配置您最喜欢的 DI 框架以将存储库的正确实现注入到控制器中。
The first thing is to forget about GridView in an ASP.NET MVC application. Server side controls, postbacks, viewstate, events, ... all those are notions that no longer exists.
In ASP.NET MVC you work with Models, Controllers and Views.
So you could write a controller action which will fetch the image from the database and serve it:
and then in your view simply:
Now of course all this controller action is nice and dandy, but you should really abstract all data access into a repository:
then implement this method for the data provider you are using:
Finally you will have your controller become database agnostic. Layers in your application are now weakly coupled between them which would allow you to reuse and unit test them in isolation:
and the last part would be to configure your favorite DI framework to inject the proper implementation of the repository into the controller.