年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

294 lines
13 KiB

using Aspose.Cells;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.AE;
using KGIS.Framework.DBOperator;
using KGIS.Framework.Maps;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Kingo.Plugin.DataLoad.View
{
/// <summary>
/// FrmLoadGJWTTB.xaml 的交互逻辑
/// </summary>
public partial class FrmLoadGJWTTB : BaseWindow
{
public FrmLoadGJWTTB()
{
InitializeComponent();
}
private void btnBrowse_Click_1(object sender, RoutedEventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.txtPath.Text = dialog.SelectedPath;
}
}
private void btnImport_Click_1(object sender, RoutedEventArgs e)
{
OleDbConnection conn = null;
OleDbTransaction pTrans = null;
try
{
if (string.IsNullOrWhiteSpace(txtPath.Text))
{
MessageHelper.Show("未获取到文件路径:" + txtPath.Text);
return;
}
string path = txtPath.Text;
ProjectInfo info = (MapsManager.Instance.CurrProjectInfo as ProjectInfo);
string wttbPath = info.ProjDir + "\\gjwttb.mdb";
string mdbTempPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\Kingo.Plugin.DataLoad\\Resources\\ProcessData.mdb";
File.Copy(mdbTempPath, wttbPath, true);
foreach (string file in Directory.GetFiles(path))
{
FileInfo fileInfo = new FileInfo(file);
switch (fileInfo.Extension.ToUpper())
{
case ".XLS":
case ".XLSX":
{
Workbook workbook = new Workbook(fileInfo.FullName);
Cells cells = workbook.Worksheets[0].Cells;
DataTable dtGJHCJG = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn + 1, false);
foreach (DataColumn c in dtGJHCJG.Columns)
{
try
{
c.ColumnName = dtGJHCJG.Rows[0][c.ColumnName].ToString();
}
catch
{
}
}
dtGJHCJG.Rows.RemoveAt(0);
if (dtGJHCJG != null && dtGJHCJG.Rows.Count > 0)
{
try
{
string connectionString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + wttbPath;
conn = new OleDbConnection(connectionString);
conn.Open();
}
catch
{
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + wttbPath;
conn = new OleDbConnection(connectionString);
conn.Open();
}
OleDbCommand cmd = new OleDbCommand("drop table DLTB_WTTB ", conn);
try
{
cmd.CommandText = "drop table DLTB_WTTB ";
cmd.ExecuteNonQuery();
}
catch { }
//}
try
{
cmd.CommandText = "Create TABLE DLTB_WTTB (BSM varchar(50),地类核查结果说明 Memo,属性核查结果说明 memo,其他核查结果说明 memo,备注 memo,照片地类 memo,情况说明 memo)";
cmd.ExecuteNonQuery();
}
catch { }
foreach (DataRow dr in dtGJHCJG.Rows)
{
try
{
string insertSQL = string.Format("insert into DLTB_WTTB (BSM,地类核查结果说明,属性核查结果说明,其他核查结果说明,备注,照片地类,情况说明) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", dr["标识码"], dr["地类核查结果说明"], dr["属性核查结果说明"], dr["其他核查结果说明"], dr["备注"], dr["照片地类"], dr["情况说明"]);
cmd.CommandText = insertSQL;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//空行数据插入异常,不抛出
}
}
dtGJHCJG.Clear();
if (conn != null)
{
conn.Close();
conn.Dispose();
}
GC.Collect();
}
}
break;
case ".VCT":
KGIS.Framework.VCT.Helper.VCTConvertGeodatabaseHelper helper = new KGIS.Framework.VCT.Helper.VCTConvertGeodatabaseHelper();
helper.RootPath = System.IO.Path.GetDirectoryName(fileInfo.FullName);
helper.VCTConvertGeodatabse(fileInfo.FullName, 1, true, false);
break;
default:
break;
}
}
foreach (string dir in Directory.GetDirectories(path))
{
if (dir.Contains("GXGC.gdb"))
{
WorkspaceAPI workspaceAPI = new WorkspaceAPI(dir, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile);
IFeatureClassAPI featureAPI = workspaceAPI.OpenFeatureClass("DLTB");
WorkspaceAPI workspaceAPI_MDB = new WorkspaceAPI(wttbPath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.MDBFile);
ISpatialReference pSR = (featureAPI.FeatureClass as IGeoDataset).SpatialReference;
GeoDBAPI.CreateFields(featureAPI.FeatureClass.Fields, out IFields ifields);
IFeatureClassAPI CreateFeatureClass = workspaceAPI_MDB.CreateFeatureClass("DLTBGXGC", pSR, ifields);
IFeatureClass fcTargetTemp = CreateFeatureClass.FeatureClass;
featureAPI.FcToFc(fcTargetTemp, null, false);
//if (fcTargetTemp != null)
// Marshal.ReleaseComObject(fcTargetTemp);
//CreateFeatureClass.CloseFeatureClass();
//workspaceAPI_MDB.CloseWorkspace(false);
List<string> lstSql = new List<string>();
lstSql.Add("delete distinctrow a.* from (DLTBGXGC a left join DLTB_WTTB b on a.bsm=b.bsm) where b.bsm is null");
string[] toFieldsSet = "BSM,地类核查结果说明,属性核查结果说明,其他核查结果说明,备注,照片地类,情况说明".Split(',');
string[] fieldsLengthSet = "varchar(50),Memo,memo,memo,memo,memo,memo".Split(',');
string sql = string.Empty;
for (int i = 0; i < toFieldsSet.Length; i++)
{
if (!string.IsNullOrEmpty(toFieldsSet[i]) && toFieldsSet[i] != "BSM")
{
//sql += "column " + toFieldsSet[i] + " " + fieldsLengthSet[i] + ",";
CreateFeatureClass.AddField(toFieldsSet[i], esriFieldType.esriFieldTypeString, toFieldsSet[i]);
}
}
//sql = sql.Substring(0, sql.Length - 1);
//lstSql.Add("alter table DLTBGXGC add " + sql);
sql = string.Empty;
for (int i = 0; i < toFieldsSet.Length; i++)
{
if (!string.IsNullOrEmpty(toFieldsSet[i]) && toFieldsSet[i] != "BSM")
{
sql += "a." + toFieldsSet[i] + "=b." + toFieldsSet[i] + ",";
}
}
sql = sql.Substring(0, sql.Length - 1);
lstSql.Add("Update DLTBGXGC a,DLTB_WTTB b set " + sql + " where a.BSM=b.BSM");
ExcuteListSQL(lstSql, wttbPath);
workspaceAPI_MDB = new WorkspaceAPI(wttbPath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.MDBFile);
//WorkspaceAPI workspaceAPI_Scheme = new WorkspaceAPI(info.GetSchemeDBPath(), KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile);
//workspaceAPI_Scheme.DeleteFeatureClass("SB_DLTBGXGC");
//GeoDBAPI.CreateFields(CreateFeatureClass.FeatureClass.Fields, out IFields ifields2);
//IFeatureClass fcTargetTemp_SB = workspaceAPI_Scheme.CreateFeatureClass("SB_DLTBGXGC", pSR, ifields2).FeatureClass;
//IFeatureClassAPI featureAPI_MDB = workspaceAPI_MDB.OpenFeatureClass("DLTBGXGC");
//CreateFeatureClass.FcToFc(fcTargetTemp_SB, null, false);
}
}
MessageHelper.Show("加载完成!");
this.Close();
}
catch (Exception ex)
{
MessageHelper.ShowError("加载国家问题图斑异常:" + ex.Message);
}
}
public static IRDBHelper CreateDbHelper(string mdbPath)
{
IRDBHelper irdbhelper = null;
string strConn = "provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}";
try
{
strConn = string.Format(strConn, mdbPath);
irdbhelper = RDBFactory.CreateDbHelper(strConn, DatabaseType.MSAccess);
}
catch (Exception ex)
{
try
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}";
strConn = string.Format(strConn, mdbPath);
irdbhelper = RDBFactory.CreateDbHelper(strConn, DatabaseType.MSAccess);
}
catch (Exception ex1)
{
LogAPI.Debug(ex1);
return null;
}
}
return irdbhelper;
}
/// <summary>
/// 执行多条操作数据库的sql语句
/// </summary>
/// <param name="strSql"></param>
public static int ExcuteListSQL(List<string> lstSql, string mdbPath)
{
IRDBHelper irdbhelper = null;
try
{
int i = -1;
irdbhelper = CreateDbHelper(mdbPath);
foreach (string str in lstSql)
{
i = irdbhelper.ExecuteSQL(str);
if (i < 0 && !str.ToUpper().Contains("DROP"))
{
LogAPI.Debug("分析sql失败" + str);
}
}
return 1;
}
catch (Exception ex)
{
irdbhelper.Rollback();
return -1;
}
finally
{
if (irdbhelper != null)
irdbhelper.DisConnect();
}
}
}
}