星空网 > 软件开发 > ASP.net

关于DataSet事务处理以及SqlDataAdapter四种用法

如果是直接执行SQL语句时,事务很好处理,对于大多数的Erp应用,不能能用SQL来处理数据,所以更新DataSet更为常用,更新单个的DataSet也非常简单,不需要事务的处理,给多个DataSet增加事务多数应用于分布式的程序代码中,下面为在Webservice中更新Winform传递过来的经过压缩的数据集的事务处理代码,多个DataSet 情况。

 
/// <summary>
    /// 更新经过压缩的DataSet
    /// </summary>
    /// <param >环境变量</param>
    /// <param >二进制的DataSet</param>
    /// <returns>出现异常,返回一个错误信息</returns>
 
        public object UpdataSet(object[] env, byte[] byt)
    {
        object info = null;
        SqlConnection sqlconn = null;
        //定义事务
        SqlTransaction transaction = null;
        DataSet ds = null;
        try
        {
            ds = md.BytesToDs(byt);
            string Url = "数据库连接语名";
            sqlconn = new SqlConnection(Url);
            sqlconn.Open();
            transaction = sqlconn.BeginTransaction(IsolationLevel.ReadCommitted, "transname");//实例化事务
            SqlDataAdapter adapter;
            SqlCommandBuilder objCommandBuilder;
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                adapter = new SqlDataAdapter("select * from " + ds.Tables[i].TableName + " where 2>3", sqlconn);//传统用法
                objCommandBuilder = new SqlCommandBuilder(adapter);
                //开始挂起
                //挂起操作
                adapter.DeleteCommand = new SqlCommand("...删除语句..", sqlconn, transaction);
                //adapter的增删查改可以与事务一起用,注意这里没有实例SqlCommand对象所以new SqlCommand
                adapter.InsertCommand = new SqlCommand("............", sqlconn, transaction);
                adapter.UpdateCommand = new SqlCommand(".............", sqlconn, transaction);
                adapter.SelectCommand = new SqlCommand("select * from " + ds.Tables[i].TableName + " where 2>3", sqlconn, transaction);
                //以上是adapter的新用法,new SqlCommand后面写sql语句,SqlConnection,还可以写事务...就不用实例SqlCommand对象
 
                //没有实例SqlCommand对象所以获取
                adapter.DeleteCommand = objCommandBuilder.GetDeleteCommand();
                adapter.InsertCommand = objCommandBuilder.GetInsertCommand();
                adapter.UpdateCommand = objCommandBuilder.GetUpdateCommand();
 
                objCommandBuilder.DataAdapter.Update(ds, ds.Tables[i].TableName.ToString());//
                
            }
            transaction.Commit();//无误后提交事务
            sqlconn.Close();
        }
        catch (Exception err)
        {
            Console.Out.WriteLine("存盘时发生错误:" + err.Message);
            info = "存盘时发生错误:" + err.Message;
            //事务回滚
            transaction.Rollback();
        }
        finally
        {
            if (sqlconn != null)
            {
                sqlconn.Close();
            }
        }
        return info;
    }



原标题:关于DataSet事务处理以及SqlDataAdapter四种用法

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国海运专线价格表:https://www.goluckyvip.com/tag/38282.html
美国华盛顿专线:https://www.goluckyvip.com/tag/38284.html
美国化妆品专线:https://www.goluckyvip.com/tag/38285.html
美国货物专线:https://www.goluckyvip.com/tag/38287.html
美国货运专线:https://www.goluckyvip.com/tag/38288.html
美国加州海外仓:https://www.goluckyvip.com/tag/38289.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流