具有对象创建和数据访问的最佳实践

时间:2013-06-11 17:57:12

标签: c# asp.net refactoring

我有一个TabContainer控件,里面有几个标签。根据TabContainer的ActiveIndex属性,我希望单击按钮以在该选项卡中创建Gridview并将其绑定到存储过程。目前,以下代码有效,但有很多代码重复

 protected void btnMakeGridView_Click(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
            if (TabContainer1.ActiveTabIndex == 0)
            {

                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest0", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else if (TabContainer1.ActiveTabIndex == 1)
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest1", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else if (TabContainer1.ActiveTabIndex == 2)
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest2", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else
            {
                Label1.Text = "You must select a tab to create the GridView in";
            }

        }

正如您所看到的,根据TabContainer的ActiveIndex,应在每个TabPanel内创建不同的存储过程。在我的示例中,我只使用一个数据库,因此我不必担心将连接字符串作为数据库连接的参数传递。乍一看,我添加了类似

的内容
 //pass in the connection string, the stored procedure name and the ActiveTabIndex
        public static void DataAccess(string connectionString,string spName, int activeTabIndex)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(spName, con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    GridView gv = new GridView();

                    gv.DataSource = rdr;
                    gv.DataBind();

                }
            }
        }

UDPATE:好的,与此同时我想出了

 public partial class _Default : System.Web.UI.Page
    {

        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMakeGridView_Click(object sender, EventArgs e)
        {
            TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(GetData(cs, "spTest", TabContainer1.ActiveTabIndex));
        }
        public static GridView GetData(string connectionString,string spName, int activeIndex)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(spName, con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    GridView gv = new GridView();
                    gv.ID = "gv" + activeIndex.ToString();
                    gv.DataSource = rdr;
                    gv.DataBind();
                    return gv;
                }

            }
        }

    }

0 个答案:

没有答案