插入数据库中的图像 [英] insert Image in database

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

问题描述

我是新来asp.net和数据库!我想保存图像到数据库中的文件上传控件。我已经尝试过,但它不工作就是在点击提交按钮,数据不会被添加到数据库中没有表现出任何差错!这是code,我已经试过

 保护无效ButtonSubmit_Click(对象发件人,EventArgs的发送)
{
    如果(FileUpload1.HasFile&安培;&安培; Page.IsValid)//文件上传,并提交
    {
        字符串fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);        如果(fileExtension.ToLower()!=.JPG)
        {
            Labelupload.Text =只有文件以.jpg扩展名被允许;
            Labelupload.ForeColor = System.Drawing.Color.Red;
        }
        其他
        {
            FileUpload1.SaveAs(使用Server.Mappath(〜/上传/+ FileUpload1.FileName));
            Labelupload.Text =文件上传
            Labelupload.ForeColor = System.Drawing.Color.DeepSkyBlue;            LabelSubmit.Text =已提交成功与
            LabelSubmit.ForeColor = System.Drawing.Color.DeepSkyBlue;
        }
    }
    其他
    {
        Labelupload.Text =请选择一个文件;
        Labelupload.ForeColor = System.Drawing.Color.Red;
        LabelSubmit.Text =无法提交;
        LabelSubmit.ForeColor = System.Drawing.Color.Red;
    }    //插入到数据库
    工作的obj =新作();    / *流FS = FileUpload1.PostedFile.InputStream;
    BinaryReader BR =新BinaryReader(FS);
    字节[]字节= br.ReadBytes((Int32)已fs.Length); * /    obj.listItem_1 = DropDownList1.SelectedValue;
    obj.listItem_2 = DropDownList2.SelectedValue;
    obj.Description = TextBoxdescription.Text;
    obj.Date = TextBoxdate.Text;
    //obj.UploadedImage =字节;    INT K = obj.insertmethod();    TextBoxdescription.Text =;
}

下面是包含 insertmethod()的工作逻辑:

 公共类工作
{
    Clssqlconnection的obj =新Clssqlconnection();    公共字符串listItem_1 {搞定;组; }
    公共字符串listItem_2 {搞定;组; }
    公共字符串描述{搞定;组; }
    公共字符串日期{搞定;组; }
    //公共字节[] {UploadedImage获得;组; }    公众诠释insertmethod()
    {
        obj.str = @插入[assign_Work(listItem_1,listItem_2,说明,日期,UploadedImage)+
             VALUES('+ listItem_1 +,+ listItem_2 +,+说明+','+日期+,+ UploadedImage +');
        返回obj.ExecuteNonQuery();
    }
}


解决方案

图片需要通过一个参数去到数据库中。你不能有它原始的SQL语句。试试这个:

 公众诠释insertmethod()
{    obj.str = @插入[assign_Work(listItem_1,listItem_2,说明,日期,UploadedImage)+
         VALUES('+ listItem_1 +,+ listItem_2 +,+说明+','+日期+',?);
    obj.Parameters.AddWithValue(文件,UploadedImage);    返回obj.ExecuteNonQuery();
}

另外,顺便说一句,你可能要考虑使用参数,所有这些值,以避免注入攻击。举例来说,如果你的描述字段它有一个单引号?

I am new to asp.net and databases! I am trying to save image into database from the file upload control. i have tried it but it isn't working that is upon clicking the submit button, the data does not get added into the database neither showing any error! this is the code which I have tried

protected void ButtonSubmit_Click(object sender, EventArgs e)
{         
    if (FileUpload1.HasFile && Page.IsValid)                //fileUpload and submit
    {
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);

        if (fileExtension.ToLower() != ".jpg")
        {
            Labelupload.Text = "Only Files with .jpg extension are allowed";
            Labelupload.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + FileUpload1.FileName));
            Labelupload.Text = "File Uploaded";
            Labelupload.ForeColor = System.Drawing.Color.DeepSkyBlue;

            LabelSubmit.Text = "Submitted Succesfully";
            LabelSubmit.ForeColor = System.Drawing.Color.DeepSkyBlue;
        }
    }
    else
    {
        Labelupload.Text = "Please select a file";
        Labelupload.ForeColor = System.Drawing.Color.Red;
        LabelSubmit.Text = "Failed to Submit";
        LabelSubmit.ForeColor = System.Drawing.Color.Red;
    }

    // insert into database
    Work obj = new Work();

    /* Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);*/

    obj.listItem_1 = DropDownList1.SelectedValue;
    obj.listItem_2 = DropDownList2.SelectedValue;
    obj.Description = TextBoxdescription.Text;
    obj.Date = TextBoxdate.Text;
    //obj.UploadedImage = bytes;

    int k = obj.insertmethod();

    TextBoxdescription.Text = "";   
}

Here is the Work class that contains the insertmethod() logic:

public class Work
{
    Clssqlconnection obj = new Clssqlconnection();

    public string listItem_1 { get; set; }
    public string listItem_2 { get; set; }
    public string Description { get; set; }
    public string Date { get; set; }
    //public Byte[] UploadedImage { get; set; }

    public int insertmethod()
    {
        obj.str = @"insert into [assign_Work] (listItem_1, listItem_2, Description, Date, UploadedImage)" +
             "values('" + listItem_1 + "','" + listItem_2 + "','" + Description + "','" + Date + "','" + UploadedImage + "')";
        return obj.ExecuteNonQuery();
    }
}

解决方案

The image needs to go into the database via a parameter. You cannot have it in a raw SQL statement. Try this:

public int insertmethod()
{

    obj.str = @"insert into [assign_Work] (listItem_1, listItem_2, Description, Date, UploadedImage)" +
         "values('" + listItem_1 + "','" + listItem_2 + "','" + Description + "','" + Date + "', ?)";
    obj.Parameters.AddWithValue("File", UploadedImage);

    return obj.ExecuteNonQuery();


}

Also, btw, you might want to consider using parameters for all of these values to avoid injection attacks. For instance, what if your Description field had an apostrophe in it?

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

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