每次加载表单时如何停止在数据库中复制数据? [英] How to stop duplicating the data in database everytime the form loads?

查看:38
本文介绍了每次加载表单时如何停止在数据库中复制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立一个系统,其中一项要求是针对库存中可用产品的总数生成报告.

I am currently making a system where one of the requirements is to generate a report for the total number of products available in the inventory.

我一直在使用此代码来获取数据库表中每个品种的数量总和.

I've been using this code to get the sum of the quantity of each variety in the database table.

public void ShowData()
    {
        con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
        con.Open();
        da = new SqlDataAdapter("SELECT * FROM Reports", con);
        dt = new DataTable();
        da.Fill(dt);
        dgv_Reports.DataSource = dt;
    }

    private void frmReports_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
        con.Open();
        cmd = new SqlCommand("INSERT INTO Reports (Variety, Quantity) SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety", con);
        cmd.ExecuteNonQuery();
        ShowData();


    }
    

但是我的问题是每次加载表单时,数据都在重复或不断添加到数据库中.

but my problem is every time the form loads, the data is duplicating or keeps on adding onto the database.

推荐答案

我不了解您的报告"表格,...如果我是您,我只会在查询中使用ShowData(),就像这样

I don't understand the need of your "reports" table,... If I was you I'll use only ShowData() with the query, like this

public void ShowData()
    {
        con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
        con.Open();
        da = new SqlDataAdapter("SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety", con);
        dt = new DataTable();
        da.Fill(dt);
        dgv_Reports.DataSource = dt;
    }
    private void frmReports_Load(object sender, EventArgs e)
    {
        ShowData();
    }

这篇关于每次加载表单时如何停止在数据库中复制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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