使用 SqlDataAdapter 读取和更新数据问题
我正在寻找一些有关如何最好地使用 SqlDataAdapter 来访问和更新应用程序中的数据的示例。 现在我有这样的事情:
SqlDataAdapter adapter;
DataSet myData = MyDataAccessClass.GetData("Select * from Students", ref adapter);
// change some data here and save changes
adapter.Update();
所有这些都发生在代码后面,我根本不喜欢它。 因此,我试图找到一种方法来做这样的事情:
DataSet myData = MyDataAccessClass.GetStudents();
// change some data and save changes
MyDataAccessClass.SaveStudents(myData);
其中 SaveStudents
方法仍然使用 SqlDataAdapter 来更新数据库。
关于如何进行这项工作的任何想法或一些最佳实践的建议 像这样的事情受到高度赞赏。谢谢。
I'm looking for some examples on how to best use SqlDataAdapter to access and update data in my application.
Right now I have something like this:
SqlDataAdapter adapter;
DataSet myData = MyDataAccessClass.GetData("Select * from Students", ref adapter);
// change some data here and save changes
adapter.Update();
All of this occurs in code behind, and I dont really like it at all.
So, I'm trying to find a way to do something like this:
DataSet myData = MyDataAccessClass.GetStudents();
// change some data and save changes
MyDataAccessClass.SaveStudents(myData);
Where SaveStudents
method still uses SqlDataAdapter to update db.
Any ideas on how to make this work or some pointers to best practices of doing
something like this are highly appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,这似乎是一个相当基本的数据访问层实现。一般来说,我会这样做:
我要注意的一点是,有这么多可用的 ORM 解决方案(仅包括 Entity Framework 和 Linq2Sql),您可能需要考虑使用对象集合而不是数据集来存储数据陈述。然后你可以采用如下方法:
我承认,这是相当主观的,但我发现它比使用直接数据集更好。
That seems like a fairly basic Data Access Layer implementation, to me. Generally, I do it something like this:
One note that I'd make is that with so many ORM solutions (including just Entity Framework and Linq2Sql) available, you may want to consider using collections of objects instead of data-sets for your Data Representations. Then you can have a method like:
That's fairly subjective, I'll admit, but I find it preferable to using straight DataSets.
如果您想使用 sql-data-adapter 获取更新数据,那么您可以使用这些
Using System.Data.SqlClient;
我希望您了解如何轻松更新数据。就像例子一样。您可以在插入数据和删除数据中使用这些类型的示例,并根据需要使用特定的命令和 SQL 查询。
If you want to get update data using the sql-data-adapter then you could use these
Using System.Data.SqlClient;
I hope you understand how to update the data easily. It just like the example. You can use these type of example in Inserting into the Data, and Deleting the Data with using specific the command and sql query as it required.