首页 > 解决方案 > C# 多个相同的表单,来自 SQL 数据库的不同值

问题描述

我因为一个我无法解决的问题而陷入困境。该项目正在使用 C# Winforms。列表中有多个作业,当用户选择一个作业时,会打开另一个表单。在此表单上,用户可以开始工作,也可以从他/她离开的地方继续工作。作业启动时,计时器启动,当用户单击暂停时,计时器停止并将其写入数据库(使用 MS SQL 更新语句,经过时间并将作业状态写入数据库)。当用户启动多个(比如说 2 个工作)工作时,可以看到 2 个具有不同值的相同表单。到目前为止一切都很好,但是当用户同时启动两个作业时,它会绑定值并且计时器计数 x2 次。换句话说,它们就像一个表格一样,并且计数器根据相同数量的表格进行多次计数。

有没有办法区分它们之间的多个相同形式?我希望你们能听懂我的英语,祝你们有美好的一天!!

这是代码:

public partial class ClientPage : DevExpress.XtraEditors.XtraForm
{
    
    readonly Stopwatch stopwatch = new Stopwatch();
    bool alreadyopened = false;
    public ClientPage()
    {
        InitializeComponent();
       
    }

    readonly SqlCNK NewConnection = new SqlCNK();

    private void ClientPage_Load(object sender, EventArgs e)
    {
        label8.Text = GlobalVeriables.ongorulensure;
        //Labellarda ilgili MPS ve İş Emri detaylarını göster
        label7.Text = GlobalVeriables.mamulstokkodu;
        label9.Text = GlobalVeriables.mpsno;           //SOME INFORMATION FOR THE USER
        label10.Text = GlobalVeriables.baslanansure;
        label11.Text = GlobalVeriables.mamuladi;
        button4.Visible = false;//FINISH BUTTON WILL BE VISIBLE WHEN USER STARTS THE JOB
        button2.Enabled = true;
        button2.ImageIndex = 3;
    }

    //GO BACK BUTTON OPERATIONS
    private void guna2ImageButton1_Click(object sender, EventArgs e)
    {
        FormCollection fc = Application.OpenForms;

        //IF THE JOB IS CONTINUING ALERT
        if (button4.Visible == true)
        {
            DialogResult result1 = MessageBox.Show("JOB WILL BE STOPPED ARE U SURE?", "WARNING", MessageBoxButtons.YesNo);
            if (result1 == DialogResult.Yes)
            {
                //STORE THE TIME
                timer2.Enabled = false;
                GlobalVeriables.status = "stopped";
                //WRITE ELAPSED TIME TO DB
                NewConnection.Connection();
                SqlCNK.connectSql.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = SqlCNK.connectSql; 
                GlobalVeriables.gecensure = GlobalVeriables.hours + ":" + GlobalVeriables.minutes;
                cmd.CommandText = "UPDATE CNKbaslananisler SET gecensure='"+GlobalVeriables.gecensure+"' where uniqueid='"+GlobalVeriables.selecteduniqueid+"'";
                cmd.ExecuteNonQuery();

                foreach (Form frm in fc) //IF MAIN PAGE IS ALREADY OPENED DONT OPEN IT AGAIN
                {
                    //iterate through
                    if (frm.Name == "MainPage")
                    {
                        alreadyopened = true;
                    }
                }
                if (alreadyopened == false) 
                {
                    MainPage OperatorPage = new MainPage();
                    OperatorPage.Location = this.Location;
                    //OperatorPage.ChooseStartPage();
                    OperatorPage.FormBorderStyle = FormBorderStyle.None;
                    OperatorPage.WindowState = FormWindowState.Maximized;
                    OperatorPage.Show();
                }

                GlobalVeriables.adminUser = false;
                this.Hide();
            }
        }
        else
        {
            //IF MAIN PAGE IS ALREADY OPENED DONT OPEN IT AGAIN
            foreach (Form frm in fc) 
            {
                //iterate through
                if (frm.Name == "MainPage")
                {
                    alreadyopened = true;
                }
            }

            if (alreadyopened == false)
            { 
             MainPage OperatorPage = new MainPage();
             OperatorPage.Location = this.Location;
             //OperatorPage.ChooseStartPage();
             OperatorPage.FormBorderStyle = FormBorderStyle.None;
             OperatorPage.WindowState = FormWindowState.Maximized;
             OperatorPage.Show();
             
            }

            SqlCNK.connectSql.Close();
            GlobalVeriables.adminUser = false;
            this.Hide();
        }
    }

    //START BUTTON OPERATIONS
    private void button2_Click(object sender, EventArgs e)
    {
        if (GlobalVeriables.started == 0) 
        {
    //RESET THE TIMER AND START IT
            timer2.Stop();
            timer2.Start();
            timer2.Enabled = true;

            //POPUP
            PopupNotifier popup = new PopupNotifier();
            popup.Image = Properties.Resources.information;
            popup.TitleText = "Bildirim";
            popup.ContentText = "MPS Başlatılmıştır";
            popup.Popup();

            statusLabel.Text = "STARTED";
            button4.Visible = true; // FINISH BUTTON IS ONLY VISIBLE WHEN THE JOB IS STARTED
            GlobalVeriables.started = 1; 
            button2.ImageIndex = 2; //START BUTTON IS CHANGED TO PAUSE BUTTON

            DateTime currentTime = DateTime.Now;//BAŞLAMA süresini göster
            button2.ImageIndex = 2;
            //DELETE THE JOB FROM ASSIGNED JOBS AND ADD IT TO THE STARTEDJOBS TABLE 
            NewConnection.Connection();
            SqlCNK.connectSql.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = SqlCNK.connectSql;
            cmd.CommandText = "DELETE FROM ASSIGNEDJOBS WHERE uniqueid='" + GlobalVeriables.selecteduniqueid+"'";
            cmd.CommandText += "INSERT INTO STARTEDJOBS values('"+GlobalVeriables.mpsno+"','" + GlobalVeriables.artikelno +"','" + GlobalVeriables.selecteduniqueid + "','" + GlobalVeriables.mamulstokkodu + "','" + GlobalVeriables.atanantarih + "','"+GlobalVeriables.uretilecekmiktar+"','1','"+GlobalVeriables.operasyonaciklamasi+"','"+GlobalVeriables.opCode+"','" + GlobalVeriables.mamuladi + "','0','"+currentTime+"','"+GlobalVeriables.ongorulensure+"')";
            cmd.ExecuteNonQuery();
            label10.Text = currentTime.ToString();
            stopwatch.Start();
            
            
        }

        //STOP BUTTON OPERATIONS
        else if (GlobalVeriables.started == 1)
        {
            //POPUP
            PopupNotifier popup = new PopupNotifier();
            popup.Image = Properties.Resources.information;
            popup.TitleText = "Bildirim";
            popup.ContentText = "MPS Duraklatılmıştır";
            popup.Popup();

            statusLabel.Text = "STOPPED";
            button2.ImageIndex = 3; //CONTINUE BUTTON WILL BE SHOWN 
            timer2.Enabled = false;
            GlobalVeriables.started = 2; //STORE THE SITUATION OF THE JOB 
            button4.Visible = false;

            Options secenekler = new Options();
            secenekler.StartPosition = FormStartPosition.CenterScreen;//USER HAS TO STATE WHY HE/SHE STOPPED THE JOB
            secenekler.ControlBox = false;
            secenekler.ShowDialog();
            stopwatch.Stop();

            //TIME CALCULATION
            GlobalVeriables.PASSEDTIME = (GlobalVeriables.elapsedhrs * 3600 + GlobalVeriables.elapsedmnts * 60 + GlobalVeriables.elapsedscnds).ToString();

            //STORE THE PASSED TIME TO THE DATABSE
            NewConnection.Connection();
            SqlCNK.connectSql.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = SqlCNK.connectSql;
            cmd.CommandText = "UPDATE STARTEDJOBS SET started='2', gecensure='"+GlobalVeriables.gecensure+"' WHERE uniqueid='"+GlobalVeriables.selecteduniqueid+"'";
            cmd.ExecuteNonQuery(); 
            SqlCNK.connectSql.Close();
        }

        else if (GlobalVeriables.started ==2)//IF THE JOB CONTINUES
        {
            //POPUP
            PopupNotifier popup = new PopupNotifier();
            popup.Image = Properties.Resources.information;
            popup.TitleText = "Bildirim";
            popup.ContentText = "MPS Devam Ettiriliyor";
            popup.Popup();

            statusLabel.Text = "CONTINUING";
            button4.Visible = true;
            timer2.Enabled = true;
            GlobalVeriables.started = 1;// IT IS STARTED AGAIN
            button2.Enabled = true;
            button2.ImageIndex = 2; //SHOW THE PAUSE ICON
            stopwatch.Start();

            //STORE THE JOB IS CONTINUING TO THE DATABASE
            NewConnection.Connection();
            SqlCNK.connectSql.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = SqlCNK.connectSql;
            cmd.CommandText = "UPDATE STARTEDJOBS SET started='1' WHERE uniqueid='" + GlobalVeriables.selecteduniqueid + "'";
            cmd.ExecuteNonQuery();
            SqlCNK.connectSql.Close();

            //TIME CALCULATIONS
            int total_seconds = Convert.ToInt32(GlobalVeriables.gecensure);
            GlobalVeriables.elapsedhrs = total_seconds / 3600;
            GlobalVeriables.elapsedmnts = total_seconds / 60 % 60;
            GlobalVeriables.elapsedscnds = total_seconds % 60;



        }
    }
    

    // WHEN FINISH BUTTON IS CLICKED
    private void guna2ImageButton3_Click(object sender, EventArgs e)
    {

        
        //SAVE FINISH TIME
        GlobalVeriables.endtime = GlobalVeriables.elapsedhrs.ToString() + "sa:" + GlobalVeriables.elapsedmnts.ToString()+"dk";

        DialogResult dresult = MessageBox.Show("ARE U SURE FINISHING THE JOB?", "Alert"
                          , MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        if (dresult == DialogResult.OK)
        {
            this.Hide();
            String elapsed = GlobalVeriables.elapsedhrs + "HRS" + GlobalVeriables.elapsedmnts+"MNTS";
            NewConnection.Connection();
            SqlCNK.connectSql.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = SqlCNK.connectSql;
            String current = DateTime.Now.ToString("MM/dd/yyyy HH:mm");
            cmd.CommandText = "DELETE FROM startedjobs where uniqueid='"+GlobalVeriables.selecteduniqueid+"'";
            //Biten İşleri bitirilenisler havuzunda topla
            cmd.CommandText += "INSERT into endedjobs values(" +GlobalVeriables.mpsno+ ",'" + GlobalVeriables.artikelno + "','" + GlobalVeriables.selecteduniqueid + "','" + GlobalVeriables.mamulstokkodu + "','" + GlobalVeriables.operasyonaciklamasi + "','" + GlobalVeriables.opCode + "','" + GlobalVeriables.mamuladi + "','" + GlobalVeriables.gecensure + "','"+current+"','"+GlobalVeriables.uretilecekmiktar+"')";
            cmd.ExecuteNonQuery();
            SqlCNK.connectSql.Close();

            FormCollection fc = Application.OpenForms;
            foreach (Form frm in fc) //IF MAIN PAGE IS ALREADY OPENED DONT OPEN IT AGAIN
            {
                //iterate through
                if (frm.Name == "MainPage")
                {
                    alreadyopened = true;
                }
            }

            if (alreadyopened == false)
            {
                MainPage OperatorPage = new MainPage();
                OperatorPage.Location = this.Location;
                OperatorPage.FormBorderStyle = FormBorderStyle.None;
                OperatorPage.WindowState = FormWindowState.Maximized;
                OperatorPage.Show();
            }
        }
    }

    //TIME OPERATIONS
    private void timer2_Tick(object sender, EventArgs e)
    {
        GlobalVeriables.elapsedscnds++;
        if (GlobalVeriables.elapsedscnds > 59)
        {
            GlobalVeriables.elapsedmnts++;
            GlobalVeriables.elapsedscnds = 0;
        }
        if (GlobalVeriables.elapsedmnts > 59)
        {
            GlobalVeriables.elapsedhrs++;
            GlobalVeriables.elapsedmnts = 0;
        }

        label13.Text = GlobalVeriables.elapsedhrs.ToString();
        label14.Text = GlobalVeriables.elapsedmnts.ToString();
        label15.Text = GlobalVeriables.elapsedscnds.ToString();

        

    }


    


   
}

标签: c#sql-serverwinformssql-update

解决方案


推荐阅读