帮助我在3层中使用数据集和dataadapter插入数据 [英] help me in inserting data using dataset and dataadapter in 3 tier

查看:53
本文介绍了帮助我在3层中使用数据集和dataadapter插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个3层应用程序,用于使用DataSet和DataAdapter进行插入,更新和删除.这不是任何语法错误,但是存在一些逻辑错误.请帮帮我.

数据库层的代码为:

I''m making a 3 tier application for insert, update and delete using DataSet and DataAdapter. This is not any kind of syntax error, but there is some logical error. Please help me.

The code for database layer is:

class dbcon
    {
        public static string connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\\testdb.mdb";
       
        OleDbConnection MAconn;
        OleDbCommand MAcmd;
        public DataSet insertRecord(String query)
        {
            DataSet ds = null;
            try
            {
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                OleDbCommandBuilder cmd = new OleDbCommandBuilder(dataAdapter);
                ds = new DataSet();
                dataAdapter.Fill(ds);
                
                dataAdapter.Update(ds);
                
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message);
            }
            return ds;
        }

using propertieslayer;

namespace databaselayer
{
    public class adddelBookDbl
    {
        dbcon obj=new dbcon();
        public DataSet dal_insert(adddelBookProps p)
        {
            string query = "select * from books ";
            DataSet ds = obj.insertRecord(query);

            return ds;
        }


业务层工作:


Business layer work:

namespace businesslayer
{
    public class admenRegBll
    {
        admenRegDbl d = new admenRegDbl();
        adddelBookDbl t = new adddelBookDbl();
       
        public DataSet binsert(adddelBookProps p)
        {
           DataSet ds = t.dal_insert(p);
           return ds; 
        }


主要形式插入按钮编码:


Main form Insert button coding:

protected void btnadd_Click(object sender, EventArgs e)
    {
        adddelBookProps p = new adddelBookProps();
        admenRegBll obj = new admenRegBll();
      
        DataSet ds = obj.binsert(p);
      
        DataTable dt = ds.Tables["Table"];
        dt.TableName = "books";

        // Insert the Data
        DataRow dr = ds.Tables["books"].NewRow();
        dr["bookno"] = booknotxt.Text;
        dr["isbn"] = isbntxt.Text;
        dr["booktitle"] = booktitletxt.Text;
        dr["author"] = authortxt.Text;
        dr["quantity"] = quantitytxt.Text;
        dr["price"] = pricetxt.Text;
        dr["pubyear"] = pytxt.Text;
        ds.Tables["books"].Rows.Add(dr);
    }


在属性层中:


In properties layer:

namespace propertieslayer
{
    public class adddelBookProps
    {
        private string bookno;
        public string Bookno
        {
            get { return bookno; }
            set { bookno = value; }
        }
        private string isbn;

        public string Isbn
        {
            get { return isbn; }
            set { isbn = value; }
        }

        private string booktitle;

        public string Booktitle
        {
            get { return booktitle; }
            set { booktitle = value; }
        }

        private string author;

        public string Author
        {
            get { return author; }
            set { author = value; }
        }

        private string quantity;

        public string Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }

        private string price;

        public string Price
        {
            get { return price; }
            set { price = value; }
        }

        private string publishedyear;

        public string Published
        {
            get { return publishedyear; }
            set { publishedyear = value; }
        }
    }

推荐答案

首先,您没有在编码中进行任何分层.层级涉及职责分离,以便每个层级彼此独立执行.换句话说,层是可替代的.在您单击按钮进行编码时,可以访问您的持久性逻辑.我没有看到任何分离.
First thing you are not doing any tiers in your coding. Tiers involves for segregation of responsibilities, so that each tier perform independent of other. In other words tiers are substitutable. In you coding at button click has access to your persistence logic. I am not seeing any kind of separation.
DataSet ds = obj.binsert(p); The 'p' travels down the stack but not used anywhere. 

       public DataSet dal_insert(adddelBookProps p)
        {
            string query = "select * from books ";
            DataSet ds = obj.insertRecord(query);
 
            return ds;
        }


p达到上述功能而死亡.

DataSet ds = obj.binsert(p);
这在堆栈中调用以更新数据库,但是之后您要更改数据集...


The p coming to the above function and died.

DataSet ds = obj.binsert(p);
This down the stack calls to update the database, but after that you are altering the dataset...

DataSet ds = obj.binsert(p);

DataTable dt = ds.Tables["Table"];
dt.TableName = "books";

// Insert the Data
DataRow dr = ds.Tables["books"].NewRow();
dr["bookno"] = booknotxt.Text;
dr["isbn"] = isbntxt.Text;
dr["booktitle"] = booktitletxt.Text;
dr["author"] = authortxt.Text;
dr["quantity"] = quantitytxt.Text;
dr["price"] = pricetxt.Text;
dr["pubyear"] = pytxt.Text;
ds.Tables["books"].Rows.Add(dr);


更改将不会保留.

如果您不清楚分层,则可以使用简单模型或现成的实体框架模型/模板或数据驱动模板Asp.Net动态数据实体Web应用程序可能会有所帮助.当您开始一个新项目时,这些模板将可用.

确定您的应用程序是数据驱动的还是模型驱动的. ASP.Net具有一个MVC框架,该框架非常适合数据驱动的应用程序.

如果您想创建一个分层的企业应用程序,这些书可能会有所帮助.请参阅企业应用程序体系结构的Martin Fowlers模式.具有C#和专业Asp.Net设计模式的Wrox出版商的域驱动设计.

如果您的应用程序很简单,那就让它变得简单,而无需与层混淆

祝你好运


The changes are not going to be persisted.

If you are not clear about layering then can use a simple model or an readily available entity framework model/ template or the data driven template Asp.Net Dynamic Data Entities Web Applications may help. When you start a new project these templates will be available.

Determine your application is a data driven or model driven. ASP.Net has an MVC framework which works well for data driven applications.

If you want to create a tiered enterprise application these books may be helpful. Refer Martin Fowlers Pattern''s of Enterprise Application Architecture. Wrox publisher''s Domain Driven Design with C# and Professional Asp.Net Design Patterns.

If your application is simple one make it simple, no need confuse with tiers

Good luck


这篇关于帮助我在3层中使用数据集和dataadapter插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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