如何在一次插入两行时避免重复 [英] How to avoid duplicating when inserting both lines at once
问题描述
你好朋友首先我会解释状态后我会给你我的需要
我有一个带有复选框的datagridview我点击保存按钮后检查线条通常是线条我检查它们是否必须插入另一个表中,但我发现只有一行是插入两次
第二个问题是我想要插入的行我想要最多看到的我点击搜索按钮,因为已经在表中分配了
你会发现处理状态的两个接口
我希望我已经很好地描述了我的状况并且谢谢您提前预订
我尝试过:
hello friends first I will explain the state after I will give you my need
I have a datagridview with a checkbox I check the lines after I click on the "save" button and normally the lines I check they will have to be inserted both in the other table but I find that just one line that was Insert twice
The second problem is that I want the lines I inserted I want the most to see when I click on the search button because there are already assigned in the table
You will find the two interfaces that process the state
I hope I have described my condition well and thank you in advance
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace gestion_attachements_decomptes
{
public partial class ajouter_attachement : Form
{
public ajouter_attachement(string num_marche,string libelle_fournisseur)
{
InitializeComponent();
textBox1.Text = num_marche;
textBox2.Text = libelle_fournisseur;
}
private void ajouter_attachement_Load(object sender, EventArgs e)
{
//désactiver button enregistrer
button1.Enabled = false;
//ajouter checkbox dans datagrid view
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "";
chk.Name = "CheckBox";
dataGridView2.Columns.Add(chk);
dataGridView2.AllowUserToAddRows = false;
dataGridView2.AllowUserToDeleteRows = false;
}
private void search_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();
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
//check the lines in datagridview
if (e.ColumnIndex == 11/*myColumn*/ && e.RowIndex >= 0 /*myRow*/)
{
button1.Enabled = true;
}
}
private void save_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注入 [ ^ ]
-----
当你不明白你的代码做了什么或者它为什么会这样做,答案是调试器。
使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]
调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Never build an sql query by concatenating with user inputs, it is dangerous for your database and error prone.
SQL injection - Wikipedia[^]
SQL Injection[^]
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
这篇关于如何在一次插入两行时避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!