作为我在所有业务应用程序中使用的常用实用程序的一部分,我有这个代码……
using System.Web.UI.WebControls; public class Database { /// <summary> /// Creates a DataView object using the provided query and an SqlDataSource object. /// </summary> /// <param name="query">The select command to perform.</param> /// <returns>A DataView with data results from executing the query.</returns> public static DataView GetDataView(string query) { SqlDataSource ds = GetDBConnection(); ds.SelectCommand = query; DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty); return dv; } /// <summary> /// Creates a SqlDataSource object with initialized connection string and provider /// </summary> /// <returns>An SqlDataSource that has been initialized.</returns> public static SqlDataSource GetDBConnection() { SqlDataSource db = new SqlDataSource(); db.ConnectionString = GetDefaultConnectionString(); //retrieves connection string from .config file db.ProviderName = GetDefaultProviderName(); //retrieves provider name from .config file return db; } }
然后,在我的项目中,要从数据库中检索数据,我会有一些代码,如…
DataView dv=Database.GetDataView("select mycolumn from my table"); //loop through data and make use of it
我以这种方式从人们那里获得了一些使用SqlDataSource的热量.人们似乎并不喜欢我纯粹从代码中使用Web控件而不是将其放在ASPX页面上.它看起来并不正确,但他们无法告诉我一个缺点.那么,有缺点吗?这是我的主要问题.因为如果有很多缺点,我可能不得不改变我开发的许多内部应用程序的方式.
我的数据库类甚至可以在非ASP.NET的情况下工作,只要我添加System.Web程序集即可.我知道它的封装尺寸略有增加,但我觉得这对于我正在编写的应用类型是值得的.说WPF / Windows Forms / Console程序使用SqlDataSource是否有缺点?
解决方法
好吧,没有硬性规则阻止任何人做这样的实现.
但是,在执行该实现之前,需要回答以下几个问题.
>这个用法线程安全吗? (因为很有可能多个消费应用程序可以进行相同的调用.>是否存在分层差异(UI.Control在数据层中使用)?>如果在下一个框架版本中该控件变得过时/受限制怎么办?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。