BindingSource

本文探讨了使用BindingSource进行数据更新时必须具备主键的原因,并通过具体代码示例说明了如何实现这一过程。文章指出,在没有主键的情况下,系统无法支持updatecommand的动态SQL生成。

BindingSource更新时更新表一定要有主键。Google了些资料,比较沉闷。

 错误提示信息:对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成。

BindingSource的使用貌似并不影响传说中的分层。。。

ExpandedBlockStart.gif代码
        private void button7_Click(object sender, EventArgs e)
        {
            MonitorRecordBLL bll 
= new MonitorRecordBLL();
            
//更新表一定要有主键
            DataTable dt = bll.UpdateByBindingSource((DataTable)bindingSource1.DataSource);

        }
//BLL
        public DataTable UpdateByBindingSource(DataTable dt)
        {
             MonitorRecordDAL dal 
= new MonitorRecordDAL();
             DataTable datatable
= dal.UpdateByBindingSource(dt);
             
return datatable;
        }
//DAL
        public DataTable UpdateByBindingSource(DataTable dt)
        {
            SqlProcess sp 
= new SqlProcess();
            
string strsql="select * from tddygx";
            DataTable datatable
=sp.UpdateByBindingSource(dt, strsql);
            
return datatable;
        }

 

SqlHelper

 

ExpandedBlockStart.gif代码
        public  DataTable UpdateByBindingSource(DataTable db,string strsql)
        {
            SqlConnection conn 
= new SqlConnection(SQL_CONNECTION_STRING);

            SqlDataAdapter myAdapter 
= new SqlDataAdapter();
            SqlCommand myCommand 
= new SqlCommand(strsql, (SqlConnection)conn);
            myAdapter.SelectCommand 
= myCommand;
            SqlCommandBuilder myCommandBuilder 
= new SqlCommandBuilder(myAdapter);
            
try
            {

                
lock (this)            //处理并发情况(分布式情况)
                {

                    myAdapter.Update(db);

                }

            }

            
catch (Exception)
            {

                conn.Close();
            }
            
return db;    //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds

        }
//以上纯属摘用

 

 

 

转载于:https://www.cnblogs.com/ouyangsi/archive/2010/04/07/1705869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值