使用无连接方法的三层插入数据 [英] three tier inserting data using connectionless approach

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

问题描述


我被卡在这个鳕鱼上.我从两个星期开始尝试.请阅读整个代码,然后告诉我问题所在.错误是,它在oOrdersDataAdapter.Update(oDS,"info");处创建异常. (无法优化数据表"info").
我正在用无连接方法的3层方法制作插入按钮.现在是我最后的希望.

在数据库层中,代码为:

Hi,
I am stuck at this codde. I am trying it from two weeks. Please read that whole code and tell me the problem. The error is, that it''s creating exception at oOrdersDataAdapter.Update(oDS, "info"); (unable to fine datatable "info").
I am making insert button with 3 tier approach with connectionless approach. It is my last hope now.

In Database layer the Code is:

public class stddbl
    {
        OleDbConnection MAconn;

    
        public static string connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:\\db.mdb";
        
public DataSet dal_search(stdprops p)
        {
            
                MAconn = new OleDbConnection();
                MAconn.ConnectionString = connectionString;
                MAconn.Open();
                DataSet oDS = new DataSet();
                string query = "SELECT * FROM info";

                OleDbDataAdapter oOrdersDataAdapter = new OleDbDataAdapter(query, connectionString);

                OleDbCommandBuilder oOrdersCmdBuilder = new OleDbCommandBuilder(oOrdersDataAdapter);

                oOrdersDataAdapter.Fill(oDS);

                oOrdersDataAdapter.Update(oDS, "info"); //there is error no  datatable info
                return oDS;
                
                  
        }



在业务层中:



In Business Layer:

public class stdbo
{
    stddbl d = new stddbl();

    public DataSet bll_search(stdprops p)
    {
        //OleDbDataAdapter da = new OleDbDataAdapter();
        DataSet da = new DataSet();
        da = d.dal_search(p);
        return da;

    }




在表示层中:




in Presentation Layer:

private void button1_Click(object sender, EventArgs e)
       {
           p.Name = textBox1.Text;
           p.Cell = textBox2.Text;

           DataSet oDS = obj.bll_search(p);
               //OleDbDataAdapter oDS = obj.bll_search();
               DataTable pTable = oDS.Tables["Table"];
               pTable.TableName = "info";

               // Insert the Data
               DataRow oOrderRow = oDS.Tables["info"].NewRow();
               oOrderRow["name"] = textBox1.Text;
               oOrderRow["cell"] = textBox2.Text;
               oDS.Tables["info"].Rows.Add(oOrderRow);
               //oDS.Tables["Orders"].Rows.Add(oOrderRow);
               //oOrdersDataAdapter.Update(oDS, "info");
               MessageBox.Show("inserted");


       }




我也有属性类:-




I have the properties class as well:-

public class stdprops
    {


        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string cell;

        public string Cell
        {
            get { return cell; }
            set { cell = value; }
        }
    }
}

推荐答案

我不确定为什么要在dal_search方法中进行更新调用,那时候您只是在填充数据并返回一个数据集....它可能应该读为...

I''m not sure why you''re doing an update call in the dal_search method, at that point you''re just populating data and returning a dataset....it should probably read...

public DataSet dal_search(stdprops p)
{    
    DataSet searchResults = new DataSet();
    string query = "SELECT * FROM info"; // Build up you SQL params? We're not using the stdprops object at all
    
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        connection.Open();               
        using (OleDbCommand command  = New OleDbCommand(query, connection))
        {
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
            {
                adapter.Fill(searchResults);                
                return searchResults;
            }            
        } 
    }                        
}



如果要在同一过程中修改数据并提交结果,则仅需要使用命令构建器对象,请在此处查看

http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx [ ^ ]

如果您只是从方法中返回数据,则不需要此



You only need to use the command builder object if you were going to modify the data and commit the results within the same procedure, have a look here

http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx[^]

If you''re just returning data from the method, you don''t need this


这篇关于使用无连接方法的三层插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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