繁体   English   中英

Base64 字符串在数据库中保存为空 C#

[英]Base64 string saves in database as null C#

我在将图像转换为 Base64 字符串并将其保存为数据库中的字符串时遇到问题,但它什么都不返回。 Base64Text 是全局变量,变量也不为空我用按钮来填充文本框进行了测试,它只是将“”保存到数据库中。

这是数据库中表的模型

    public class Product
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public double ProductPrice { get; set; }
        public int ProductAmount { get; set; }
        public string ProductImage { get; set; } // Used for storing image string
        public int userID { get; set; }
    }
// Here is image converter
        private void btnAddImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image Files(*.BMP;*.JPG;*.PNG;*.JPEG)|*.BMP;*.JPG;*.PNG;*.JPEG" +
                "|All files(*.*)|*.*";
            dialog.CheckFileExists = true;
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var image = new Bitmap(dialog.FileName);
                pictureBoxProductImage.Show();
                pictureBoxProductImage.Image = (Image)image;

                byte[] imageArray = System.IO.File.ReadAllBytes(dialog.FileName);
                base64Text = Convert.ToBase64String(imageArray);
            }
        }
// Here is saving image using Entity framework
        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            string imagePath = base64Text;
            if (txtBoxProductName.Text == null || txtBoxProductPrice.Text == null || txtBoxProductQuantity.Text == null || imagePath == null || imagePath == "")
            {
                MessageBox.Show("Please fill required information!", "", MessageBoxButtons.OK);
            }
            else
            {
                model.ProductName = txtBoxProductName.Text;
                model.ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text);
                model.ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text);
                model.ProductImage = imagePath;
                model.userID = id;

                using (var context = new ProductContext())
                {
                    context.Products.Add(model);
                    context.SaveChanges();
                }
                MessageBox.Show("Product sucesffuly added to database!", "", MessageBoxButtons.OK);
                Clear();
            }
        }

您看起来正在危险地混合变量范围。 考虑到这段代码:

model.ProductName = txtBoxProductName.Text;
model.ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text);
model.ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text);
model.ProductImage = imagePath;
model.userID = id;

using (var context = new ProductContext())
{
    context.Products.Add(model);
    context.SaveChanges();
}

'model' 不限于此方法。

我不建议将视图绑定到实体作为视图模型,而是使用普通的 C# ViewModel 类来避免混淆和处理具有多个关注点的实体。 (属性更改跟踪、UI 验证行为等)

它应该看起来更像:

using (var context = new ProductContext())
{
    var product = new Product
    {
        UserId = id,
        ProductName = txtBoxProductName.Text,
        ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text),
        ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text),
        ProductImage = hiddenImage.Text, // Something in the scope of the view storing the image Base64 content, not a global variable.
    };
    context.Products.Add(product);
    context.SaveChanges();
}

添加实体时,您要确保处理的是该实体的新实例,而不是引用。 model可能是对上下文已经跟踪的另一个实体的引用,这意味着当您认为要添加新行时,最终可以替换完全不同记录上的值。 此外,由于您正在确定新的 DbContext 实例的范围,因此该范围之外的任何实体引用都可能导致异常。

避免任何细节的全局变量。 选择图像时,请确保 Base64 存储在应用程序页面或绑定视图模型范围内的某处。 如果在这些更改之后仍然没有 ProductImage 被保存,那么我会查看 Entity 声明以确保它没有被标记为Ignore ,并考虑使用探查器来查看正在生成的 SQL 以确保该字段被包括以及正在发送的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM