程序在部署时停止响应

时间:2012-12-07 13:08:29

标签: c# hang

我遇到的问题是我的应用程序在开发环境中工作正常,但是当部署到客户时只是挂起而不会绊倒任何异常。

为了部署我必须将登录详细信息(已删除)和sql语句更改为显示的那些,我想这可能是问题,但我希望任何sql syntex错误都会抛出异常。我已经将无限循环排除在外(我认为)。

我所知道的唯一可能是使用多线程并希望问题得到解决,但我没有那种时间,除非我能确定这是解决方案。

坦率地说,我很茫然,并且提出任何关于解决问题的提示/建议。

以下是代码:

        but_exit.Enabled = false;
        but_manual.Enabled = false;

        //switch off gui timer to allow process messages
        guiTimer.Enabled = false;
        lbl_dyn_status.Text = "Finding records...";

        /*create connection resources*/

        /*MYSQl*/
        //connect to mysql
        /*

         */

        var db_name = "";
        var db_host = "";
        var db_user = "";
        var db_password = "";

        var connectionString = "SERVER=" + db_host + ";" + "DATABASE=" +
    db_name + ";" + "UID=" + db_user + ";" + "PASSWORD=" + db_password + ";";
        MySqlConnection mysql_con = new MySqlConnection(connectionString);

        //Create a list to store the  result
        List<string>[] mysql_idlist = new List<string>[1];
        mysql_idlist[0] = new List<string>();


        /*MSSQL*/
        //connect to mssyql



        db_user = "";
        db_password = "";
        db_name = "";
        db_host = "";

        System.Data.SqlClient.SqlConnection mssql_con = new System.Data.SqlClient.SqlConnection();
        mssql_con.ConnectionString = "SERVER=" + db_host + ";" + "DATABASE=" +
    db_name + ";" + "UID=" + db_user + ";" + "PASSWORD=" + db_password + ";";

        //Create a list to store the result
        List<string>[] mssql_idlist = new List<string>[2];
        mssql_idlist[0] = new List<string>();
        mssql_idlist[1] = new List<string>();

        /*Processing logic*/

        //first pass on mysql
        try
        {
            mysql_con.Open();

            //get id list
            string mysql_idlist_query = "SELECT product_code FROM cscart_products";

            MySqlCommand cmd = new MySqlCommand(mysql_idlist_query, mysql_con);

            //Create a data reader and Execute the command
            MySqlDataReader dataReader = cmd.ExecuteReader();

            //Read the data and store them in the list
            while (dataReader.Read())
            {
                mysql_idlist[0].Add(dataReader["product_code"] + "");
            }

            //close Data Reader
            dataReader.Close();

            //test content
            /*foreach (var id in mysql_idlist[0]) {
                lbl_dev.Text = lbl_dev.Text + id + " - ";
            }*/

            //close mysql connection
            mysql_con.Close();
        }

        catch (MySqlException ex)
        {

            switch (ex.Number)
            {
                case 0:
                    lbl_dev.Text = "(mysql) Cannot connect to server. Contact administrator";
                    break;

                case 1045:
                    lbl_dev.Text = "(mysql) Invalid username/password, please try again";
                    break;

                default:
                    lbl_dev.Text = "Unknown mysql error.";
                    break;
            }
        }
        catch
        {
            lbl_dev.Text = "first mysql error";
        }


        //first pass on mssql
        try
        {
            mssql_con.Open();

            //get id list
            //protect for duplicate skus
            string mssql_idlist_query = "SELECT substring (RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar ), patindex('%[^0]%',RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar )), 10) as SKU, ID FROM Denton_multi.dbo.JH_TblFurniture6Colour WHERE substring (RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar ), patindex('%[^0]%',RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar )), 10) IN (SELECT substring(RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar ), patindex('%[^0]%',RIGHT('00'+ CAST (JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar )), 10))";

            SqlCommand cmd = new SqlCommand(mssql_idlist_query, mssql_con);

            //Create a data reader and Execute the command
            SqlDataReader dataReader = cmd.ExecuteReader();

            //Read the data and store them in the list
            while (dataReader.Read())
            {
                mssql_idlist[0].Add(dataReader["SKU"] + "");
                mssql_idlist[1].Add(dataReader["ID"] + "");
            }

            //close Data Reader
            dataReader.Close();
            mssql_con.Close();
        }

        catch (SqlException ex)
        {

            switch (ex.Number)
            {
                case 0:
                    lbl_dev.Text = "(mssql) Cannot connect to server.  Contact administrator";
                    break;

                case 1045:
                    lbl_dev.Text = "(mssql) Invalid username/password, please try again";
                    break;

                default:
                    lbl_dev.Text = "Unknown mssql error. " + ex.Number; ;
                    break;
            }


        }
        catch
        {
            lbl_dev.Text = "first mssql error";
        }


        //compare lists and get ids that need inserting in mysql
        List<string>[] insert_idlist = new List<string>[10];
        insert_idlist[0] = new List<string>();//product code
        insert_idlist[1] = new List<string>();//short description
        insert_idlist[2] = new List<string>();//full description
        insert_idlist[3] = new List<string>();//product id
        insert_idlist[4] = new List<string>();//weight
        insert_idlist[5] = new List<string>();//rrp price
        insert_idlist[6] = new List<string>();//our price
        insert_idlist[7] = new List<string>();//categories
        insert_idlist[8] = new List<string>();//denton side id
        insert_idlist[9] = new List<string>();//insert / update tag
        List<string> dup_list = new List<string>();

        var about_to_insert = 0;

        foreach (var id in mssql_idlist[0])
        {
            if (mysql_idlist[0].Contains(id) == false)
            {
                //insert list
                insert_idlist[0].Add(id);
                insert_idlist[9].Add("i");
            }
            else
            {
                //update_list
                insert_idlist[0].Add(id);
                insert_idlist[9].Add("u");
            }
        }

        //construct full mssql dataset for insert items
        //final pass on mssql
        try
        {
            mssql_con.Open();


            foreach (var id in insert_idlist[0])
            {
                //top 1 for duplicate removal
                var mssql_select = "SELECT Denton_Multi.dbo.tblproductcategorys.*, Denton_Multi.dbo.JH_TblFurniture6Colour.*, substring(RIGHT('00'+ CAST (Denton_Multi.dbo.JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar ), patindex('%[^0]%',RIGHT('00'+ CAST (Denton_Multi.dbo.JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar )), 10) as SKU, suppliers.[supplier name], (select top 1 [item description] from Denton_Multi.dbo.JH_TblFurniture4item where [supplier code] = Denton_Multi.dbo.JH_TblFurniture6Colour.[supplier code] and [item code] = Denton_Multi.dbo.JH_TblFurniture6Colour.[item code] And [Delete] = 0) as [item description], (select top 1 [model description] from Denton_Multi.dbo.JH_TblFurniture3Range where [supplier code] = Denton_Multi.dbo.JH_TblFurniture6Colour.[supplier code] and [model code] = Denton_Multi.dbo.JH_TblFurniture6Colour.[model code]) as [range description] FROM Denton_Multi.dbo.JH_TblFurniture6Colour join Denton_Multi.dbo.tblproductcategorys on Denton_Multi.dbo.tblproductcategorys.CategoryID = Denton_Multi.dbo.JH_TblFurniture6Colour.[product group] join Denton_Multi.dbo.Suppliers on Denton_Multi.dbo.Suppliers.[supplier code] = Denton_Multi.dbo.JH_TblFurniture6Colour.[supplier code] WHERE ExportWeb = 1 AND substring(RIGHT('00'+ CAST (Denton_Multi.dbo.JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar ), patindex('%[^0]%',RIGHT('00'+ CAST (Denton_Multi.dbo.JH_TblFurniture6Colour.[product group] AS varchar), 2) + CAST( ID AS Varchar )), 10) = '"+id+"'";
                SqlCommand cmd = new SqlCommand(mssql_select, mssql_con);

                //Create a data reader and Execute the command
                SqlDataReader dataReader = cmd.ExecuteReader();

                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    insert_idlist[1].Add(dataReader["Supplier Name"] + " " + dataReader["Range Description"] + " " + dataReader["Item Description"]);
                    insert_idlist[3].Add(dataReader["Sale Price"] + "");
                    insert_idlist[2].Add(dataReader["WebDesc"] + "");
                    //insert_idlist[3].Add(dataReader["id"] + "");removed
                    insert_idlist[4].Add(dataReader["WebDimensions"] + "");
                    insert_idlist[5].Add(dataReader["RRP"] + "");
                    insert_idlist[6].Add(dataReader["Normal Price"] + "");
                    insert_idlist[7].Add("482"); //add me
                    insert_idlist[8].Add(dataReader["ID"] + "");

                    about_to_insert = about_to_insert + 1;

                }
                lbl_dyn_status.Text = "Record 0 of " + about_to_insert + "updated.";

                dataReader.Close();

            }


            //close mysql connection
            mssql_con.Close();
        }

        catch (SqlException ex)
        {

            switch (ex.Number)
            {
                case 0:
                    lbl_dev.Text = "(mssql) Cannot connect to server. Contact administrator";
                    break;

                case 1045:
                    lbl_dev.Text = "(mssql) Invalid username/password, please try again";
                    break;

                default:
                    lbl_dev.Text = "Unknown mssql error. "+ex.Number;
                    break;
            }
        }
        catch
        {
            lbl_dev.Text = "second mssql error";
        }



        //insert data in mysql db
        try
        {
            //final mysql pass
            var inc = insert_idlist[0].Count() - 1;

            if (about_to_insert > 0 && insert_idlist[0][0].Count() > 0)
            {


                mysql_con.Open();
                for (int x = 0; x <= inc; x++)
                {
                    int pid = 0;
                    if (insert_idlist[9][x] == "u")
                    {
                        //get web side product_id for updates
                        var sku = insert_idlist[0][x];
                        var get_id = "SELECT product_id FROM cscart_products WHERE product_code = '" + sku + "' LIMIT 1";
                        MySqlCommand do_get_id = new MySqlCommand(get_id, mysql_con);
                        MySqlDataReader rpid = do_get_id.ExecuteReader();

                        //get id
                        while (rpid.Read())
                        {
                            pid = Convert.ToInt32(rpid["product_id"]);
                        }
                        rpid.Close();
                    }


                    /*main record*/
                    var mysql_product = "";

                    if (insert_idlist[9][x] == "u")
                    {
                        mysql_product = "UPDATE cscart_products SET product_code = @product_code, list_price = @rrp, status='D' WHERE product_id = '" + pid + "'";
                    }
                    else
                    {
                        mysql_product = "INSERT INTO cscart_products (product_code, list_price, status) VALUES (@product_code, @rrp, 'D')";
                    }
                    MySqlCommand product_insert = new MySqlCommand(mysql_product, mysql_con);

                    product_insert.Parameters.Add(new MySqlParameter("@product_code", insert_idlist[0][x]));
                    product_insert.Parameters.Add(new MySqlParameter("@rrp", insert_idlist[5][x]));


                    product_insert.ExecuteNonQuery();
                    var insertid = product_insert.LastInsertedId;

                    //get mssql id records
                    var sql_ID = insert_idlist[8][x];
                    var stock_sql = "SELECT * FROM dbo.TblSupplierDelivery INNER JOIN dbo.TblManagerStockListings ON dbo.TblSupplierDelivery.ID = dbo.TblManagerStockListings.ID WHERE dbo.TblSupplierDelivery.ID = '" + sql_ID + "'";

                    mssql_con.Open();
                    SqlCommand cmd = new SqlCommand(stock_sql, mssql_con);

                    //Create a data reader and Execute the command
                    SqlDataReader dataReader = cmd.ExecuteReader();

                    //insert extended data fields
                    while (dataReader.Read())
                    {
                        var delivery_time = dataReader["Delivery Weeks"];
                        var stock_quant = Convert.ToInt16(dataReader["WHTotal"]) - Convert.ToInt16(dataReader["TotalAv"]);
                        var sale_price = insert_idlist[3][x];
                        var prod_ex = "";

                        if (insert_idlist[9][x] == "u")
                        {
                            prod_ex = "UPDATE cscart_oo_product_extend SET product_id = '" + pid + "', transfer_date = now(), sale_price = '" + sale_price + "', stock_due_date = '" + delivery_time + "' WHERE product_id = '" + pid + "'";
                        }
                        else
                        {
                            prod_ex = "INSERT INTO cscart_oo_product_extend (product_id, transfer_date, sale_price, stock_due_date) VALUES ('" + insertid + "', now(), '" + sale_price + "', '" + delivery_time + "')";
                        }
                        MySqlCommand product_ex_insert = new MySqlCommand(prod_ex, mysql_con);
                        product_ex_insert.ExecuteNonQuery();

                        //this is always an update
                        if (Convert.ToString(insertid) == "0")
                        {
                            insertid = pid;
                        }
                        var stock_insert = "UPDATE cscart_products SET amount = '" + stock_quant + "' WHERE product_id = '" + insertid + "'";
                        MySqlCommand product_stock_insert = new MySqlCommand(stock_insert, mysql_con);
                        product_stock_insert.ExecuteNonQuery();
                    }

                    //close Data Reader
                    dataReader.Close();
                    mssql_con.Close();


                    /*description*/
                    var mysql_desc = "";
                    if (insert_idlist[9][x] == "u")
                    {
                        mysql_desc = "UPDATE cscart_product_descriptions SET product_id = @id, product = @product_name, full_description = @product_desc WHERE product_id = @id";
                    }
                    else
                    {
                        mysql_desc = "INSERT INTO cscart_product_descriptions (product_id, product, full_description) VALUES (@id, @product_name, @product_desc)";
                    }

                    MySqlCommand product_desc = new MySqlCommand(mysql_desc, mysql_con);

                    product_desc.Parameters.Add(new MySqlParameter("@id", insertid));
                    product_desc.Parameters.Add(new MySqlParameter("@product_name", insert_idlist[1][x]));
                    product_desc.Parameters.Add(new MySqlParameter("@product_desc", insert_idlist[2][x]));

                    product_desc.ExecuteNonQuery();

                    //category
                    var mysql_cat = "";
                    if (insert_idlist[9][x] == "i")
                    {
                        mysql_cat = "INSERT INTO cscart_products_categories (product_id, category_id) VALUES (@id, @cat_id)";

                        MySqlCommand product_cat = new MySqlCommand(mysql_cat, mysql_con);

                        product_cat.Parameters.Add(new MySqlParameter("@id", insertid));
                        product_cat.Parameters.Add(new MySqlParameter("@cat_id", insert_idlist[7][x]));
                        product_cat.ExecuteNonQuery();
                    }

                    //price
                    var mysql_price = "";
                    if (insert_idlist[9][x] == "u")
                    {
                        mysql_price = "UPDATE cscart_product_prices SET product_id = @id, price = @our_price, lower_limit = '1' WHERE product_id = @id";
                    }
                    else
                    {
                        mysql_price = "INSERT INTO cscart_product_prices (product_id, price, lower_limit) VALUES (@id, @our_price, '1')";
                    }
                    MySqlCommand product_price = new MySqlCommand(mysql_price, mysql_con);

                    product_price.Parameters.Add(new MySqlParameter("@id", insertid));
                    product_price.Parameters.Add(new MySqlParameter("@our_price", insert_idlist[6][x]));

                    product_price.ExecuteNonQuery();

                    lbl_dyn_status.Text = "Record " + x + " of " + about_to_insert + "updated.";
                }


                mysql_con.Close();
            }
        }
        catch
        {
            lbl_dev.Text = "upload error";
        }

        //reset gui timer for next idle period
        minutes_left = 10;
        lbl_dyn_status.Text = "Time until next automatic update: " + minutes_left + " minutes.";
        guiTimer.Start();

        but_exit.Enabled = true;
        but_manual.Enabled = true;
    }

1 个答案:

答案 0 :(得分:0)

行。我已经确定问题是来自sql语句的缓慢响应