winforms 当尝试运行代码时,“连接未关闭,连接的当前状态为打开,”

vwhgwdsa  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(195)

这是一个用于旅游计算的windows窗体应用程序。当我从按钮计算值时,即使所有连接都正确关闭,也会出现以下错误。
连接没有关闭。连接的当前状态是打开的。我已经检查了天气是否有任何剩余的连接,但我找不到一个。
你能帮我拿一下这个吗?

public void ltcalculate(string vehicleno, string packagetype, DateTime startdate, DateTime enddate, int startkmreading, int endkmreading)
        {
            try
            {

                TimeSpan noofD1 = enddate - startdate;
                double noofD2 = noofD1.TotalDays;
                int noofD3 = Convert.ToInt32(noofD2);

                totalkms = endkmreading - startkmreading;

                conn.Open();

                string serchquery = "select * from Vehicle where Vehicle_NO = '" + vehicleno + "'";
                SqlCommand comm = new SqlCommand(serchquery, conn);
                SqlDataReader dataReader = comm.ExecuteReader();

                if (dataReader.Read())
                {
                    double driverrate = Convert.ToDouble(dataReader["Driver_Rate"]);
                }
                else
                {
                    MessageBox.Show("Vehicle not a found");
                    
                }
                
                conn.Close();
                dataReader.Close();

                conn.Open();

                string searchquearypackage = "select * from Long_Tour_Package where Package_Type = '" + packagetype + "'";
                SqlCommand com = new SqlCommand(searchquearypackage, conn);
                SqlDataReader packdatareder = com.ExecuteReader();

                if (packdatareder.Read())
                {
                    double packagerate = Convert.ToDouble(packdatareder["Package_Rate"]);
                    double driverovernightrate = Convert.ToDouble(packdatareder["Driver_Overmight_Rate"]);
                    double vehiclenightparkrate = Convert.ToDouble(packdatareder["Vehicle_Night_Park_Rate"]);
                    int maximumkmlimit = Convert.ToInt32(packdatareder["Max_Km"]);
                    int rateperextrakm = Convert.ToInt32(packdatareder["Extra_Km_Rate"]);

                    int maxnokmsperpack = Convert.ToInt32(maximumkmlimit * noofD3); //This is the limit per the whole trip, not a a day

                    if (totalkms > maxnokmsperpack)
                    {
                        extrakmfortrip = totalkms - maxnokmsperpack; 
                        extrakmfee = Convert.ToDouble(extrakmfortrip * rateperextrakm);  

                    }
                    else
                    {
                        extrakmfee = 0;
                    }
                    int noofnt = noofD3 - 1;
                    double toOntfee = (driverovernightrate + vehiclenightparkrate) * noofnt;
                    double totpackrate = packagerate * noofD3;

                    double totcharge = totpackrate + toOntfee + extrakmfee;

                    txtbasehirerate.Text = totpackrate.ToString();
                    txtextrakmrate.Text = extrakmfee.ToString();
                    txtovernightstayfee.Text = driverovernightrate.ToString();
                    txttotalamount.Text = totcharge.ToString();

                    MessageBox.Show(noofnt + "\n" + toOntfee + "\n" + extrakmfee + "\n");

                }

               conn.Close();
               packdatareder.Close();
                

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

private void btncalculate_Click(object sender, EventArgs e)
        {
            string vehicleno = txtvehicleno.Text;
            string packagetype = txtpackagetype.Text;
            int startkmreading = Convert.ToInt32(txtstartkmreading.Text);
            int endkmreading = Convert.ToInt32(txtendkmreading.Text);
            DateTime startdate = dtpstartdate.Value;
            DateTime enddate = dtpenddate.Value;

            ltcalculate(vehicleno, packagetype, startdate, enddate,startkmreading,endkmreading);

        }
h7wcgrx3

h7wcgrx31#

首先,将Close()语句移到finally块中,如下所示:

...
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally 
        {
            conn.Close();
            packdatareder.Close();
        }
      ...

相关问题