如何自动执行代码?

时间:2013-05-20 15:41:06

标签: c# asp.net sql sql-server

我已经手动使用这些代码将数据从另一台服务器更新到我自己的服务器:

    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手动将数据从1台服务器更新到另一台服务器,该按钮使用生成的数据并合并到K()以在另一台服务器上运行查询。之后,通过点击保存button-btnSave_Click,它将查询的数据保存到另一台服务器。有了这个,我需要手动点击每个文件来生成结果并更新数据。

有没有办法让自动更新按钮?我阅读了有关循环的帖子,但却遇到了问题。

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用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);
}

来自here