Asp.Net:从类返回DataSet [英] Asp.Net: Returning a DataSet from a Class

查看:250
本文介绍了Asp.Net:从类返回DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定开始另一个线程基于我在这个线程的回应:
Asp.Net:从类中返回读者

I've decided to start another thread based on the responses I got in this thread: Asp.Net: Returning a Reader from a Class

我回来了一个读者,但成员建议我'

I was returning a reader, but members have suggested I'd be better off returning a Dataset instead and also try to seperate the data access tier from the presentation tier.

这是我到目前为止的情况:
/ / my类方法

This is what I have so far: //my class methods

   public DataSet GetSuppliers()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("con_spSuppliersList", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@blogid", HttpContext.Current.Request.QueryString["p"]);

        return FillDataSet(cmd, "SuppliersList");
    }

    //my FillDataSet method

    private DataSet FillDataSet(SqlCommand cmd, string tableName)
    {
     SqlConnection conn = new SqlConnection(connectionString);

     cmd.Connection = conn;
     SqlDataAdapter adapter = new SqlDataAdapter(cmd);

     DataSet ds = new DataSet();
         try
         {
             conn.Open();
             adapter.Fill(ds, tableName);
         }
         finally
         {
             conn.Close();
         }
     return ds;

    }

//在我的ascx页面上我调用这样的方法:

// on my ascx page I call the method like so:

protected void Page_Load(object sender, EventArgs e)
{

    //instantiate our class
    MyClass DB = new MyClass();

    // grab the table of data
    DataTable dt = DB.GetSuppliers().Tables["SuppliersList"];

    //loop through the results
    foreach (DataRow row in dt.Rows)
    { 
        this.supplierslist.InnerHtml += Server.HtmlEncode(row["Address"].ToString()) + "<br/>";
        this.supplierslist.InnerHtml += "<b>Tel: </b>" + Server.HtmlEncode(row["Telephone"].ToString()) + "<p/>";

    }


}

}

有人会建议改进吗?

是我的循环数据层 ',应该循环是在类中,我只是返回一个格式化的字符串的数据集?

Is my loop 'data tier' or 'presentation tier', should the loop be inside the class and I just return a formatted string instaed of a dataset?

感谢所有的好建议

推荐答案

我还将使用一个类型化的DataSet或创建自己的类保存属性,所以你不处理像 row [地址] ,您会说 object.Address 并获得编译时检查。

I also would use a Typed DataSet or create your own class that holds the properties so you are not dealing with strings like row["Address"] you would say object.Address and get compile time checking.

DataSets有很多内置的功能是很好的,但也嘲笑它很多开销,可能不需要在简单的东西。使用属性创建一个简单的类并将其传递出你的数据访问层可能是实现你想要的最简单的方法。

DataSets have a lot of built in functionality that is nice but also caries with it a lot of overhead that might not be needed in something simple. Creating a simple class with properties and passing that out of your data access layer is probably the simplest way to implement what you want.

DAL (数据访问层)侧

//Also pass in the blogID dont have the DAL get the value from the UI layer..
    //make the UI layer pass it in.
    public IList<Supplier> GetSuppliers(string connectionString, int blogID)
    {
        IList<Supplier> suppliers = new List<Supplier>();
        //wrap with the using statements
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("con_spSuppliersList", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@blogid", blogID);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    suppliers.Add(new Supplier 
                    { 
                        Address = reader.GetString(0),
                        Telephone = reader.GetString(1)
                    });
                }
            }
        }

        return suppliers;
    }
}

public class Supplier
{
    //I would have Address an object....but you have as string
    //public Address Address { get; set; }
    public string Address { get; set; }
    public string Telephone { get; set; }
}

//Example if you went with Address class...
    public class Address
    {
        //Whatever you want in the address
        public string StreetName { get; set; }
        public string Country { get; set; }
        public string Region { get; set; }
        public string City { get; set; }

}

这篇关于Asp.Net:从类返回DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆