示例#1
0
        private void UpdateDataSet(string sSqlCommand, DataSet custDS, bool designMode, string alias, IDbCommand cmd, UpdateComponent uc, List<string> sqlSentences, IDbConnection conn)
        {
            IDbTransaction dbTrans = null;
            Boolean b = false;

            // q 表示master是否有更改。
            bool q = false;
            try
            {
                if (!uc.UseTranscationScope)
                {
                    if (cmd.Connection is OdbcConnection)
                    {
                        uc.AutoTrans = false;
                    }

                    if (uc.AutoTrans)
                    {
                        dbTrans = cmd.Connection.BeginTransaction(uc.TransIsolationLevel);
                        uc.SetTransaction(dbTrans);
                    }
                }

                // ------------------------------------------------------------------------------------
                // Delete先做Details,再做Master。
                DataSet setQ = custDS.GetChanges(DataRowState.Deleted);
                if (setQ != null)
                {
                    ArrayList mySDC = new ArrayList();
                    ArrayList mySDC1 = new ArrayList();

                    ArrayList myDC = GetDetailCommandUp(cmd, mySDC);
                    ArrayList myDC1 = GetDetailCommandUp(cmd, mySDC1, true);

                    for (int i = 0; i < myDC.Count; i++)
                    {
                        sqlSentences.AddRange(_UpdateDataSet(conn, (IDbCommand)myDC[i], custDS, (string)mySDC[i], dbTrans, 1));
                    }

                    for (int i = 0; i < myDC1.Count; i++)
                    {
                        sqlSentences.AddRange(_UpdateDataSet(conn, (IDbCommand)myDC1[i], custDS, (string)mySDC1[i], dbTrans, true, 1));
                    }

                    DataTable tableQ = custDS.Tables[sSqlCommand].GetChanges(DataRowState.Deleted);
                    if (tableQ != null)
                    {
                        //q = true;
                        string tablename = this.GetTableName(cmd, false);
                        string replaceCmd = (string)((object[])ClientInfo)[1];
                        if (conn is OdbcConnection || conn is OleDbConnection)
                            sqlSentences.AddRange(uc.Update(custDS, sSqlCommand, false, replaceCmd, 1, tablename));
                        else
                            sqlSentences.AddRange(uc.Update(custDS, sSqlCommand, replaceCmd, 1));
                    }
                }

                // ------------------------------------------------------------------------------------
                setQ = custDS.GetChanges(DataRowState.Added | DataRowState.Modified);
                if (setQ != null)
                {
                    DataTable tableQ = custDS.Tables[sSqlCommand].GetChanges(DataRowState.Added | DataRowState.Modified);
                    if (tableQ != null)
                    {
                        q = custDS.Tables[sSqlCommand].GetChanges(DataRowState.Added) != null;
                        string replaceCmd = (string)((object[])ClientInfo)[1];
                        string tablename = this.GetTableName(cmd, false);
                        if (conn is OdbcConnection || conn is OleDbConnection || conn.GetType().Name == "IfxConnection")
                            sqlSentences.AddRange(uc.Update(custDS, sSqlCommand, false, replaceCmd, 2, tablename));
                        else
                            sqlSentences.AddRange(uc.Update(custDS, sSqlCommand, replaceCmd, 2));
                    }

                    ArrayList mySDC = new ArrayList();
                    ArrayList mySDC1 = new ArrayList();

                    ArrayList myDC = GetDetailCommandDown(cmd, mySDC);
                    ArrayList myDC1 = GetDetailCommandDown(cmd, mySDC1, true);
                    for (int i = 0; i < myDC.Count; i++)
                    {
                        sqlSentences.AddRange(_UpdateDataSet(conn, (IDbCommand)myDC[i], custDS, (string)mySDC[i], dbTrans, 2));
                    }

                    for (int i = 0; i < myDC1.Count; i++)
                    {
                        sqlSentences.AddRange(_UpdateDataSet(conn, (IDbCommand)myDC1[i], custDS, (string)mySDC1[i], dbTrans, true, 2));
                    }
                }
            }
            catch
            {
                if (!uc.UseTranscationScope)
                {
                    if (uc.AutoTrans)
                    {
                        b = true;
                        dbTrans.Rollback();
                    }
                }
                throw;
            }
            finally
            {
                if (!uc.UseTranscationScope)
                {
                    if (uc.AutoTrans && !b)
                    {
                        dbTrans.Commit();
                    }
                }
                uc.OnAfterApplied(new EventArgs());
                // 同步identity字段
                // Added by yangdong.
                if (q && uc.ServerModify)
                {
                    string tablename = this.GetTableName(cmd, false);
                    if (conn is OdbcConnection || conn is OleDbConnection || conn.GetType().Name == "IfxConnection")
                        uc.IdentitySync(custDS, sSqlCommand, tablename);
                    else
                        uc.IdentitySync(custDS, sSqlCommand);
                }
                if (!designMode)
                {
                    ReleaseConnection(alias, cmd.Connection);
                }
                else
                {
                    cmd.Connection.Close();
                }

            }
        }