c#新手,我正在尝试将新记录保存到数据库表'ProductTbl‘中。该程序首先运行具有多个输入的windows表单,即描述、价格、类别和图像。然后单击保存按钮,这应该会将新记录保存到表中。下面是savebtn点击代码。
private void btnSave_Click(object sender, EventArgs e)
{
ProductTbl product = new ProductTbl();
product.Description = txtDescription.Text;
product.Price = decimal.Parse(txtPrice.Text);
product.Image = byteBLOBData;
product.ProductType = (int)cboCategory.SelectedValue;
cse.AddToProductTbl(product);
cse.SaveChanges();
MessageBox.Show("Record Saved");
}在cse.AddToProductTbl(产品);标记之后,是数据库上下文中的这段代码。
internal void AddToProductTbl(ProductTbl product)
{
throw new NotImplementedException();
}我不断地收到同样的错误;
System.NotImplementedException: 'The method or operation is not implemented.'任何帮助都会很感激,
提前谢谢你,雅各布
发布于 2021-05-15 10:38:15
是的,您没有实现"AddToProductTbl(ProductTbl产品)“方法。实现该方法。
internal void AddToProductTbl(ProductTbl product)
{
//implementation for actually adding product to the product table
}确保还实现了"SaveChanges()“方法
发布于 2021-05-17 13:43:12
@ Jacobrussell16。为了在数据库表"ProductTbl“中插入和保存新记录,您可以使用现有的 add 方法(Context.TableName.Add())添加记录并删除AddToProductTbl(ProductTbl产品)方法。您可以参考下面的代码示例。
更新:
1.在数据库表中添加image列,并将其类型设置为varbinary(MAX)。
Form.cs中的代码:
ProductTblContext cse = new ProductTblContext ();
private void btnSave_Click(object sender, EventArgs e)
{
ProductTbl product = new ProductTbl ();
product.Id = int.Parse(txtId.Text);
product.Name = txtProduct.Text;
product.price = decimal.Parse(txtPrice.Text);
product.description = txtDescription.Text;
product.type = Convert.ToInt32(comboBox1.SelectedValue.ToString());
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Dispose();
product.image = ms.ToArray();
cse. ProductTbl.Add(product);
cse.SaveChanges();
MessageBox.Show("record saved");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "@.jpg|*JPG|@.Png|*png|@Gif|*.gif|@.All files|*.*";
DialogResult dr = openFileDialog.ShowDialog();
if (dr == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog.FileName,FileMode.Open,FileAccess.Read);
Image im = Image.FromStream(fs);
pictureBox1.Image = im;
}
} ProductTblContext的相关代码如下:
public partial class ProductTblContext: DbContext
{
public ProductTblContext()
: base("name= ProductTblContext")
{
}
public virtual DbSet<ProductTbl> ProductTbl{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProductTbl>()
.Property(e => e.Name)
.IsFixedLength();
modelBuilder.Entity<ProductTbl>()
.Property(e => e.price)
.HasPrecision(18, 0);
modelBuilder.Entity<ProductTbl>()
.Property(e => e.description)
.IsFixedLength();
}
}结果如图所示:

https://stackoverflow.com/questions/67542790
复制相似问题