예제 #1
0
        /// <summary>
        /// Save shapefile in the specified database. It calls CreateTable and InsertShapes internally.
        /// </summary>
        /// <param name="sf">Shapfile object to save into database</param>
        /// <param name="tableName">The table name to save shapefile into. The new table will be created.
        /// If table with such name exists it will be rewritten.</param>
        public bool SaveShapefile(MapWinGIS.Shapefile sf, string tableName, bool overwrite)
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            bool result = false;

            // deleting the table
            if (DbUtilities.TableExists(m_connection, tableName))
            {
                if (overwrite)
                {
                    this.DeleteTable(tableName);
                }
                else
                {
                    return(false);
                }
            }

            //creating new one
            if (!this.CreateTable(sf, tableName))
            {
                return(false);
            }

            //inserting
            int count = this.InsertShapes(sf, tableName, false);

            result = count == sf.NumShapes;

            watch.Stop();
            Debug.Print(watch.Elapsed.ToString());
            return(result);
        }
예제 #2
0
        /// <summary>
        /// Creates a table based upon fileds of specified shapefile
        /// </summary>
        public bool CreateTable(MapWinGIS.Shapefile sf, string tableName)
        {
            this.CheckConnection();

            DbCommand command = m_connection.CreateCommand();

            string sql = " ([Geometry] " + m_provider.GetBinaryTypeName() + ", ";

            int count = sf.NumFields;

            for (int i = 0; i < count; i++)
            {
                MapWinGIS.Field field = sf.get_Field(i);
                string          s     = "[" + field.Name + "] ";
                switch (field.Type)
                {
                case MapWinGIS.FieldType.DOUBLE_FIELD:
                    s += m_provider.GetDoubleTypeName();
                    break;

                case MapWinGIS.FieldType.INTEGER_FIELD:
                    s += m_provider.GetIntergerTypeName();
                    break;

                case MapWinGIS.FieldType.STRING_FIELD:
                    bool   fixedLength = true;
                    string name        = m_provider.GetStringTypeName(out fixedLength);
                    s += fixedLength ? name + "(" + field.Width.ToString() + ")" : name;
                    break;

                default:
                    continue;
                }
                sql += s;
                sql += i < count - 1 ? ", " : ")";
            }

            command.CommandText = "CREATE TABLE [" + tableName + "]" + sql;
            command.ExecuteNonQuery();
            return(DbUtilities.TableExists(m_connection, tableName));
        }