我已经在数据库中添加用户时创建了一个简单的查询,但是错误是未显示数据。这是我的代码

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

编辑:添加使用语句。提醒@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();
}

10-06 13:45