在C#中插入时如何避免重复 [英] How to avoid duplication when inserting in C#

查看:95
本文介绍了在C#中插入时如何避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们首先我遇到了这个问题我无法解决它而且我已经在这里发布了简要说明我将复制状态描述我将解释状态后我会给你我的需要

我有一个带有复选框的DataGridView我单击保存按钮后检查行,通常我检查它们必须插入另一个表中的行,但我发现只是插入两行的行

第二个问题是,当我点击搜索按钮时,我希望我插入的行最多,因为它们已经在表格中分配了

你会发现两个接口处理

我希望我已经很好地描述了我的情况并提前感谢你



我有什么试过:



Hi friends first I am stuck in this problem I could not solve it and I already published here brief I will copy the status description "I will explain the state after I will give you my need
I have a DataGridView with a check box I check the rows after I click the "save" button and normally the rows I check that they will have to be inserted in both the other table but I find that Just a line that was inserted twice
The second problem is that I want the rows I inserted that I want the most to see when I click the search button because they are already assigned in the table
You will find the two interfaces that process the "
I hope I have described my condition well and thank you in advance

What I have tried:

//script search button
private void button4_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            Program.cmd.CommandText = "select * from bon_reception_marche where Date_reception between '" + dateTimePicker1.Value.Date + "' and '" + dateTimePicker2.Value.Date + "' and Id_marche in (select TOP 1 Id_marche from marche where Num_marche = '" + textBox1.Text + "')";
            Program.dr = Program.cmd.ExecuteReader();
            while (Program.dr.Read())
            {
                dataGridView2.Rows.Add(Program.dr[0], Program.dr[2], Program.dr[3], Program.dr[5], Program.dr[6], Program.dr[7], Program.dr[8], Program.dr[9], Program.dr[10], Program.dr[11], Program.dr[12]);
            }
            Program.dr.Close();
        }
        //script click datagridview
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 11/*myColumn*/ && e.RowIndex >= 0 /*myRow*/)
            {
                button1.Enabled = true;
            }
        }
        //script button save :
        private void button1_Click(object sender, EventArgs e)
        {
            int colIndex = dataGridView2.Columns["CheckBox"].Index;
            try
            {
                var rows = dataGridView2.Rows
                .Cast<DataGridViewRow>()
                .Where(row => row.Cells[colIndex].Value != null)
                .Where(row => (bool)row.Cells[colIndex].Value)
                .ToList();
                foreach (DataGridViewRow row in rows)
                    insertRowData(row);
                MessageBox.Show("c'est ajouté avec succés");
            }
            catch (FormatException)
            {
                MessageBox.Show("Only input numbers into the table!",
                "Only Numbers", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                MessageBox.Show("There was an error while saving!",
                "Error", MessageBoxButtons.OK);
            }
        }
        private void insertRowData(DataGridViewRow row)
        {
            double montantValue = Convert.ToDouble(row.Cells["Column7"].Value);
            int id_br_value = Convert.ToInt32(row.Cells["Column11"].Value);
            string check;
            if (checkBox1.Checked == true)
            {
                check = "O";
            }
            else
            {
                check = "N";
            }
            Program.cmd.Parameters.Clear();
            Program.cmd.CommandText = "insert into attachement_marche (Id_bon_reception_marche,Id_marche,Num_attachement,Date_debut,Date_fin,Flag_dernier,Montant,User_create,Date_create) values ( " + id_br_value + ",(select TOP 1 Id_marche from marche where Num_marche = '" + textBox1.Text + "'),'" + textBox3.Text + "','" + dateTimePicker1.Value.Date + "','" + dateTimePicker1.Value.Date + "','" + check + "'," + montantValue + ",'" + values.username + "','" + DateTime.Now.Date + "')";
            Program.cmd.ExecuteNonQuery();
        }

推荐答案

永远不要通过连接用户输入来构建SQL查询,它被命名为SQL注入,它是对您的数据库造成危险并且容易出错。

名称中的单引号和程序崩溃。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]
Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于在C#中插入时如何避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆