// Save the uploaded MDB file to the SDE Database
Protected void btnsavetosde_click (Object sender, eventargs E)
{
// Obtain the server context
Iservercontext servercontext = getservercontext ();
// Open the workspace of the MDB File
ESRI. ArcGIS. Geodatabase. iworkspacefactory WF = servercontext. Createobject ("esridatasourcesgdb. accessworkspacefactory") as iworkspacefactory;
String filename = @ "F: \ mygeodatabase. mdb ";
Ifeatureworkspace pfeatureworkspacemdb = WF. openfromfile (filename, 0) as ifeatureworkspace;
Iworkspace pworkspacemdb = pfeatureworkspacemdb as iworkspace;
// Open the workspace of the SDE space database
Ifeatureworkspace pfeatureworkspacesde = openworkspace (servercontext );
Iworkspace pworkspacesde = pfeatureworkspacesde as iworkspace;
# Region // obtain all the element datasets of MDB and perform conversion and Replication
Ienumdataset penumdataset = pworkspacemdb. get_datasets (esridatasettype. esridtfeaturedataset );
Idataset pdataset = penumdataset. Next ();
// Traverse each data set in MDB
String nameofsourcefeaturedataset = NULL; // source dataset name
String nameoftargetfeaturedataset = NULL; // target dataset name
While (pdataset! = NULL)
{
Nameofsourcefeaturedataset = pdataset. Name;
Nameoftargetfeaturedataset = nameofsourcefeaturedataset + "_ SDE ";
Convertmdbfeaturedatasettosde (servercontext, pworkspacemdb, pworkspacesde, nameofsourcefeaturedataset, nameoftargetfeaturedataset );
Pdataset = penumdataset. Next ();
}
# Endregion
# Region // obtain all the independent element classes in MDB and perform conversion and Replication
Ienumdataset enumdataset = pworkspacemdb. get_datasets (esridatasettype. esridtfeatureclass) as ienumdataset;
Idataset dataset = enumdataset. Next ();
// Traverse each independent element class of MDB
String nameofsourcefeatureclass = NULL; // Source Element Class Name
String nameoftargetfeatureclass = NULL; // name of the target element class
While (Dataset! = NULL)
{
If (Dataset. type = esridatasettype. esridtfeatureclass)
{
Ifeatureclass pfeatureclass = dataset as ifeatureclass;
Nameofsourcefeatureclass = pfeatureclass. aliasname;
Nameoftargetfeatureclass = nameofsourcefeatureclass + "_ SDE ";
Convertmdbfeatureclasstosde (servercontext, pworkspacemdb, pworkspacesde, nameofsourcefeatureclass, nameoftargetfeatureclass );
Dataset = enumdataset. Next ();
}
Else
{
Return;
}
}
# Endregion
// Release the server context
Servercontext. releasecontext ();
}
// Obtain the server context
Public iservercontext getservercontext ()
{
# Region creates server context through igisserverconnection
Igisserverconnection gisserverconnection = new gisserverconnection ();
Gisserverconnection. Connect ("server name ");
Iserverobjectmanager4 serverobjectmanager = (iserverobjectmanager4) gisserverconnection. serverobjectmanager;
Iservercontext servercontext = serverobjectmanager. createservercontext ("","");
# Endregion
Return servercontext;
}
// Open the SDE Database
Public ifeatureworkspace openworkspace (iservercontext servercontext)
{
// Set the connection property of the SDE Database
Ipropertyset pproperty = servercontext. Createobject ("esrisystem. propertyset") as ipropertyset;
Pproperty. setproperty ("server", "FMS"); // server name or IP address
Pproperty. setproperty ("instance", "esri_sde ");//
Pproperty. setproperty ("Database", "SDE database name"); // SDE Database Name
Pproperty. setproperty ("user", "User Name"); // User Name
Pproperty. setproperty ("password", "password"); // User Password
Pproperty. setproperty ("version", "SDE. Default"); // connection version
// Create an iworkspacefactory object
ESRI. ArcGIS. Geodatabase. iworkspacefactory2 WF = servercontext. Createobject ("esricescesgdb. sdeworkspacefactory") as iworkspacefactory2;
// Open the ifeatureworkspace object
Ifeatureworkspace FWS = WF. Open (pproperty, 0) as ifeatureworkspace;
Return FWS;
}
// Convert the element dataset in MDB to the SDE Database
Public void convertmdbfeaturedatasettosde (iservercontext servercontext, iworkspace sourceworkspace, iworkspace targetworkspace, string nameofsourcefeaturedataset, string nameoftargetfeaturedataset)
{
If (sourceworkspace = NULL | targetworkspace = NULL)
{
Return;
}
// Create the source workspace name
Idataset sourceworkspacedataset = (idataset) sourceworkspace;
Iworkspacename sourceworkspacename = (iworkspacename) sourceworkspacedataset. fullname;
// Create the source dataset name
Ifeaturedatasetname sourcefeaturedatasetname = servercontext. Createobject ("esrigeodatabase. featuredatasetname") as ifeaturedatasetname;
Idatasetname sourcedatasetname = (idatasetname) sourcefeaturedatasetname;
Sourcedatasetname. workspacename = sourceworkspacename;
Sourcedatasetname. Name = nameofsourcefeaturedataset;
// Create the target workspace name
Idataset targetworkspacedataset = (idataset) targetworkspace;
Iworkspacename targetworkspacename = (iworkspacename) targetworkspacedataset. fullname;
// Create the target dataset name
Ifeaturedatasetname targetfeaturedatasetname = servercontext. Createobject ("esrigeodatabase. featuredatasetname") as ifeaturedatasetname;
Idatasetname targetdatasetname = (idatasetname) targetfeaturedatasetname;
Targetdatasetname. workspacename = targetworkspacename;
Targetdatasetname. Name = nameoftargetfeaturedataset;
// Convert (copy) the source dataset to the target Dataset
Ifeaturedataconverter featuredataconverter = servercontext. Createobject ("esrigeodatabase. featuredataconverter") as ifeaturedataconverter;
Featuredataconverter. convertfeaturedataset (sourcefeaturedatasetname, targetfeaturedatasetname, null, "", 1000, 0 );
}
// Copy the element classes in MDB to the SDE Database
Public void convertmdbfeatureclasstosde (iservercontext servercontext, iworkspace sourceworkspace, iworkspace targetworkspace, string nameofsourcefeatureclass, string nameoftargetfeatureclass)
{
// Create a source workspace name
Idataset sourceworkspacedataset = (idataset) sourceworkspace;
Iworkspacename sourceworkspacename = (iworkspacename) sourceworkspacedataset. fullname;
// Create the source dataset name
Ifeatureclassname sourcefeatureclassname = servercontext. Createobject ("esrigeodatabase. featureclassname") as ifeatureclassname;
Idatasetname sourcedatasetname = (idatasetname) sourcefeatureclassname;
Sourcedatasetname. workspacename = sourceworkspacename;
Sourcedatasetname. Name = nameofsourcefeatureclass;
// Create the name of the target Workspace
Idataset targetworkspacedataset = (idataset) targetworkspace;
Iworkspacename targetworkspacename = (iworkspacename) targetworkspacedataset. fullname;
// Create a target dataset name
Ifeatureclassname targetfeatureclassname = servercontext. Createobject ("esrigeodatabase. featureclassname") as ifeatureclassname;
Idatasetname targetdatasetname = (idatasetname) targetfeatureclassname;
Targetdatasetname. workspacename = targetworkspacename;
Targetdatasetname. Name = nameoftargetfeatureclass;
// Open the input element class to get the field Definition
ESRI. ArcGIS. esrisystem. INAME sourcename = (ESRI. ArcGIS. esrisystem. INAME) sourcefeatureclassname;
Ifeatureclass sourcefeatureclass = (ifeatureclass) sourcename. open (); // Open Source Element class
// Verify the field name because you are converting data between different types of workspaces
Ifieldchecker fieldchecker = servercontext. Createobject ("esrigeodatabase. fieldchecker") as ifieldchecker;
Ifields sourcefeatureclassfields = sourcefeatureclass. fields;
Ifields targetfeatureclassfields;
Ienumfielderror enumfielderror;
// Enter and verify the workspace for the most important settings
Fieldchecker. inputworkspace = sourceworkspace;
Fieldchecker. validateworkspace = targetworkspace;
Fieldchecker. Validate (sourcefeatureclassfields, out enumfielderror, out targetfeatureclassfields );
// Traverse all output fields to find geometric Fields
Ifield geometryfield;
For (INT I = 0; I <targetfeatureclassfields. fieldcount; I ++)
{
If (targetfeatureclassfields. get_field (I). type = esrifieldtype. esrifieldtypegeometry)
{
Geometryfield = targetfeatureclassfields. get_field (I );
// Obtain the geometric definition of the geometric field
Igeometrydef geometrydef = geometryfield. geometrydef;
// Assign ry a space index number and grid size value
Igeometrydefedit targetfcgeodefedit = (igeometrydefedit) geometrydef;
Targetfcgeodefedit. gridcount_2 = 1;
Targetfcgeodefedit. set_gridsize (0, 0 );
// Allows ArcGIS to determine a valid grid size for Data Loading
Targetfcgeodefedit. spatialreference_2 = geometryfield. geometrydef. spatialreference;
// All elements of the conversion element class
Iqueryfilter queryfilter = servercontext. Createobject ("esrigeodatabase. queryfilter") as iqueryfilter ;;
Queryfilter. whereclause = "";
// Load element class
Ifeaturedataconverter fctofc = servercontext. Createobject ("esrigeodatabase. featuredataconverter") as ifeaturedataconverter;
Ienuminvalidobject enumerrors = fctofc. convertfeatureclass (sourcefeatureclassname, queryfilter, null, targetfeatureclassname, geometrydef, targetfeatureclassfields, "", 1000, 0 );
Break;
}
}
}