开发者

Good approach to interface with database through gridview

开发者 https://www.devze.com 2023-03-17 23:24 出处:网络
What I am doing is I create stored procedures for each table for select/insert/update, then I (if select) fill datatable with all the rows and pass to objectdatasource which is开发者_开发百科 bound wi

What I am doing is I create stored procedures for each table for select/insert/update, then I (if select) fill datatable with all the rows and pass to objectdatasource which is开发者_开发百科 bound with gridview. Is it good or bad technique? What are the good techniques (using BO, BL, DAL) to update/remove/insert records in the database through gridviews?

Thanks!


I do a data model layer which encapsulates all data that I want exposed to the main application. I then have a buisiness logic layer which connects the model layer to the Data Access Layer. The data access layer is where all of the magic happens. In here, I use the System.Data, System.Data.SqlClient or MySQL.Data.MySQLClient (or whatever you use--it has a .NET connector) namespaces to actually call the stored procedures and put the data into the data model or vice versa.

Here is an example of one I have done as I explained, but I do mine a little different. I like my data model to actually expose the access methods, it helps with organization of the data. Also, I use the System.Data.DataTable for binding data to the gridview. It works wonderfully and even allows you store primary key and foreign key information. It will manage passing schema information to the gridview for you.

public class ImageListModel
{
    private ImageListBLL objImageListBLL; 

    public ImageListModel(string connectionString, string databaseEngine, int groupID)
    {
        if(databaseEngine.ToLower() == "mysql")
            objImageListBLL = new ImageListBLL(DatabaseEngine.MySQL, connectionString);
        GroupID = groupID;
    } 

    public int GroupID
    {
        get;
        set;
    } 

    public DataTable GetImageList()
    {
        return objImageListBLL.GetImageList(GroupID);
    } 

    public bool InsertImage(ImageModel objImage)
    {
        objImage.GroupID = GroupID;
        return objImage.Insert();
    } 

    public bool DeleteImage(ImageModel objImage)
    {
        return objImage.Delete();
    } 

    public bool EditImage(ImageModel objImage)
    {
        return objImage.Edit();
    }
} 

public class ImageModel
{
    private ImageBLL objImageBLL;

    public ImageModel(string connectionString, string databaseEngine)
    {
        if (databaseEngine.ToLower() == "mysql")
            objImageBLL = new ImageBLL(DatabaseEngine.MySQL, connectionString);

    } 

    public long ID
    {
        get;
        set;
    }



    public string TitleTop
    {
        get;
        set;
    } 

    public string TitleBottom
    {
        get;
        set;
    }



    public string ImageUrl
    {
        get;
        set;
    } 

    public string ExtraMarkup
    {
        get;
        set;
    } 

    public string DescriptionUrl
    {
        get;
        set;
    } 

    public int Order
    {
        get;
        set;
    }


    public int UserID
    {
        get;
        set;
    } 

    public int GroupID
    {
        get;
        set;
    } 

    public bool Insert()
    {
        return objImageBLL.InsertImage(this);
    } 

    public bool Edit()
    {
        return objImageBLL.EditImage(this);
    } 

    public bool Delete()
    {
        return objImageBLL.DeleteImage(this);
    } 

    public void ChangePosition()
    {
        objImageBLL.ChangeImagePosition(this);
    }
}

As you see, the DAL is completely hidden. The BLL manages calling the appropriate data access layer methods. This models a generic list of images with appropriate data state manipulation, retrieving, and saving methods. This is what modeling should mean in my opinion.

I assume you know the nuts and bolts of how to use a db connector. Let me know if you don't and I will post some more code.

My model works as follows:

               DataModel -- Exposed to main application
                 |
                 |
                \/
           Business Logic Layer
                 |
              pass DataModel
                 |
                \/
            Data Access Layer


This approach is a good way for basic CRUD functions with a gridview (This might be all you need!). Anything more fancy you might consider using an ORM (Object-Relational Mapping) tool to make business objects that you can play around with.


Using ADO.NET Entity Data Models please, it's very simple!

Reference: http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消