保存并从数据库中检索图像 [英] Save and Retrieve Images from the Database

查看:63
本文介绍了保存并从数据库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试建立商店搜索引擎,我想将图像上传到数据库。我有数据库名称商店它有两个字段 Id,ImageName,ImagePath

帮我从数据库中搜索图像。我不知道如何显示特定商店ID和节目的图像



Iam trying to make a shops search engine now i want to upload a image to database. i have databse name Shops it's have two fields Id,ImageName,ImagePath
Help me to Retrive image from database.i dont know how to show image for specific shop id's and shows

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


public partial class avatar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["VeinConnectionString"].ConnectionString);


        con.Open();
        cmd = new SqlCommand("select * from UserReg where Id", con);
        cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(txtsrno.Text));
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            txtname.Text = dr["Name"].ToString();
            Image1.ImageUrl = "~/Image/" + dr["UserImg"].ToString();

        }

        con.Close();




    }
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader dr;
    protected void save_btn_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("insert into Shops (ImageName,ImagePath) values (@ImageName,@ImagePath)", con);
        cmd.Parameters.AddWithValue("@ImageName", txtname.Text);
        cmd.Parameters.AddWithValue("@ImagePath", imageUpload.FileName);
        imageUpload.SaveAs(Server.MapPath("~/Images/") + imageUpload.FileName);
        cmd.ExecuteNonQuery();
        Message.Visible = true;
        Message.Text = "Add Successfully ";
        con.Close();
        txtname.Text = "";
    }
}

推荐答案

首先使用有效的SQL:

Start by using valid SQL:
cmd = new SqlCommand("select * from UserReg where Id", con);

应该是:

Should probably be:

cmd = new SqlCommand("select * from UserReg where Id=@Id", con);


用它来保存数据:



use this to save data:

con.Open();
cmd = new SqlCommand("insert into Shops (ImageFile) values (@ImageFile)", con);
System.IO.MemoryStream msGoodSrc = new System.IO.MemoryStream();
pictureBox1.Image.Save(msGoodSrc, pictureBox1.Image.RawFormat);
byte[] arrPicGoodSrc = msGoodSrc.GetBuffer();
msGoodSrc.Close();
cmd.Parameters.Add("@ImageFile", DbType.Binary).Value = arrPicGoodSrc;
cmd.ExecuteNonQuery();
con.Close();





这是读取图像数据:





and this is for read image data:

DataTable dt = new DataTable();
con.Open();
cmd = new SqlCommand("select ImageFile from Shops where Id='???'", con);
SQLeDataAdapter dataadapter = new SQLeDataAdapter(cmd);
dataadapter.Fill(dt);
byte[] arrPicture = (byte[])(dt.Rows[0][0]);
System.IO.MemoryStream ms = new System.IO.MemoryStream(arrPicture);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Close();
con.Close();


这篇关于保存并从数据库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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