Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于控制某些项的LINQ查询_C#_Linq_Linq To Sql - Fatal编程技术网

C# 用于控制某些项的LINQ查询

C# 用于控制某些项的LINQ查询,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有一个C语言的windows窗体项目。我的窗体上有两个文本框。 第一个文本框表示姓名,第二个文本框表示姓氏 每当用户按下TAB按钮时,我都想从数据库中检查是否有像我的第一个文本框那样的名称。如果有,则它将移动到下一个控件。如果不是,则我的程序将要求使用LINQ to SQL将此名称保存到DB?您要附加到控件上的验证事件。关于ExitFocus事件不确定第一个文本框的事件名称请从DB中选择文本框中的文本 如果您的计数为0,则向用户显示一个消息框,提示他检查是否要保存用户名或获取姓氏并将其填充到第

我有一个C语言的windows窗体项目。我的窗体上有两个文本框。 第一个文本框表示姓名,第二个文本框表示姓氏


每当用户按下TAB按钮时,我都想从数据库中检查是否有像我的第一个文本框那样的名称。如果有,则它将移动到下一个控件。如果不是,则我的程序将要求使用LINQ to SQL将此名称保存到DB?

您要附加到控件上的验证事件。

关于ExitFocus事件不确定第一个文本框的事件名称请从DB中选择文本框中的文本


如果您的计数为0,则向用户显示一个消息框,提示他检查是否要保存用户名或获取姓氏并将其填充到第二个文本框中。首先,您需要定义并附加第一个文本框的休假事件,假设其id为textBox1,如下所示:

        this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
        private void textBox1_Leave(object sender, EventArgs e)
        {
            DataClasses1DataContext linqToSqlDataContext = new DataClasses1DataContext();
            if (linqToSqlDataContext.employees.Any(x => x.Name == textBox1.Text))
            {
                //move focus to surname text box control as the name already exists in DB
                textBox2.Focus();
            }
            else
            {
                //add a new entity into database for the name entered by the user
                linqToSqlDataContext.employees.Attach(new employee { Name = textBox1.Text });
                linqToSqlDataContext.SubmitChanges();

            }
        }
然后,在其相应的leave事件处理程序中,您可以编写适当的逻辑来检查用户输入的名称是否已经存在于使用LINQ to SQL的数据库中。根据调查结果,您可以使用if语句做出适当的决定,如下所示:

        this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
        private void textBox1_Leave(object sender, EventArgs e)
        {
            DataClasses1DataContext linqToSqlDataContext = new DataClasses1DataContext();
            if (linqToSqlDataContext.employees.Any(x => x.Name == textBox1.Text))
            {
                //move focus to surname text box control as the name already exists in DB
                textBox2.Focus();
            }
            else
            {
                //add a new entity into database for the name entered by the user
                linqToSqlDataContext.employees.Attach(new employee { Name = textBox1.Text });
                linqToSqlDataContext.SubmitChanges();

            }
        }