[英]Insert linq-to-sql problem?
我正在玩Northwind數據庫,通過閱讀一些教程,我發現Categories應該具有Add方法,但是它不起作用。 缺少了一些東西,我很感謝其他知道更多信息的人。
public void AddCategory(string categoryname)
{
string connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
DataClasses1DataContext db = new DataClasses1DataContext(connstring);
Category category = new Category();
category.CategoryName = categoryname;
// db.Categories.Add(category);
db.SubmitChanges();
}
它應該是
db.Categories.InsertOnSubmit(category);
public void AddCategory(Category category)
{
NorthWindDataContext dataContext = new NorthWindDataContext();
try
{
dataContext.Categories.InsertOnSubmit(category);
dataContext.SubmitChanges();
}
catch (Exception) { }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.