我在数据库中添加用户时创建了一个简单的查询,但出现错误,数据未显示。这是我的代码
private void button1_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
{
//Instantiate a new Hasher Object
var hasher = new Hasher();
hasher.SaltSize = 16;
//Encrypts The password
var encryptedPassword = hasher.Encrypt(txtPass.Text);
Account newUser = new Account();
newUser.accnt_User = txtUser.Text;
newUser.accnt_Position = txtPosition.Text;
// Replace AccountTableName with the actual table
// name found in your dbml's context
myDbContext.Accounts.InsertOnSubmit(newUser);
myDbContext.SubmitChanges();
MessageBox.Show("XD");
}
}
我的表数据仅显示此
最佳答案
您需要直接在上下文上设置InsertOnSubmit
:
编辑:添加了Using语句。友情提醒@David Khaykin
using (DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath))
{
//Instantiate a new Hasher Object
var hasher = new Hasher();
hasher.SaltSize = 16;
//Encrypts The password
var encryptedPassword = hasher.Encrypt(txtPass.Text);
Account newUser = new Account();
newUser.accnt_User = txtUser.Text;
newUser.accnt_Position = txtPosition.Text;
newUser.accnt_Position = encryptedPassword;
// Replace AccountTableName with the actual table
// name found in your dbml's context
myDbContext.AccountTableName.InsertOnSubmit(newUser);
myDbContext.SubmitChanges();
}
关于c# - LINQ TO SQL 中的查询未插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435977/