创建SQL Helper类 [英] Creating SQL Helper Class
问题描述
大家好,我有一个需要修改的工作代码.我想完全摆脱System.Data.SqlClient命名空间,并建立一个帮助程序类,该类将帮助我使用dataadapter更新数据.完成后,我不需要导入SqlClient
我的视图层上的命名空间了.
这是需要修改的工作代码.
Hello everyone, i have a working code that need some revision. I want to completely get rid of the System.Data.SqlClient namespace and build a helper class that will help me for updating data using dataadapter. When done, i don't need to import the SqlClient
namespace anymore on my View Layer.
This is the working code that needs to be revise.
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string sql="query here";
SqlConnection con=new SqlConnection("Connections String");
con.open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM table", con);
da.Fill(ds, "Products");
GridView1.DataSource=ds.Tables[0];
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand();
da.DeleteCommand = cmdBuilder.GetDeleteCommand();
da.UpdateCommand = cmdBuilder.GetUpdateCommand();
con.Close();
}
private void Button2_Click(object sender, EventArgs e)
{
da.Update(ds.Tables[0]);
ds.AcceptChanges();
}
我的助手类上已经有一个使用dataadapter填充数据集的方法.问题是我无法通过适配器.
I already have a method on my helper class which fills a dataset using dataadapter. The problem with it is i can't pass adapter around.
public static DataSet GetDataSet(DataSet ds, string sql)
{
{
using (var con = new SqlConnection(ConnectionString))
{
var da = new SqlDataAdapter(sql, con);
da.Fill(ds);
}
}
return ds;
}
推荐答案
https://en.wikipedia.org/wiki/Separation_of_concerns
https://en.wikipedia.org/wiki/Separation_of_concerns
https://www.codeproject.com/articles/228214/understanding-basics-of-ui-design-pattern-mvc-mvp
https://www.codeproject.com/articles/228214/understanding-basics-of-ui-design-pattern-mvc-mvp
https://www.codeproject.com/Articles/613682/Your-first-program-using-MVC-pattern-with-Csharp-W
https://www.codeproject.com/Articles/613682/Your-first-program-using-MVC-pattern-with-Csharp-W
http://www.c-sharpcorner.com/UploadFile/ankithakur/mvp-design-pattern-for-window-forms/
http://www.c-sharpcorner.com/UploadFile/ankithakur/mvp-design-pattern-for-window-forms/
https://msdn.microsoft.com/zh-cn/library/bb384398.aspx?f=255&MSPPError=-2147217396
https://msdn.microsoft.com/en-us/library/bb384398.aspx?f=255&MSPPError=-2147217396
https://www.codeproject.com/articles/36847/three-layer-architecture-in-c-net
https://www.codeproject.com/articles/36847/three-layer-architecture-in-c-net
这篇关于创建SQL Helper类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!