Ejemplo n.º 1
0
        /// <summary>
        /// Inserts shapes with the given indices in the specified table.
        /// </summary>
        /// <returns>Number of inserted shapes. -1 if the table doesn't exist.</returns>
        public int InsertShapes(MapWinGIS.Shapefile sf, string tableName, int[] indices)
        {
            this.CheckConnection();

            int count = 0;

            using (DbTransaction dbTrans = m_connection.BeginTransaction())
            {
                using (DbCommand cmd = this.CreateCommand())
                {
                    DbParameter param = this.CreateBinaryParameter();
                    param.SourceColumn  = "Geometry";
                    param.ParameterName = "@Geometry";
                    cmd.Parameters.Add(param);

                    // generating sql and parameters
                    int    fieldCount = sf.NumFields;
                    string sql        = "INSERT INTO [" + tableName + "] ([Geometry], ";
                    string values     = "VALUES (?, ";

                    for (int j = 0; j < fieldCount; j++)
                    {
                        param = this.CreateParameter();

                        MapWinGIS.Field fld = sf.get_Field(j);
                        switch (fld.Type)
                        {
                        case MapWinGIS.FieldType.STRING_FIELD:

                            param.DbType = DbType.StringFixedLength;
                            param.Size   = fld.Width;
                            break;

                        case MapWinGIS.FieldType.INTEGER_FIELD:
                            param.DbType = DbType.Int32;
                            break;

                        case MapWinGIS.FieldType.DOUBLE_FIELD:
                            param.DbType = DbType.Double;
                            break;
                        }

                        param.ParameterName = "@p" + j.ToString();
                        param.SourceColumn  = fld.Name;
                        cmd.Parameters.Add(param);

                        sql    += "[" + fld.Name + "]";
                        sql    += (j == fieldCount - 1) ? ") " : ", ";
                        values += (j == fieldCount - 1) ? "?)" : "?, ";
                    }
                    cmd.CommandText = sql + values;
                    cmd.Transaction = dbTrans;
                    cmd.Connection  = m_connection;
                    System.Diagnostics.Debug.Print(cmd.CommandText);

                    // adding new records
                    DbParameterCollection paramters = cmd.Parameters;
                    MapWinGIS.Table       table     = sf.Table;
                    int maxSize = 0;
                    int percent = 0;

                    for (int i = 0; i < sf.NumShapes; i++)
                    {
                        if (m_callback != null && m_showCallback)
                        {
                            int newPercent = (int)((double)i / (double)(sf.NumShapes - 1) * 100.0);
                            if (newPercent != percent)
                            {
                                m_callback.Progress("", newPercent, "Exporting shapes...");
                                percent = newPercent;
                            }
                        }

                        object          data  = null;
                        MapWinGIS.Shape shape = sf.get_Shape(i);

                        if (shape.ExportToBinary(ref data))
                        {
                            if ((data as byte[]).Length > maxSize)
                            {
                                maxSize = (data as byte[]).Length;
                            }

                            paramters[0].Value = data as byte[];
                            for (int j = 0; j < fieldCount; j++)
                            {
                                paramters[j + 1].Value = table.get_CellValue(j, i);
                            }
                            if (cmd.ExecuteNonQuery() != 0)
                            {
                                count++;
                            }
                        }
                    }

                    if (m_callback != null && m_showCallback)
                    {
                        m_callback.Progress("", 100, "");
                    }
                }
                dbTrans.Commit();
            }
            return(count);
        }