asp.net如何将记录插入数据库

时间:2014-04-15 08:41:44

标签: c# asp.net web-services

我在使插入方法正常工作时遇到困难。我是后期开发的新手,所以任何建议或意见都会有所帮助。

public Boolean insertDefaultUser()
    {
        Boolean flag = true;
        Users newUser = new Users();
        newUser.alias = "bulby";
        newUser.password = "chicken";
        newUser.email = "r@hot.com";

        dbc.Users.AddObject(newUser);   // ERROR !
        dbc.SaveChanges();
        return flag;

    }

但是,在“添加对象行”上,它给出了以下错误--->

Error "The best overloaded method match for System.Data.Objects.ObjectSet<Guild_Chat.User>.AddObject(Guild_Chat.User)' has some invalid arguments ".

1 个答案:

答案 0 :(得分:1)

尝试添加实际的实体类型Guild_Chat.User,例如:

public bool InsertDefaultUser()
{
    try
    {
       Guild_Chat.User newUser = new Guild_Chat.User
                              {
                                 alias = "bulby",
                                 password = "chicken",
                                 email = "r@hot.com"
                              };
       dbc.Users.AddObject(newUser);
       dbc.SaveChanges();
       return true;
    }
    catch(Exception e)
    {
       return false;
    }

}