【问题标题】:How to check if username is already exists in data table with LINQ TO SQL如何使用 LINQ TO SQL 检查用户名是否已存在于数据表中
【发布时间】:2016-09-23 06:32:45
【问题描述】:

我正在使用 Visual Studio C#;

var d = new linqclassDataContext();
var c = new Table();
            c.Id =System.Convert.ToInt32(confirmationCode);
            c.username = txtUserName.Text;
            c.password = txtPaswword.Text;
            c.mail = txtMail.Text;
            d.Tables.InsertOnSubmit(c);
            d.SubmitChanges();

我想检查用户名是否已经在 DT 中 请帮忙...

【问题讨论】:

    标签: c# sql database linq visual-studio


    【解决方案1】:

    有多种方法可以做到。您可以使用Any,如下所示:

    if (!d.Tables.Any(t => t.username.Equals(txtUserName.Text)))
    {
       d.Tables.InsertOnSubmit(c);
       d.SubmitChanges();
    }
    

    注意:这不考虑大小写。如果需要,您可以考虑使用ToLower() 或ToUpper()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      相关资源
      最近更新 更多