簡體   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