简体   繁体   English

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

[英]retrieve data from database and insert those values to another table in database c#

I'm getting error in connection open like "The connection is already opened". 我在打开连接时遇到错误,例如“连接已打开”。 It is telling to close the opened connection of while loop(reading data from table). 它告诉关闭while循环的打开连接(从表中读取数据)。 But any way the data's have to inserted to data base while reading. 但是在读取时必须以任何方式将数据插入数据库。 How to solve? 怎么解决?

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();
}
    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..

This is worked for me.I have used DataTable to store the retrieved data. 这对我有用。我已经使用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();
                    }


                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM