如何使用C#将图像插入数据库表

问题描述 投票:-3回答:3

我正在尝试将图像插入数据库表中,但是在选择图像后,上载的图像在表中显示为NULL。帮助我将图像正确上传到数据库中。

这是我的将数据插入数据库表的代码:

protected void addProduct_Click(object sender, EventArgs e)
{
    using (TradersRehmanEntities db = new TradersRehmanEntities())
    {
        tblProduct ad = new tblProduct();
        ad.ProductName = txtProducttName.Text;
        ad.ProductPrice = txtSalePrice.Text;
        ad.SalePrice = txtSalePrice.Text;
        ad.StockAvaliable = txtStock.Text;
        ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);

        ad.ProductImage = flImageUpload.SaveAs('ProductImage' + flImageUpload.FileName);

        db.tblProducts.Add(ad);

        db.SaveChanges();

        Response.Write("Product added successfully");
    }
} 
c# asp.net webforms entity-framework-6
3个回答
0
投票

[尝试将其另存为blob。但是最好将其保存在对象存储区(例如Amazon S3)中,并在db中保留对其的引用


0
投票

尝试一下-从.FileBytes控件的FileUpload属性中获取上载文件的字节数组:

protected void addProduct_Click(object sender, EventArgs e)
{
    using (TradersRehmanEntities db = new TradersRehmanEntities())
    {
        tblProduct ad = new tblProduct();
        ad.ProductName = txtProducttName.Text;
        ad.ProductPrice = txtSalePrice.Text;
        ad.SalePrice = txtSalePrice.Text;
        ad.StockAvaliable = txtStock.Text;
        ad.CategoriesId = Convert.ToInt32(catergoriesDDL.SelectedValue);

        // grab the file's byte array from the FileUpload control    
        ad.ProductImage = flImageUpload.FileBytes();

        db.tblProducts.Add(ad);

        db.SaveChanges();

        Response.Write("Product added successfully");
    }
} 

-1
投票

您可以将图像作为字节数组存储在数据库中。

© www.soinside.com 2019 - 2024. All rights reserved.