加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从非控制情况中使用SqlDataSource

发布时间:2020-12-15 21:59:43 所属栏目:百科 来源:网络整理
导读:作为我在所有业务应用程序中使用的常用实用程序的一部分,我有这个代码…… using System.Web.UI.WebControls;public class Database { /// summary /// Creates a DataView object using the provided query and an SqlDataSource object. /// /summary ///
作为我在所有业务应用程序中使用的常用实用程序的一部分,我有这个代码……

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在数据层中使用)?>如果在下一个框架版本中该控件变得过时/受限制怎么办?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读