我有一個名為BookMetaData.cs. 該類具有三個屬性:id、Name和Price。我還有一個BookRepository.cs包含另一個類的檔案,該檔案具有Add()將書保存到myModel.edmx.
這是我的`添加方法:
private CRUDTestEntities db = new CRUDTestEntities();
public bool Add(CRUDTest.BOOK_TBL entity,bool autoSave = true)
{
try
{
db.BOOK_TBL.Add(entity);
if (autoSave)
{
return Convert.ToBoolean(db.SaveChanges());
}
else
return false;
}
catch
{
return false;
}
}
我的表單中有 3 個文本框:id、Name和Price. 如何將這些文本框中的值傳遞給 MyAdd()方法?
我正在嘗試像這樣呼叫該方法,但我不確定如何完成此代碼:
var blBook = new Repositories.BookRepository();
blBook.Add(/* There are two arguments in this method. How do I pass in textboxes?*/);
uj5u.com熱心網友回復:
您必須使用文本框中的值來構造一個與方法的引數型別匹配的新物件,這似乎是CRUDTest.BOOK_TBL. 它看起來像這樣:
var blBook = new Repositories.BookRepository();
var book = new CRUDTest.BOOK_TBL() {
id = txtID.Text,
Name = txtName.Text,
Price = Convert.ToDecimal(txtPrice.Text)
};
blBook.Add(book);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459602.html
上一篇:Microsoft.Data.SqlClient.SqlException(0x80131904)
下一篇:如何首先在代碼中保存外鍵的集合
