c# - LINQ TO SQL 中的查询未插入

标签 c# .net linq linq-to-sql

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

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");
            }
        }

我的表数据仅显示此

enter image description here

最佳答案

您需要直接在上下文上设置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/

相关文章:

c# - Linq 查询给出不适当的输出

c# - LINQ- 组合多个 List<T> 并按值排序 (.Net 3.5)

c# - Windows Phone 8 MVVM Linq 表在 NotifyPropertyChanging 上创建新实例

c# - 更新切换按钮检查其中一个切换时的状态

javascript - google-visualization-errors : container is null. 消息:一名或多名参与者未能绘制()

.net - 从 64 位 .NET 程序使用 32 位 COM 服务器

c# - 使用带有进度报告的 Async/Await 下载并提取 zip 文件

c# - 在 Open XML SDK 中的单词书签后插入 OpenXmlElement

c# - 如何使用 XmlSerializer 获取 XML 元素的内容?

c# - 如何限制设计师的图像大小?