⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collecteddataprovider.cs

📁 EpiInfo 开源的导航系统远程序,不知道在哪里下的了,分享一下,有兴趣的
💻 CS
📖 第 1 页 / 共 5 页
字号:
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not insert data table name", ex);
            }
            finally
            {

            }
        }

        #endregion Insert Statements

        #region Delete Statements
        #endregion Delete Statements

        #region Update Statements
        /// <summary>
        /// Saves the current record
        /// </summary>
        /// <param name="view">View of a table.</param>
        /// <param name="recordID">Id of record.</param>
        public int SaveRecord(View view, int recordID)
        {
            Configuration config = Configuration.GetNewInstance();

            try
            {
                #region Input Validation
                if (view == null)
                {
                    throw new ArgumentOutOfRangeException("View");
                }
                if (recordID < 1)
                {
                    throw new ArgumentOutOfRangeException("Record ID");
                }
                #endregion //Input Validation
                StringBuilder sb = new StringBuilder();
                sb.Append("UPDATE [").Append(view.TableName);
                sb.Append(StringLiterals.RIGHT_SQUARE_BRACKET).Append(StringLiterals.SPACE);
                sb.Append("SET").Append(StringLiterals.SPACE);

                foreach (IDataField dataField in view.Fields.DataFields)
                {
                    //                    if (!(dataField is UniqueKeyField) && !(dataField is RecStatusField) && !(dataField is MirrorField))
                    if (!(dataField is UniqueKeyField) && !(dataField is MirrorField))
                    {
                        sb.Append(Util.InsertInSquareBrackets(dataField.Name));
                        sb.Append(StringLiterals.EQUAL);

                        if (dataField is RecStatusField)
                        {
                            if (dataField.CurrentRecordValue == null)   //saving a new record
                            {
                                sb.Append("1, ");
                            }
                            else
                            {
                                sb.Append(int.Parse(dataField.CurrentRecordValue.ToString())).Append(", ");
                            }
                        }
                        else if (dataField is DateField || dataField is DateTimeField || dataField is TimeField)
                        {
                            //if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                            // DEFECT #178
                            DateTime dateResult = DateTime.MinValue;
                            if (DateTime.TryParse(dataField.CurrentRecordValue.ToString(), out dateResult))
                            {
                                sb.Append(Util.InsertInSingleQuotes(dateResult.ToString()));
                                sb.Append(", ");
                            }
                            else
                            {
                                sb.Append("null, ");
                            }
                        }
                        else if (dataField is ImageField)
                        {
                            if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                            {
                                sb.Append(Util.InsertInSingleQuotes(dataField.CurrentRecordValue.ToString()));
                                sb.Append(", ");
                            }
                            else
                            {
                                sb.Append("null, ");
                            }
                        }
                        else if (dataField is CheckBoxField || dataField is YesNoField)
                        {
                            if (dataField is YesNoField)
                            {
                                if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                                {
                                    if (dataField.CurrentRecordValue.Equals("1") || dataField.CurrentRecordValue.Equals(config.Settings.RepresentationOfYes))
                                    {
                                        sb.Append("1, ");
                                    }
                                    else if (dataField.CurrentRecordValue.Equals("0") || dataField.CurrentRecordValue.Equals(config.Settings.RepresentationOfNo))
                                    {
                                        sb.Append("0, ");
                                    }
                                }
                                else
                                {
                                    sb.Append("null, ");
                                }
                            }
                            else if (dataField is CheckBoxField)
                            {
                                if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                                {
                                    if (dataField.CurrentRecordValue.Equals("1") || dataField.CurrentRecordValue.Equals("True"))
                                    {
                                        sb.Append("1, ");
                                    }
                                    else if (dataField.CurrentRecordValue.Equals("0") || dataField.CurrentRecordValue.Equals("False"))
                                    {
                                        sb.Append("0, ");
                                    }
                                }
                                else
                                {
                                    sb.Append("null, ");
                                }
                            }
                        }
                        else if (dataField is NumberField)
                        {
                            //if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                            // DEFECT #178
                            double dblResult = double.MinValue;
                            if (double.TryParse(dataField.CurrentRecordValue.ToString(), out dblResult))
                            {
                                sb.Append(dblResult.ToString()).Append(", ");
                            }
                            else
                            {
                                sb.Append("null, ");
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(dataField.CurrentRecordValue))
                            {
                                sb.Append(Util.InsertInSingleQuotes(dataField.CurrentRecordValue));
                                sb.Append(", ");
                            }
                            else
                            {
                                sb.Append("null, ");
                            }
                        }
                    }
                }
                sb.Remove(sb.Length - 2, 2);

                sb.Append(StringLiterals.SPACE);
                sb.Append("WHERE [Uniquekey] = ").Append(recordID.ToString());
                Query updateQuery = db.CreateQuery(sb.ToString());

                db.ExecuteNonQuery(updateQuery);
                //SaveGridRecord(view, recordID);
                return recordID;
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not save current record.", ex);
            }
            finally
            {

            }
        }
        #endregion //Update Statements

        #region Create Statements

        /// <summary>
        /// Creates a datatable corresponding to a view.
        /// </summary>
        /// <param name="view">View of a table.</param>
        /// <param name="startingId">Starting Id for new table.</param>
        public void CreateDataTableForView(View view, int startingId)
        {
            #region Input Validation
            if (view == null)
            {
                throw new ArgumentNullException("view");
            }
            #endregion Input Validation

            Query createQuery = db.CreateQuery("CREATE TABLE [" + view.TableName + "] " +
                " (UniqueKey INT IDENTITY (" + startingId +
                ",1) PRIMARY KEY NOT NULL, RecStatus SMALLINT NOT NULL Default 1)");
            try
            {
                db.ExecuteNonQuery(createQuery);
                foreach (IInputField field in view.Fields.InputFields)
                {
                    CreateTableColumn(field, view.TableName);
                }
                // GridFields do not inherit IInputField because there is no column
                // for the fields in the collected data.
                foreach (Field field in view.Fields)
                {
                    if (field is GridField)
                    {
                        CreateDataTableForGrid(view, startingId, (GridField)field);
                    }
                }

                //Zack : we definitely need this for related view
                if (view.IsRelatedView == true)
                {
                    IInputField fkeyfield = new NumberField(view);
                    fkeyfield.Name = "FKey";
                    CreateTableColumn(fkeyfield, view.TableName);
                }


            }
            finally
            {

            }
        }

        #endregion Create Statements

        #region Static Methods
        /// <summary>
        /// Finds Epi Version and Purpose of the database.
        /// </summary>
        /// <param name="db">Abstract data type for database.</param>
        /// <returns>Results of test.</returns>
        public static bool IsEpi7CollectedData(IDbDriver db)
        {
            #region Input Validation
            if (db == null)
            {
                throw new ArgumentNullException("DB");
            }
            #endregion

            try
            {
                // Open the database and look for dbInfo table, find Epi Version and purpose ...		
                bool isEpi7CollectedData = false;
                Query query = db.CreateQuery("SELECT [EpiVersion], [Purpose] FROM metaDBInfo");

                DataTable results = db.Select(query);

                if (results.Rows.Count > 0)
                {
                    foreach (DataRow row in results.Rows)
                    {
                        isEpi7CollectedData = (row["EpiVersion"].ToString().Substring(0, 1) == "7") && ((row["Purpose"].ToString() == "0") || (row["Purpose"].ToString() == "2"));
                    }
                }
                return isEpi7CollectedData;
            }
            finally
            {

            }
        }

        #endregion Static Methods

        #region Private Methods

        /// <summary>
        /// Synchronizes a view's tables with its fields
        /// </summary>
        /// <param name="view">View of a table.</param>
        private void SynchronizeTableColumns(View view)
        {
            string sTableName = string.Empty;
            try
            {

                foreach (IInputField field in view.Fields.InputFields)
                {
                    if (!db.ColumnExists(view.TableName, field.Name))
                    {
                        CreateTableColumn(field, view.TableName);
                    }
                }
                // GridFields do not inherit IInputField because there is no column
                // for the fields in the collected data.
                foreach (Field field in view.Fields)
                {
                    if (field is GridField)
                    {
                        sTableName = view.TableName + field.Name;
                        if (!db.TableExists(sTableName))
                        {
                            CreateDataTableForGrid(view, 1, (GridField)field);
                        }
                        else
                        {
                            foreach (GridColumn column in ((GridField)field).Columns)
                            {
                                if (!db.ColumnExists(sTableName, column.Name))
                                {
                                    CreateTableColumn(column, sTableName);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not synchronize table columns", ex);
            }
            finally
            {

            }
        }

        /// <summary>
        /// Creates a field column in a table.
        /// </summary>
        /// <param name="field">A field object</param>
        /// <param name="tableName">Name of the table.</param>
        private void CreateTableColumn(IInputField field, string tableName)
        {
            Query query = db.CreateQuery("ALTER TABLE [" + tableName + "] ADD [" + field.Name + "] " + field.SqlDataType);
            db.ExecuteNonQuery(query);
        }
        /// <summary>
        /// Creates a Gridfield column in a GridField table.
        /// </summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -