将多次使用的相同代码

时间:2017-03-04 14:23:57

标签: c# oop methods redundancy

我制作了一个系统,我注意到了我的代码 我的代码没有优化,但它正在运行 我想减少代码的冗余 这里有一个例子

这是我加载的方式

private void frmCategory_Load(object sender, EventArgs e)
    {
        //Populate
        con.Open();
        SqlCommand sc = new SqlCommand("SELECT ID,Category FROM Category WHERE Active = 1",con);
        SqlDataReader reader;
        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("ID",typeof(int));
        dt.Columns.Add("Category",typeof(string));
        dt.Load(reader);

        for(int x = 0; x <dt.Rows.Count; x++)
        {
            string ID = dt.Rows[x].ItemArray[0].ToString();
            string Category = dt.Rows[x].ItemArray[1].ToString();
            string[] row = { ID,Category };
            dgvCategory.Rows.Add(row);
        }
        con.Close();
    }

这是我添加的方式

ry//Add
        {
            con.Open();
            SqlCommand sc = new SqlCommand("INSERT INTO Category([Category]) VALUES(@Category)", con);
            {
                sc.Parameters.AddWithValue("@Category", txtCategory.Text);
                sc.ExecuteNonQuery();
                MessageBox.Show(txtCategory.Text + " is added");
                txtCategory.Clear();
            }
            con.Close();
        }
        catch (SystemException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        try//Refresh
        {
            dgvCategory.Rows.Clear();
            SqlCommand sc = new SqlCommand("SELECT ID,Category FROM Category WHERE Active = 1", con);
            SqlDataReader reader;
            reader = sc.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Category", typeof(string));
            dt.Load(reader);

            for (int x = 0; x < dt.Rows.Count; x++)
            {
                string ID = dt.Rows[x].ItemArray[0].ToString();
                string Category = dt.Rows[x].ItemArray[1].ToString();
                string[] row = { ID, Category };
                dgvCategory.Rows.Add(row);
            }
            con.Close();
        }
        catch (SystemException ex)
        {
            MessageBox.Show(ex.ToString());
        }

这是我更新的方式

try//Update
        {
            string ID = dgvCategory.SelectedCells[0].Value.ToString();
            con.Open();
            SqlCommand sc = new SqlCommand("UPDATE Category SET Category = '" + txtCategory.Text + "' WHERE ID = '" + ID + "'", con);
            sc.ExecuteNonQuery();
            MessageBox.Show(dgvCategory.SelectedCells[1].Value.ToString() + " is updated to " + txtCategory.Text);
            con.Close();
        }
        catch (SystemException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        try
        {
            dgvCategory.Rows.Clear();
            SqlCommand sc = new SqlCommand("SELECT ID,Category FROM Category WHERE Active = 1", con);
            SqlDataReader reader;
            reader = sc.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Category", typeof(string));
            dt.Load(reader);

            for (int x = 0; x < dt.Rows.Count; x++)
            {
                string ID = dt.Rows[x].ItemArray[0].ToString();
                string Category = dt.Rows[x].ItemArray[1].ToString();
                string[] row = { ID, Category };
                dgvCategory.Rows.Add(row);
            }
            con.Close();
        }
        catch (SystemException ex)
        {
            MessageBox.Show(ex.ToString());
        }

如果你能看到的话。我总是使用来自负载的代码来再次填充我的DGV以及我如何刷新。我想知道我是否可以制作一个代码块并命名为LOAD然后生病只需使用LOAD,只要我再次需要该代码

1 个答案:

答案 0 :(得分:0)

这就是你所说的“样板”。这对SQL代码来说很常见。我知道消除它的最好例子是Spring的JdbcTemplate。

您需要查看这些方法中的代码,并弄清楚如何将其重构为多个类。您的目标应该是能够将更改的内容传递到包含不会更改的样板文件的方法中。

您有一个消息框在一个方法中执行I / O.你需要删除它。没有用户界面代码。

至少你必须能够传入SQL并执行它。从那开始。

相关问题