C# 将多次使用的同一代码
我正在做一个系统,我注意到我的代码上有一些东西 我的代码没有优化,但正在运行 我想减少代码的冗余 这里有一个例子 这就是我加载的方式C# 将多次使用的同一代码,c#,oop,methods,redundancy,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 A
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();
}
private void frmCategory\u加载(对象发送方,事件参数e)
{
//填充
con.Open();
SqlCommand sc=newsqlcommand(“从活动=1的类别中选择ID、类别”,con);
SqlDataReader;
reader=sc.ExecuteReader();
DataTable dt=新的DataTable();
添加(“ID”,typeof(int));
添加(“类别”,类型(字符串));
dt.负载(读卡器);
对于(intx=0;x这就是所谓的“样板文件”。这对于SQL代码来说很常见。我所知道的消除它的最好例子是Spring的JdbcTemplate
您需要查看这些方法中的代码,并找出如何将其重构为几个类。您的目标应该是能够将更改的内容传递到包含不变样板的方法中
您有一个消息框在一个方法中执行I/O。您需要删除它。没有用户界面代码
至少你必须能够传入SQL并执行它。从那开始。将重复的代码重构为方法并调用这些方法。如果你只看到一些变化,请使用传入的参数。对不起,伙计们,我现在就把代码放进去了。@duffymo
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());
}