在c#中复制记录

时间:2017-05-12 19:42:49

标签: c# winforms

我正在制作一个考勤系统,现在这是我的问题,在我搜索一个人的名字并尝试登录他出席之后,一开始就没问题,在登录第二个名字之后它仍然是精细。但是一旦我尝试编辑第一个或第二个用户的登录出勤率,我的datagridview(连接到我的数据库)中的所有值都会重复。如果我输入name1参加我的第一周就可以了。第1周出席的名字2仍然没问题。 但如果我编辑相同的名称。或者甚至转到下一周的数字,所有保存的值都根据我最近输入的名称重复。 用于插入新记录

    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();
                }

和更新

             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();
                }`}

1 个答案:

答案 0 :(得分:0)

尼卡,我想我可以提供帮助。您可以使用较少的编码线轻松完成您要做的事情。通过使用更多对象

,可以显示,编辑,删除整个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();
        }
    }
}

作为正在发生的事情的快速概述,SqlDataAdapter保存了四个查询,但是从SELECT命令自动创建了三个查询。使用SqlCommandBuilder可以添加其他三个,尽管调试器不会将它们显示为除NULL之外的任何内容。 adapter.Fill()和adapter.Update()根据datagridview中每行的RowState处理所有不同的命令。无论如何,“取消”看起来有点过分了。