插入linq-to-sql问题?

时间:2010-02-14 11:04:36

标签: c# linq

我正在玩Northwind数据库并阅读一些tutorials我看到类别应该有一个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(); 
        }

2 个答案:

答案 0 :(得分:3)

应该是

db.Categories.InsertOnSubmit(category);

答案 1 :(得分:0)

        public void AddCategory(Category category)
        {
            NorthWindDataContext dataContext = new NorthWindDataContext();
            try
            { 
             dataContext.Categories.InsertOnSubmit(category);
             dataContext.SubmitChanges();
            }
            catch (Exception) { }
        }