从数据库中检索数据并将这些值插入数据库c#中的另一个表

时间:2017-11-16 13:44:02

标签: c# mysql

我在连接打开时遇到错误,例如“连接已打开”。它告诉关闭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();
}

2 个答案:

答案 0 :(得分:0)

    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..

答案 1 :(得分:0)

这对我有用。我使用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();
                    }


                }

            }