linq to sql插入语句不起作用
我有以下代码:
pillboxDataContext db = new pillboxDataContext();
userAccount newUser = new userAccount();
newUser.userName = "test123";
newUser.userPhone = "1234567890";
newUser.userEmail = "[email protected]";
newUser.userPwd = "testpassword";
newUser.userCreateDate = DateTime.Now;
newUser.userAccountType = "basic";
db.users.Add(newUser);
db.SubmitChanges();
userAccount 是我的具有属性的对象(与用户表匹配)。
我对 db.users.Add(newUser); 行感到困惑。 .Add 指出:System.Data.LINQ.Table 不包含“Add”的定义。
我遵循的示例似乎表明应该允许 .Add 。我对此很陌生,所以请提供任何建议都会非常有帮助。
更新:
我将其更改为db.users.InsertOnSubmit(newUser);
但是,我仍然收到错误:
最佳重载方法匹配 'System.Data.Linq.Table.InsertOnSubmit(user)' 有一些无效 参数
后,
我发现我将数据分配给类(userAccount)而不是表(user)。
所以我将: userAccount newUser = new userAccount();
更改为 user newUser = new user();
然后一切都像我预期的那样进行。
感谢您的信息!
I have the following code:
pillboxDataContext db = new pillboxDataContext();
userAccount newUser = new userAccount();
newUser.userName = "test123";
newUser.userPhone = "1234567890";
newUser.userEmail = "[email protected]";
newUser.userPwd = "testpassword";
newUser.userCreateDate = DateTime.Now;
newUser.userAccountType = "basic";
db.users.Add(newUser);
db.SubmitChanges();
userAccount is my object with properties (that match up with the user table).
I'm confused on the db.users.Add(newUser);
line. The .Add is stating: System.Data.LINQ.Table does not contain a definition for 'Add'.
The examples I was following seem to indicate that the .Add should be allowed. I'm new at this so please any advice would be very helpful.
UPDATE:
I changed it to db.users.InsertOnSubmit(newUser);
however, I'm still getting an error:
The best overloaded method match for
'System.Data.Linq.Table.InsertOnSubmit(user)' has some invalid
arguments
Fix!
After looking at other code samples I figured out that I was assigning my data to the class (userAccount) rather than the table (user).
So I changed: userAccount newUser = new userAccount();
to user newUser = new user();
Then everything worked like I expected.
Thanks for the info!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
Add
不起作用 - 我怀疑您想要InsertOnSubmit< /代码>
。
Yes,
Add
won't work - I suspect you wantInsertOnSubmit
.您的代码是:
乔恩建议您的代码应该是:
两者之间存在巨大差异。 IDE 对于这类事情非常有帮助......
Your code is:
Jon suggested that your code should be:
There is an enormous difference between the two. An IDE is very helpful for this sort of thing...