C# 如何在ASP.NET MVC4中不使用实体框架从数据库检索图像
我是ASP.NET MVC新手,需要您的帮助。我正在尝试从数据库创建插入和获取图像,我可以将图像保存为二进制格式,但无法将其恢复为图像格式 我的输出如下图所示 这是我的看法C# 如何在ASP.NET MVC4中不使用实体框架从数据库检索图像,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我是ASP.NET MVC新手,需要您的帮助。我正在尝试从数据库创建插入和获取图像,我可以将图像保存为二进制格式,但无法将其恢复为图像格式 我的输出如下图所示 这是我的看法 @model IEnumerable<HelphoProject.Models.Property> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> hi you are now in Index page o
@model IEnumerable<HelphoProject.Models.Property>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
hi you are now in Index page of HElpho
</p>
<div>
@Html.DropDownList("CityID", (IEnumerable<SelectListItem>)ViewBag.CityList,"Select City")
</div>
<div>
@Html.DropDownList("propertyTypeID",(IEnumerable<SelectListItem>)ViewBag.PropertyTypeList,"Select PropertyType")
</div>
@Html.ActionLink("Add New Property", "AddNewProperty",null, new { @class="btn btn-primary"})
<table class="table" style="width: 1200px;">
<tr>
<th>
<b>ImageID</b>
</th>
<th>
<b>CityID</b>
</th>
<th>
<b>TypeID</b>
</th>
<th>
<b>Image</b>
</th>
<th>
<b>Description</b>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(a =>item.ImageID )
</td>
<td>
@Html.DisplayFor(a=>item.CityID)
</td>
<td>
@Html.DisplayFor(a=>item.propertyTypeID)
</td>
<td style="width: 500px;">
@Html.Raw(item.Image)
</td>
<td>
<img src="/Content/RetrieveImage/@item.ImageID" alt="" height=100 width=200 />
</td>
<td>
@Html.DisplayFor(a=>item.Description)
</td>
<td style="width: 500px;">
@*@Html.Raw(item.content)*@
</td>
<td>
</td>
<td>
@* @Html.DisplayFor(modelItem => item.Description)*@
</td>
</tr>
}
</table>
我还使用了另一个东西来连接数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using HelphoProject.Models;
using System.IO;
namespace HelphoProject.DataAccessLayer
{
public class readwriteProperty
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlCommand cmd;
SqlDataAdapter sda;
DataTable dt;
public string SaveProperty(HttpPostedFileBase file, Property objproperty)
{
objproperty.Image = ConvertToBytes(file,null);
cmd = new SqlCommand("spSaveProperty", cn);
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@intStateID", objCity.StateID);
cmd.Parameters.AddWithValue("@ImageID", objproperty.ImageID);
cmd.Parameters.AddWithValue("@CityID", objproperty.CityID);
cmd.Parameters.AddWithValue("@TypeID", objproperty.propertyTypeID);
cmd.Parameters.AddWithValue("@Image", objproperty.Image);
cmd.Parameters.AddWithValue("@Description", objproperty.Description);
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
return "Datat Not Saved Successfully";
}
cn.Close();
return "Data Saved Successfully";
}
public byte[] getImageFromDB()
{
cn.Open();
sda = new SqlDataAdapter("select Image FROM tblImages", cn);
dt = new DataTable();
sda.Fill(dt);
Property objmainProperty = new Property();
foreach (DataRow dr in dt.Rows)
{
Property objProperty = new Property();
objProperty.Image=(byte[])dr["Image"];
objmainProperty.Image = objProperty.Image;
}
cn.Close();
return objmainProperty.Image;
}
public List<Property> getproperties()
{
cn.Open();
sda = new SqlDataAdapter("select ImageID,CityID,TypeID,Image,Description from tblImages", cn);
dt = new DataTable();
sda.Fill(dt);
List<Property> properties = new List<Property>();
foreach(DataRow dr in dt.Rows)
{
Property objProperty = new Property();
objProperty.ImageID = Convert.ToInt16(dr["ImageID"]);
objProperty.CityID = Convert.ToInt16(dr["CityID"]);
objProperty.propertyTypeID = Convert.ToInt16(dr["TypeID"]);
objProperty.Image = (byte[])dr["Image"];
objProperty.Description = dr["Description"].ToString();
properties.Add(objProperty);
}
cn.Close();
return properties;
}
public byte[] ConvertToBytes(HttpPostedFileBase image,Property objproperty)
{
if (image != null)
{
byte[] imageBytes = null;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}
else
{
byte[] imageBytes = objproperty.Image;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.SqlClient;
使用系统数据;
使用系统配置;
使用HelphoProject.Models;
使用System.IO;
命名空间HelpHopProject.DataAccessLayer
{
公共类读写属性
{
SqlConnection cn=新的SqlConnection(ConfigurationManager.ConnectionString[“mycon”].ConnectionString);
SqlCommand命令cmd;
SqlDataAdapter-sda;
数据表dt;
公共字符串SaveProperty(HttpPostedFileBase文件,属性objproperty)
{
objproperty.Image=ConvertToBytes(文件,null);
cmd=新的SqlCommand(“spSaveProperty”,cn);
cmd.CommandType=CommandType.storedProcess;
//cmd.Parameters.AddWithValue(“@intStateID”,objCity.StateID);
cmd.Parameters.AddWithValue(“@ImageID”,objproperty.ImageID);
cmd.Parameters.AddWithValue(“@CityID”,objproperty.CityID);
cmd.Parameters.AddWithValue(“@TypeID”,objproperty.propertyTypeID);
cmd.Parameters.AddWithValue(“@Image”,objproperty.Image);
cmd.Parameters.AddWithValue(“@Description”,objproperty.Description);
cn.Open();
尝试
{
cmd.ExecuteNonQuery();
}
捕获(例外情况除外)
{
返回“数据未成功保存”;
}
cn.Close();
返回“数据保存成功”;
}
公共字节[]getImageFromDB()
{
cn.Open();
sda=新的SqlDataAdapter(“从tblImages中选择图像”,cn);
dt=新数据表();
sda.填充(dt);
Property objmainProperty=新属性();
foreach(数据行dr在dt.行中)
{
Property objProperty=新属性();
objProperty.Image=(字节[])dr[“Image”];
objmainProperty.Image=objProperty.Image;
}
cn.Close();
返回objmainProperty.Image;
}
公共列表getproperties()
{
cn.Open();
sda=新的SqlDataAdapter(“从tblImages中选择ImageID、CityID、TypeID、Image、Description”,cn);
dt=新数据表();
sda.填充(dt);
列表属性=新列表();
foreach(数据行dr在dt.行中)
{
Property objProperty=新属性();
objProperty.ImageID=Convert.ToInt16(dr[“ImageID”]);
objProperty.CityID=Convert.ToInt16(dr[“CityID]”);
objProperty.propertyTypeID=Convert.ToInt16(dr[“TypeID]”);
objProperty.Image=(字节[])dr[“Image”];
objProperty.Description=dr[“Description”].ToString();
添加(对象属性);
}
cn.Close();
归还财产;
}
公共字节[]转换为字节(HttpPostedFileBase映像,属性objproperty)
{
如果(图像!=null)
{
byte[]imageBytes=null;
BinaryReader=新的BinaryReader(image.InputStream);
imageBytes=reader.ReadBytes((int)image.ContentLength);
返回imageBytes;
}
其他的
{
byte[]imageBytes=objproperty.Image;
BinaryReader=新的BinaryReader(image.InputStream);
imageBytes=reader.ReadBytes((int)image.ContentLength);
返回imageBytes;
}
}
}
}
使用此代码,我可以上传并保存我的图像到数据库中,我可以从数据中获取所有数据。我也可以得到图像,但在二进制格式中,我认为您可以在观看下面的图像后了解更多信息您使用了错误的型号名称。 改变 到 在视图部分
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace HelphoProject.Models
{
public class Property
{
public int ImageID { get; set; }
public int CityID { get; set; }
public int propertyTypeID { get; set; }
[Required]
public byte[] Image { get; set; }
public string Description { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using HelphoProject.Models;
using System.IO;
namespace HelphoProject.DataAccessLayer
{
public class readwriteProperty
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
SqlCommand cmd;
SqlDataAdapter sda;
DataTable dt;
public string SaveProperty(HttpPostedFileBase file, Property objproperty)
{
objproperty.Image = ConvertToBytes(file,null);
cmd = new SqlCommand("spSaveProperty", cn);
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@intStateID", objCity.StateID);
cmd.Parameters.AddWithValue("@ImageID", objproperty.ImageID);
cmd.Parameters.AddWithValue("@CityID", objproperty.CityID);
cmd.Parameters.AddWithValue("@TypeID", objproperty.propertyTypeID);
cmd.Parameters.AddWithValue("@Image", objproperty.Image);
cmd.Parameters.AddWithValue("@Description", objproperty.Description);
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
return "Datat Not Saved Successfully";
}
cn.Close();
return "Data Saved Successfully";
}
public byte[] getImageFromDB()
{
cn.Open();
sda = new SqlDataAdapter("select Image FROM tblImages", cn);
dt = new DataTable();
sda.Fill(dt);
Property objmainProperty = new Property();
foreach (DataRow dr in dt.Rows)
{
Property objProperty = new Property();
objProperty.Image=(byte[])dr["Image"];
objmainProperty.Image = objProperty.Image;
}
cn.Close();
return objmainProperty.Image;
}
public List<Property> getproperties()
{
cn.Open();
sda = new SqlDataAdapter("select ImageID,CityID,TypeID,Image,Description from tblImages", cn);
dt = new DataTable();
sda.Fill(dt);
List<Property> properties = new List<Property>();
foreach(DataRow dr in dt.Rows)
{
Property objProperty = new Property();
objProperty.ImageID = Convert.ToInt16(dr["ImageID"]);
objProperty.CityID = Convert.ToInt16(dr["CityID"]);
objProperty.propertyTypeID = Convert.ToInt16(dr["TypeID"]);
objProperty.Image = (byte[])dr["Image"];
objProperty.Description = dr["Description"].ToString();
properties.Add(objProperty);
}
cn.Close();
return properties;
}
public byte[] ConvertToBytes(HttpPostedFileBase image,Property objproperty)
{
if (image != null)
{
byte[] imageBytes = null;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}
else
{
byte[] imageBytes = objproperty.Image;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}
}
}
}
img src="/Content/RetrieveImage/@item.ImageID" alt="" height=100 width=200
img src="/Property/RetrieveImage/@item.ImageID" alt="" height=100 width=200