[英]Insert linq-to-sql problem?
I'm playing around with the Northwind database and by reading some tutorials I see that Categories should have an Add method, but it ain't working. 我正在玩Northwind数据库,通过阅读一些教程,我发现Categories应该具有Add方法,但是它不起作用。 Something is missing and I would appreciate someone else who knows more for some feedback. 缺少了一些东西,我很感谢其他知道更多信息的人。
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.