C# 对象创建和数据访问的最佳实践
我有一个TabContainer控件,其中包含几个选项卡。根据TabContainer的ActiveIndex属性,我希望通过单击按钮在该选项卡中创建Gridview并将其绑定到存储过程。目前,下面的代码可以工作,但是有很多代码重复C# 对象创建和数据访问的最佳实践,c#,asp.net,refactoring,C#,Asp.net,Refactoring,我有一个TabContainer控件,其中包含几个选项卡。根据TabContainer的ActiveIndex属性,我希望通过单击按钮在该选项卡中创建Gridview并将其绑定到存储过程。目前,下面的代码可以工作,但是有很多代码重复 protected void btnMakeGridView_Click(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionSt
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();
}
}
}
乌德帕特:好吧,同时我想出了
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;
}
}
}
}
你应该考虑使用实体框架或其他东西,这将使这一点变得简单很多。@约翰桑德斯,我一直对学习有兴趣,但我仍然或多或少地在学习基础知识,我认为现在对我来说可能太先进了。这是访问数据的建议方法。@JohnSaunders您能详细说明一下吗?你的意思是现在数据访问的事实上的标准建议,还是在这种特殊情况下你的建议?我想说这是现在在.NET中进行数据访问的入门级方法。只需使用“数据库优先”,从数据库表创建一个EDMX文件,然后开始编码。它甚至比VisualStudio中旧的“强类型数据集”功能更简单。