在3层中使用数据集和dataadapter插入数据... [英] Inserting data using dataset and dataadapter in 3 tier...

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

问题描述

我正在制作一个3层应用程序,用于使用数据适配器和数据集插入数据.有一些问题.

检查其中是否不包含所有代码属性层.
databaselayerwork,dbcon类

I am making a 3 tier app for inserting data using data adpater and data set. There are some issues.

check all code properties layer is not included in it.
databaselayerwork, dbcon class

public class dbcon

{
 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);
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.Message);
            }
            return ds;






调用该类的数据库层工作:-







databse layer work calling that class :-


 public DataSet dal_insert(adddelBookProps p)
        {

            string query = "select * from books ";
            DataSet ds = obj.insertRecord(query);

            return ds;
}






业务层

工作:-






Business layer

work:-

public DataSet binsert(adddelBookProps p)
        {

            DataSet ds = new DataSet();
            //OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
            ds = t.dal_insert(p);
            return ds;

        }






主窗体Insrt按钮的工作方式







Main Form Insrt button work


protected void btnadd_Click(object sender, EventArgs e)
    {
       
        adddelBookProps p = new adddelBookProps();
        admenRegBll obj = new admenRegBll();
        p.Bookno = booknotxt.Text;
        p.Isbn=isbntxt.Text;
        p.Booktitle=booktitletxt.Text;
        p.Author=authortxt.Text;
        p.Quantity=quantitytxt.Text;
        p.Price=pricetxt.Text;
        p.Published = pytxt.Text;

        DataSet ds = obj.binsert(p);
        //OleDbDataAdapter dataAdapter = obj.binsert;
        DataTable dt = ds.Tables["Table"];
        dt.TableName = "books";

        // Insert the Data
        DataRow dr = ds.Tables["books"].NewRow();
        dr["bookno"] = p.Bookno;
        dr["isbn"] = p.Isbn;
        dr["booktitle"] = p.Booktitle;
        dr["author"] = p.Author;
        dr["quantity"] = p.Quantity;
        dr["price"] = p.Price;
        dr["pubyear"] = p.Published;
        ds.Tables["books"].Rows.Add(dr);
        //oDS.Tables["Orders"].Rows.Add(oOrderRow);
        
        dataAdapter.Update(ds, "books");
        //MessageBox.Show("inserted");

推荐答案

这里的事物以及已经提到的事物


* dataSet dal_insert()
进行选择,而不是插入

*在insertRecord(String query)例程中
我认为OleDb *与Sql *调用的工作方式相同,在Sql *调用中,您需要在SqlCommandBuilder类之前为适配器创建一个SqlCommand对象


例如:
Couple of things here, as well as those mentioned already


* dataSet dal_insert()
does a select, not an insert

* in the insertRecord(String query) routine
I think OleDb* works the same as Sql* calls and in Sql* calls you need to create a a SqlCommand object for the adaptor before the SqlCommandBuilder class


example:
<br />
                SqlConnection conn = new SqlConnection(TheConnectionString);<br />
                SqlCommand cmd = new SqlCommand("select * from inventory");<br />
                cmd.Connection = conn; <br />
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);<br />
                SqlCommandBuilder bld = new SqlCommandBuilder(adapter );<br />
                // just for reference below <br />
                SqlCommand cmdUpdate = bld.GetUpdateCommand();<br />
                SqlCommand cmdInsert = bld.GetInsertCommand();<br />



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

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