以下是我的代码...我正在尝试将图像存储在数据库中。我使用varbinary(MAX)数据类型在数据库中存储数据字节。请告诉我哪里出错了。
int qtype = Convert.ToInt32(ddl_q_Type.SelectedValue);
var getQ = (from q in obj.QuestionRegistrations
orderby q.QueCode ascending
where q.QueQuesType == qtype && q.QueLanguageCode == 1
select new { q.QueCode, q.QueQuestion }
).ToArray();
index = Convert.ToInt32(ViewState["index"].ToString()) + 1;
ViewState["index"] = index;
previosIndex = index - 1;
ViewState["previosIndex"] = previosIndex;
txt_question.Text = getQ[index].QueQuestion;
lblqcode.Text = getQ[index].QueCode.ToString();
int qcode = Convert.ToInt32(lblqcode.Text);
var options = (from opt in obj.QuestionAndOptionsMappings
where opt.QueMapQuestionCode == qcode
select new { opt.QueMapOptions, opt.QueMapCorrectAnswer, opt.QueMapId, opt.QueMapImage }
).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("QueMapOptions", typeof(string));
dt.Columns.Add("QueMapId", typeof(string));
dt.Columns.Add("QueMapImage", typeof(string));
foreach (var y in options)
{
DataRow dr = dt.NewRow();
dr["QueMapOptions"] = y.QueMapOptions;
dr["QueMapId"] = y.QueMapId;
dr["QueMapImage"] = (Byte[])y.QueMapImage;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
此代码用于存储数据库中的图像
for (int i = 0; i < GridView1.Rows.Count; i++)
{
FileUpload fp = (FileUpload)GridView1.Rows[i].Cells[2].FindControl("fp");
Label lblmapid = (Label)GridView1.Rows[i].Cells[1].FindControl("lblmapid");
bool e1 = fp.HasFile;
int mapiidd = Convert.ToInt32(lblmapid.Text);
if (fp.HasFile)
{
string path = Server.MapPath("~/Uploads/") + fp.FileName;
fp.SaveAs(path);
byte[] imageBytes =
File.ReadAllBytes(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/") + fp.FileName);
int qcodelbl = Convert.ToInt32(lblqcode.Text);
QuestionAndOptionsMapping qmap = new QuestionAndOptionsMapping();
var update = obj.QuestionAndOptionsMappings.Where(q => q.QueMapId == mapiidd && q.QueMapQuestionCode == qcodelbl)
;
update.SingleOrDefault().QueMapImage = imageBytes;
obj.SaveChanges();
}
}
ASPX代码
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="OPTIONS" HeaderStyle-Width="55%">
<ItemTemplate>
<asp:TextBox ID="Option" runat="server" Text='<%#Eval("QueMapOptions") %>' Height="40px"
Width="500px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%" Visible="false">
<ItemTemplate>
<asp:Label ID="lblmapid" runat="server" Text='<%#Eval("QueMapId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:CheckBox ID="Chk_correct_Ans" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:FileUpload ID="fp" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Section" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("QueMapImage") %>' Height="80px"
Width="100px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:1)
好的,您不能将二进制列绑定到GridView
列。你需要使用Response.BinaryWrite
方法。
以下几个步骤可以实现您的目标
创建一个通用处理程序来读取二进制数据。我们称之为 ImageHandler.ashx 。确保将表的主键传递给此处理程序。编写下面的处理程序代码(仅作为示例)。
public void ProcessRequest (HttpContext context)
{
string connectionString = ConfigurationManager.ConnectionStrings [&#34; DBConnection&#34;]。ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select [Content] from Images where ID =@ID";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlParameter ImageID = new SqlParameter("@ID", SqlDbType.BigInt);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
conn.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Content"]);
dReader.Close();
conn.Close();
}
并像这样调用处理程序。
ImageUrl =&#39;&lt;%#&#34; ImageHandler.ashx?ID =&#34; + Eval(&#34; ID&#34;)%&gt;&#39;
而不是
ImageUrl='<%#Eval("QueMapImage") %>'
Here's我从上面的例子中提取的一个完整示例。
一切顺利!