繁体   English   中英

如何避免记录重复

[英]How to avoid record duplication

如果我尝试将Employee提交到数据库中已经存在其电子邮件地址的数据库中,我想抛出Message框让用户知道问题,否则让数据被保存。

下面是我当前的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Employees
{
    public partial class Registration : Form
    {
        SqlConnection con;
        SqlCommand com;
        SqlDataReader sdr;
        string cmdstr;

        string constr = @"Data Source=DESKTOP-J6KRI77\SQLEXPRESS; Initial Catalog = SELLnBUY; Integrated Security=true";


        public Registration()
        {
            InitializeComponent();
        }

 private void btnsubmit_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(constr);
            cmdstr = "INSERT INTO Emptbl([First Name], [Last Name], sex, [Birth Date], Email, Password, Confirm_Password, Membership) VALUES(@fname, @lname, @sex, @dob, @email, @password, @confirmpassword, @membership)";

            con.Open();
            com = new SqlCommand(cmdstr, con);

            if (txtfname.Text == "" || txtlname.Text == "" || combosex.SelectedItem.ToString() == "" || maskeddob.Text == "" || txtemail.Text == "" || txtpassword.Text == "" || txtconfirmpassword.Text == "" || (!radiopremium.Checked && !radioregular.Checked))
            {
                MessageBox.Show("Please, fill all the fields");
            }

            else
            {
                com.Parameters.AddWithValue("@fname", txtfname.Text);
                com.Parameters.AddWithValue("@lname", txtlname.Text);
                com.Parameters.AddWithValue("@sex", combosex.SelectedItem.ToString());
                com.Parameters.AddWithValue("@dob", maskeddob.Text);
                com.Parameters.AddWithValue("@email", txtemail.Text);
                com.Parameters.AddWithValue("@password", txtpassword.Text);
                com.Parameters.AddWithValue("@confirmpassword", txtconfirmpassword.Text);
                if (radiopremium.Checked)
                {
                    com.Parameters.AddWithValue("@membership", "Premium");
                }
                else
                {
                    com.Parameters.AddWithValue("@membership", "Regular");
                }
                com.ExecuteNonQuery();

                MessageBox.Show("Thanks for registering");
                txtfname.Clear();
                txtlname.Clear();
                combosex.Text = "";
                maskeddob.Clear();
                txtemail.Clear();
                txtpassword.Clear();
                txtconfirmpassword.Clear();
                grpbxmembership.Text = "";

                txtfname.Focus();

            }
con.Close();
}

首先,您的代码很旧。 如今,所有人都在使用ORM。

然后,关于您的问题,您只需要在插入DB之前检查DB中是否存在相同的数据。

SqlCommand command = new SqlCommand("select count(*) from Emptbl where  Email= @email");
  command.Parameters.AddWithValue("@email", txtemail.Text);
  var count= (Int32)cmd.ExecuteScalar();
   if(count>0) 
       //Show error

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM