简体   繁体   English

在c#中复制记录

[英]Duplicating records in c#

I am making a attendance system, and here is my problem now, after i searched for a name of a person and try to log him in for attendance, it is fine at first, after logging in the second name it is still fine. 我正在制作一个考勤系统,现在这是我的问题,在我搜索了一个人的名字并尝试登录他出席之后,一开始就没问题,在登录第二个名字之后它仍然没问题。 but once i tried to edit the login attendance of the first or second user, all the values in my datagridview(connected to my database) became duplicated. 但是一旦我尝试编辑第一个或第二个用户的登录出勤率,我的datagridview(连接到我的数据库)中的所有值都会重复。 if i enter name1 for attendance in my week1 it is fine. 如果我输入name1参加我的第一周就可以了。 name2 for attendance in week1 is still fine. 第1周出席的名字2仍然没问题。 but if i edit the same name. 但如果我编辑相同的名称。 or even go to the next week number, all of the saved values got duplicated based on my recent inputed name. 或者甚至转到下一周的数字,所有保存的值都根据我最近输入的名称重复。 for inserting new records 用于插入新记录

    SqlConnection cnn200 = new SqlConnection(connectionstring);
            string sql200 = "SELECT * FROM attendance WHERE csign=@csign ";
            cnn200.Open();
            SqlCommand cmd200 = new SqlCommand(sql200, cnn200);
            SqlDataReader rdr200;
            cmd200.Parameters.AddWithValue("@csign", callsign);

            rdr200 = cmd200.ExecuteReader();

                if (rdr200.Read() == true)

                {
            SqlConnection cnn201 = new SqlConnection(connectionstring);
                    if (textBox89.Text == "1")
                    {
                        string sql201 = "insert INTO attendance  
           (csign,name,week1)" + "VALUES" + "(@csign,@name,@week1)";
                        cnn201.Open();
                        SqlCommand cmd201 = new SqlCommand(sql201, cnn201);
                        cmd201.Parameters.AddWithValue("@csign", callsign); 
       cmd201.Parameters.AddWithValue("@name", namee);
                        cmd201.Parameters.AddWithValue("@week1", 
          comboBox1.Text);

                        cmd201.ExecuteNonQuery();
                    }
                if (textBox89.Text == "2")
                {
                    string sql201 = "insert INTO attendance  
              (csign,name,week2)" + "VALUES" + "(@csign,@name,@week2)";
                    cnn201.Open();
                    SqlCommand cmd201 = new SqlCommand(sql201, cnn201);
                    cmd201.Parameters.AddWithValue("@csign", callsign); 
           cmd201.Parameters.AddWithValue("@name", namee);
                    cmd201.Parameters.AddWithValue("@week2", 
              comboBox1.Text);

                    cmd201.ExecuteNonQuery();
                }

and for updating 并进行更新

             else{
             SqlConnection cnn201 = new SqlConnection(connectionstring);
                    if (textBox89.Text == "1")
                    {
                        string sql201 = "UPDATE attendance SET 
          name=@name,csign=@csign,week1=@week1";
                        cnn201.Open();
                        SqlCommand cmd201 = new SqlCommand(sql201, cnn201);

                       cmd201.Parameters.AddWithValue("@name", namee);   
                  cmd201.Parameters.AddWithValue("@csign", callsign);
                        cmd201.Parameters.AddWithValue("@week1", 
                    comboBox1.Text);

                        cmd201.ExecuteNonQuery();
                    }
                if (textBox89.Text == "2")
                {
                    string sql201 = "UPDATE attendance SET 
                   name=@name,csign=@csign,week2=@week2";
                    cnn201.Open();
                    SqlCommand cmd201 = new SqlCommand(sql201, cnn201);

                    cmd201.Parameters.AddWithValue("@name", namee); 
                     cmd201.Parameters.AddWithValue("@csign", callsign);
                    cmd201.Parameters.AddWithValue("@week2", 
                    comboBox1.Text);

                    cmd201.ExecuteNonQuery();
                }`}

nica, I think I can help. 尼卡,我想我可以帮忙。 What you are trying to do can be done easily with less coding lines. 您可以使用较少的编码线轻松完成您要做的事情。 An entire DataGridView can be displayed, edited, deleted and have no duplicates by using a few more objects 通过使用更多对象,可以显示,编辑,删除整个DataGridView并且不会重复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;


namespace ADO_NET_Testbed
{
    public partial class MainForm : Form
    {
        private SqlDataAdapter adapter;
        private string connectionString = @"Data Source=Server;Persist Security Info=True;Password=password!;User ID=sooperuser;Initial Catalog=Database";
        private string sqlcommand = @"SELECT OPID, LastName, FirstName, Title, PhoneOffice, PhoneCell, Email, Active, Admin, Tester, Educator, Developer FROM AuditUser";
        private SqlCommandBuilder cmdBuilder = new SqlCommandBuilder();
        private DataTable datatable = new DataTable();
        private DataSet dataset = new DataSet();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            adapter = new SqlDataAdapter(sqlcommand, connectionString);
            adapter.Fill(dataset, "AuditUser");
            dgvUsers.DataSource = dataset.Tables[0];
            dgvUsers.Enabled = true;
            this.Show();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataset.Clear();
            dataset.Reset();
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            cmdBuilder.DataAdapter = adapter;
            adapter.Update(dataset.Tables[0]);
            this.Close();
        }
    }
}

As a quick rundown of what is going on, the SqlDataAdapter holds the four queries, but auto-creates three from the SELECT command. 作为正在发生的事情的快速概述,SqlDataAdapter保存了四个查询,但是从SELECT命令自动创建了三个查询。 Using a SqlCommandBuilder enables the other three to be added, although the debugger will not show them as anything but NULL. 使用SqlCommandBuilder可以添加其他三个,尽管调试器不会将它们显示为除NULL之外的任何内容。 The adapter.Fill() and adapter.Update() handle all the different commands based on the RowState of each row in the datagridview. adapter.Fill()和adapter.Update()根据datagridview中每行的RowState处理所有不同的命令。 "Cancel" is overkill seeing as this form is closed anyway. 无论如何,“取消”看起来有点过分了。

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

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