簡體   English   中英

如何使我的代碼自動化?

[英]How can I automate my code?

我已經有了使用以下代碼將數據從另一台服務器更新到我自己的服務器的手動方法:

    protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = (sender as GridView).Rows[rowIndex];
            string strID = GridView1.DataKeys[row.RowIndex].Values[0].ToString();

            lblWO.Text = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString();
            lblPN.Text = GridView1.Rows[row.RowIndex].Cells[9].Text.ToString();


            k();

            if (lblFLF.Text == "NO DATA FOUND")
            {
                btnSave.Visible = false;
                lblFLF.ForeColor = Color.Red;
                lblFLM.ForeColor = Color.Red;
                lblFWKG.ForeColor = Color.Red;
                lblFWLB.ForeColor = Color.Red;
                lblPieceNumber.ForeColor = Color.Red;
                lblFinalWO.ForeColor = Color.Red;
            }
            else
            {
                btnSave.Visible = true;
                lblStatus.Text = "";
                lblFLF.ForeColor = Color.Black;
                lblFLM.ForeColor = Color.Black;
                lblFWKG.ForeColor = Color.Black;
                lblFWLB.ForeColor = Color.Black;
                lblPieceNumber.ForeColor = Color.Black;
                lblFinalWO.ForeColor = Color.Black;
            }

        }
        catch (Exception) { }

    }

    protected void k()
    {

        SC = new SqlConnection(ConfigurationManager.ConnectionStrings["BABBLER"].ConnectionString);
        SC.Open();
        CMD = new SqlCommand("Select TOP(1) * from F564101D WHERE ZAWODES = '" + lblWO.Text + "' and ZAPIPLVER = '" + lblPN.Text + "'", SC);

        DR = CMD.ExecuteReader();

        if (DR.HasRows)
        {
            while (DR.Read())
            {
                lblFWLB.Text = (Convert.ToDouble(DR["ZAAWGT"]) / 1000).ToString();
                lblFWKG.Text =  System.Math.Round((Convert.ToDouble(lblFWLB.Text) * 0.453592), 0, MidpointRounding.AwayFromZero).ToString();
                lblFLM.Text = (Convert.ToDouble(DR["ZADILI"]) / 10000).ToString();
                lblFLF.Text = System.Math.Round((Convert.ToDouble(lblFLM.Text) * 0.00328084),1, MidpointRounding.AwayFromZero).ToString();
                lblPieceNumber.Text = Convert.ToDouble(DR["ZABSNN"]).ToString();
                lblFinalWO.Text = DR["ZAWTYPEDES"].ToString();

            }

        }
        else
        {
            lblFLF.Text = "NO DATA FOUND";
            lblFLM.Text = "NO DATA FOUND";
            lblFWKG.Text = "NO DATA FOUND";
            lblFWLB.Text = "NO DATA FOUND";
            lblFinalWO.Text = "NO DATA FOUND";
            lblPieceNumber.Text = "NO DATA FOUND";
        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
        if (lblFWKG.Text == "NO DATA FOUND")
        {
            lblStatus.Text = "Pipe is not yet saved in weighing";
        }
        else
        {
            string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                string sqlQuery = "UPDATE LPM_QC_Pcard SET [WT_LBS] = @WT_LBS, [WT_KGS] = @WT_KGS, [LENGTH_FT] = @LENGTH_FT, [LENGTH_MM] = @LENGTH_MM, [MEMO_NO] = @MEMO_NO, [FWO_NO] = @FWO_NO, [PIECE_NO] = @PIECE_NO  WHERE [WO_NO] = @WO_NO and [PIPE_NO] = @PIPE_NO ";
                SqlCommand cmd = new SqlCommand(sqlQuery, con);


                cmd.Parameters.AddWithValue("@WT_LBS", lblFWLB.Text);
                cmd.Parameters.AddWithValue("@WT_KGS", lblFWKG.Text);
                cmd.Parameters.AddWithValue("@LENGTH_FT", lblFLF.Text);
                cmd.Parameters.AddWithValue("@LENGTH_MM", lblFLM.Text);
                cmd.Parameters.AddWithValue("@WO_NO", lblWO.Text);
                cmd.Parameters.AddWithValue("@MEMO_NO", txtMemo.Text);
                cmd.Parameters.AddWithValue("@PIPE_NO", lblPN.Text);
                cmd.Parameters.AddWithValue("@FWO_NO", lblFinalWO.Text);
                cmd.Parameters.AddWithValue("@PIECE_NO", lblPieceNumber.Text);


                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



                lblStatus.Text = "Data Saved";
                lblStatus.ForeColor = System.Drawing.Color.Red;

                btnSave.Visible = false;

                lblFWKG.Text = "";
                lblFWLB.Text = "";
                lblFLM.Text = "";
                lblFLF.Text = "";
                lblPieceNumber.Text = "";
                lblFinalWO.Text = "";

                SqlDataSource1.SelectCommand = "Select * from LPM_QC_Pcard where WO_NO = '" + DropDownList1.Text + "'";

            }
        }
        }
        catch(Exception){}

這是一個工作代碼,它通過按選擇按鈕GridView1_RowCommand1手動將數據從一台服務器更新到另一台服務器,該按鈕使用生成的數據並將其合並到K()在另一台服務器上運行查詢。 之后,點擊保存button-btnSave_Click ,將查詢的數據保存到另一台服務器。 有了這個,我需要手動單擊每個文件以生成結果並更新數據。

有沒有一種方法可以使用自動更新按鈕? 我閱讀了有關循環的文章,但遇到了問題。

請幫我。 謝謝。

您可以使用Timer

aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    btnSave_Click(btnSave, EventArgs.Empty);
}

這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM