【问题标题】:retrieve data from database and insert those values to another table in database c#从数据库中检索数据并将这些值插入到数据库c#中的另一个表中
【发布时间】:2017-11-16 13:44:02
【问题描述】:

我在打开连接时遇到错误,例如“连接已打开”。它告诉关闭while循环的打开连接(从表中读取数据)。但是在读取时必须以任何方式将数据插入数据库。如何解决?

private bool iscompanyExist(string comname)
{
    string query = "select * from Ageingrpt where Companyname='" + comname + "' group by Companyname having sum(current)>0 or sum('a1-30')>0 or sum('a31-60')>0 or sum('a61-90')>0 or sum('a90g')>0 ;";
    MySqlCommand cmd = new MySqlCommand(query, cnn);
    MySqlDataReader reader1;
    cnn.Open();
    reader1 = cmd.ExecuteReader();
    if (reader1.HasRows)
    {
        cnn.Close();
        return true;
    }
    else
    {
        cnn.Close();
        return false;
    }
}

private void Ageingcalc()
{
    string duedate = null;
    string compname = null, outstanding = null;
    //90<
    string query = "select c.address1,sum(p.OutstandingAmt) from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
    MySqlCommand cmd = new MySqlCommand(query, cnn);
    MySqlDataReader reader;
    cnn.Open(); //open1
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        compname = reader[0].ToString();
        outstanding = reader[1].ToString();
        if (iscompanyExist(compname) == true)
        {
            cnn.Open();
            query = "update Ageingrpt set a90g='" + outstanding + "';";
            cmd = new MySqlCommand(query, cnn);
            MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
            cnn.Close();
        }
        else
        {
            cnn.Open();
            query = "insert into Ageingrpt  values('" + compname + "',0,0,0,0,'" + outstanding + "',0); ";
            cmd = new MySqlCommand(query, cnn);
            cmd.ExecuteReader(); // query will be executed and data saved to the db
            cnn.Close();
        }
    }

    cnn.Close();
}

【问题讨论】:

  • 为什么不写一个简单的INSERT SELECT FROM WHERE 呢?您尝试的操作将至少慢 2N 倍 - 每次插入一次,每次“检查”一次。对于 10 行,它会慢 20 倍。对于 2000 年,它将慢 2000 倍
  • 当您在 IF ELSE 中再次在 cnn.Open() 中使用 Ageingcalc() 方法时
  • 您在 while 循环中再次打开连接 cnn
  • 谢谢。我可以理解错误。这就是场景。在 while 循环中,将从发票表中检索一组数据。我必须检查在 while 循环阅读器中使用的查询中的每条记录。在每条记录中,将检索“公司名称”。我必须检查“公司”字段是否已在 ageingrpt 表中退出。如果存在,我必须更新ageingrpt 表中的记录,否则将该记录插入老化报告表中。主要的是我必须处理在 while 循环中检索到的每条记录。但错误是要关闭为 while 循环打开的连接。
  • 如果我在 if 条件之前关闭 while 循环连接,我只能处理在循环中检索到的最后一条记录吗?

标签: c# mysql


【解决方案1】:
    private void Ageingcalc()
            {
                string duedate = null;
                string compname = null, outstanding = null;

                //90<
                string query = "select c.address1,sum(p.OutstandingAmt) from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
                MySqlCommand cmd = new MySqlCommand(query, cnn);
                MySqlDataReader reader;
                cnn.Open();         //open1
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    compname = reader[0].ToString();
                    outstanding = reader[1].ToString();
                    if (iscompanyExist(compname) == true)
                    {
                        query = "update Ageingrpt set a90g='" + outstanding + "';";
                        cmd = new MySqlCommand(query, cnn);
                        MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }
                    else
                    {
                        query = "insert into Ageingrpt  values('" + compname + "',0,0,0,0,'" + outstanding + "',0); ";
                        cmd = new MySqlCommand(query, cnn);
                        cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }

                }
                cnn.Close();
            }


once connection is open and need to close it..we should not open again..

【讨论】:

    【解决方案2】:

    这对我有用。我使用 DataTable 来存储检索到的数据。

     private void Ageingcalc()
                {
                    string duedate = null;
                    string compname = null, outstanding = null;
    
    
                    //90<
                    string query = "select c.address1,sum(p.OutstandingAmt) as outstanding from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
                    MySqlCommand cmd = new MySqlCommand(query, cnn);
                    MySqlDataReader reader;
                    cnn.Open();
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    da.Dispose();
                    cnn.Close();
    
                    foreach (DataRow row in dt.Rows)
                    {
    
                        if (iscompanyExist(row["address1"].ToString()) == true)
                        {
                            cnn.Open();
                            MessageBox.Show(row["address1"].ToString());
                            query = "update Ageingrpt set a90g='" + row["outstanding"].ToString() + "';";
                            cmd = new MySqlCommand(query, cnn);
                            MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                            cnn.Close();
                        }
                        else
                        {
                            cnn.Open();
                            MessageBox.Show(row["address1"].ToString());
                            query = "insert into Ageingrpt  values('" + row["address1"].ToString() + "',0,0,0,0,'" + row["outstanding"].ToString() + "',0); ";
                            cmd = new MySqlCommand(query, cnn);
                            MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                            cnn.Close();
                        }
    
    
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2015-03-31
      • 2010-12-02
      • 2023-04-10
      • 1970-01-01
      • 2016-07-19
      相关资源
      最近更新 更多