Пример #1
0
        /// <summary>
        /// 给指定的WorkspaceName生成GP字符串
        /// </summary>
        /// <param name="wsNameTarget"></param>
        /// <returns></returns>
        public static string GetGpString(IWorkspaceName wsNameTarget)
        {
            if (wsNameTarget == null)
            {
                return(null);
            }

            if (wsNameTarget.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
            {
                try
                {
                    string strTempPath = System.IO.Path.GetPathRoot(System.Environment.SystemDirectory); // System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), "SDETemp");
                    string strTempName = string.Format("Temp_{0}", wsNameTarget.GetHashCode());
                    string strGpString = System.IO.Path.Combine(strTempPath, strTempName + ".SDE");

                    IPropertySet      propertySet = wsNameTarget.ConnectionProperties;
                    IWorkspaceFactory wsfSDE      = new SdeWorkspaceFactoryClass();
                    if (System.IO.File.Exists(strGpString))
                    {
                        System.IO.File.Delete(strGpString);
                    }
                    wsfSDE.Create(strTempPath, strTempName, propertySet, 0);
                    return(strGpString);
                }
                catch
                {
                    return(null);
                }
            }
            else
            {
                return(wsNameTarget.PathName);
            }
        }
Пример #2
0
        /// <summary>
        /// 为SDE的Workspace生成GP字符串
        /// @remark  方法是在系统目录的根目录下生成Temp.SDE,并用此文件来生成字符串
        /// </summary>
        /// <param name="wsTarget"></param>
        /// <param name="featureDatasetName"></param>
        /// <param name="featureClassName"></param>
        /// <returns></returns>
        public static string GetGpString(IWorkspace wsTarget, string featureDatasetName, string featureClassName)
        {
            if (wsTarget == null)
                return null;

            try
            {
                string strTempPath = System.IO.Path.GetPathRoot(global::System.Environment.SystemDirectory); // System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), "SDETemp");
                string strTempName = "Temp";
                string strGpString = System.IO.Path.Combine(strTempPath, strTempName+".SDE");

                IPropertySet propertySet = wsTarget.ConnectionProperties;
                IWorkspaceFactory wsfSDE = new SdeWorkspaceFactoryClass();
                if (System.IO.File.Exists(strGpString))
                {
                    System.IO.File.Delete(strGpString);
                }
                wsfSDE.Create(strTempPath, strTempName, propertySet, 0);

                if (!string.IsNullOrEmpty(featureDatasetName))
                {
                    //strGpString = System.IO.Path.Combine(strGpString, featureDatasetName);
                    return string.Format("{0}\\{1}\\{2}", strGpString, featureDatasetName, featureClassName);
                }
                else
                {
                    return string.Format("{0}.{1}", strGpString, featureClassName);
                }
            }
            catch
            {
                return null;
            }
        }
Пример #3
0
        //--------------------------------------------------------------------------------

        public static IWorkspace GetSDEWorkspace()
        {
            try
            {
                //声明临时路径
                string path = @"d:\temp";
                // 声明临时.sde文件名称
                string sdeName = @"localhost.sde";
                string sdePath = path + "\\" + sdeName;
                // 如果已经存在了,则删除了重新创建
                if (File.Exists(sdePath))
                {
                    File.Delete(sdePath);
                }
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
                IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
                // 创建.sde文件
                IWorkspaceName workspaceName = workspaceFactory.Create(path, sdeName, ConnectSDE(true), 0);
                // 使用.sde文件,通过.sde文件获取IWorkspace,之后就可以对数据库中的数据进行操作了
                IWorkspace pWorkspace = workspaceFactory.OpenFromFile(sdePath, 0);

                return(pWorkspace);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Пример #4
0
        /// <summary>
        /// 为SDE的Workspace生成GP字符串
        /// @remark  方法是在系统目录的根目录下生成Temp.SDE,并用此文件来生成字符串
        /// </summary>
        /// <param name="wsTarget"></param>
        /// <param name="featureDatasetName"></param>
        /// <param name="featureClassName"></param>
        /// <returns></returns>
        public static string GetGpString(IWorkspace wsTarget, string featureDatasetName, string featureClassName)
        {
            if (wsTarget == null)
                return null;

            try
            {
                string strTempPath = System.IO.Path.GetPathRoot(global::System.Environment.SystemDirectory); // System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), "SDETemp");
                string strTempName = "Temp";
                string strGpString = System.IO.Path.Combine(strTempPath, strTempName+".SDE");

                IPropertySet propertySet = wsTarget.ConnectionProperties;
                IWorkspaceFactory wsfSDE = new SdeWorkspaceFactoryClass();
                if (System.IO.File.Exists(strGpString))
                {
                    System.IO.File.Delete(strGpString);
                }
                wsfSDE.Create(strTempPath, strTempName, propertySet, 0);

                if (!string.IsNullOrEmpty(featureDatasetName))
                {
                    //strGpString = System.IO.Path.Combine(strGpString, featureDatasetName);
                    return string.Format("{0}\\{1}\\{2}", strGpString, featureDatasetName, featureClassName);
                }
                else
                {
                    return string.Format("{0}.{1}", strGpString, featureClassName);
                }
            }
            catch
            {
                return null;
            }

        }
Пример #5
0
 private void btnOK_Click(object sender, EventArgs e)
 {
     try
     {
         IPropertySet connectionProperties = this.method_3(this.chkSaveUserandPsw.Checked,
                                                           this.chkSaveVersion.Checked);
         File.Delete(this.string_2);
         string            directoryName = Path.GetDirectoryName(this.string_2);
         IWorkspaceFactory factory       = new SdeWorkspaceFactoryClass();
         factory.Create(directoryName, Path.GetFileName(this.string_2), connectionProperties, 0);
         base.DialogResult = DialogResult.OK;
     }
     catch (Exception exception)
     {
         Logger.Current.Error("", exception, "");
     }
     base.Close();
 }
Пример #6
0
        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmConnSDE pFrm = new FrmConnSDE();

            pFrm.ShowDialog();
            //if (pFrm.Result == DialogResult.OK)
            if (pFrm.DialogResult == DialogResult.OK)
            {
                IPropertySet   pPS             = pFrm.ConnectionProperty;
                FrmSDEFileName pFrmSDEFileName = new FrmSDEFileName();
                if (pFrmSDEFileName.ShowDialog() == DialogResult.OK)
                {
                    if (pFrmSDEFileName.SDEFileName.Length > 0)
                    {
                        IWorkspaceFactory pWS      = new SdeWorkspaceFactoryClass();
                        string            filename = pFrmSDEFileName.SDEFileName;
                        if (!Directory.Exists(sDir))
                        {
                            Directory.CreateDirectory(sDir);
                        }
                        if (File.Exists(sDir + "\\" + filename + ".sde"))
                        {
                            File.Delete(sDir + "\\" + filename + ".sde");
                        }
                        try
                        {
                            pWS.Create(sDir, filename, pPS, 0);     //自动添加后缀.sde
                            pHashtable.Add(filename + ".sde", pPS); //filename没有添加后缀.sde
                            refreshSDEFiles();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
        }
Пример #7
0
 private void btnOK_Click(object sender, EventArgs e)
 {
     try
     {
         IPropertySet connectionProperties = this.method_3(this.chkSaveUserandPsw.Checked,
                                                           this.chkSaveVersion.Checked);
         string path = Environment.SystemDirectory.Substring(0, 2) +
                       @"\Documents and Settings\Administrator\Application Data\ESRI\ArcCatalog\";
         if (Directory.Exists(path))
         {
             string str2 = path + "Connection to " + this.txtServer.Text.Trim() + ".sde";
             str2 = this.method_1(str2);
             IWorkspaceFactory factory = new SdeWorkspaceFactoryClass();
             factory.Create(path, Path.GetFileName(str2), connectionProperties, 0);
             this.string_0     = str2;
             base.DialogResult = DialogResult.OK;
         }
     }
     catch (Exception exception)
     {
         Logger.Current.Error("", exception, "");
     }
     base.Close();
 }
Пример #8
0
        /// <summary>
        /// 给指定的WorkspaceName生成GP字符串
        /// </summary>
        /// <param name="wsNameTarget"></param>
        /// <returns></returns>
        public static string GetGpString(IWorkspaceName wsNameTarget)
        {
            if (wsNameTarget == null)
                return null;

            if (wsNameTarget.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
            {
                try
                {
                    string strTempPath = System.IO.Path.GetPathRoot(System.Environment.SystemDirectory); // System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.SystemDirectory), "SDETemp");
                    string strTempName = string.Format("Temp_{0}", wsNameTarget.GetHashCode());
                    string strGpString = System.IO.Path.Combine(strTempPath, strTempName + ".SDE");

                    IPropertySet propertySet = wsNameTarget.ConnectionProperties;
                    IWorkspaceFactory wsfSDE = new SdeWorkspaceFactoryClass();
                    if (System.IO.File.Exists(strGpString))
                    {
                        System.IO.File.Delete(strGpString);
                    }
                    wsfSDE.Create(strTempPath, strTempName, propertySet, 0);
                    return strGpString;
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return wsNameTarget.PathName;
            }
        }
Пример #9
0
        //--------------------------------------------------------------------------------
        public static IWorkspace GetSDEWorkspace()
        {
            try
            {
                //声明临时路径
                string path = @"d:\temp";
                // 声明临时.sde文件名称
                string sdeName = @"localhost.sde";
                string sdePath = path + "\\" + sdeName;
                // 如果已经存在了,则删除了重新创建
                if (File.Exists(sdePath))
                {
                    File.Delete(sdePath);
                }
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
                IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
                // 创建.sde文件
                IWorkspaceName workspaceName = workspaceFactory.Create(path, sdeName, ConnectSDE(true), 0);
                // 使用.sde文件,通过.sde文件获取IWorkspace,之后就可以对数据库中的数据进行操作了
                IWorkspace pWorkspace = workspaceFactory.OpenFromFile(sdePath, 0);

                return pWorkspace;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// creates a sde connection file outfl name of the file sv server ins instance db database ver version 
        /// owner.name method osa or dbms user and password
        /// </summary>
        /// <returns>path to connection file</returns>
        public string createSDEConnFile(string outfl, string sv, string ins, string db, string ver, string method,string user,object pass)
        {
            string path = null;
            //Console.WriteLine(outfl + "\n" + sv + "\n" + ins + "\n" + db + "\n" + ver + "\n" + method + "\n" + user + "\n" + pass);
            try
            {
                string dir = System.IO.Path.GetDirectoryName(outfl);
                string fname = System.IO.Path.GetFileName(outfl);
                if (System.IO.Path.HasExtension(fname))
                {
                    string ext = System.IO.Path.GetExtension(outfl);
                    if (ext.ToLower() != ".sde")
                    {
                        fname = fname.Replace(ext, ".sde");
                    }
                }
                else
                {
                    fname = fname + ".sde";
                }
                path = System.IO.Path.Combine(dir, fname);
                System.IO.FileInfo fInfo = new System.IO.FileInfo(path);
                if (fInfo.Exists)
                {
                    fInfo.Delete();
                }
                IWorkspaceFactory2 wksF = new SdeWorkspaceFactoryClass();
                IPropertySet propSet = new PropertySetClass();
                propSet.SetProperty("SERVER", sv);
                propSet.SetProperty("INSTANCE", ins);
                propSet.SetProperty("DATABASE", db);
                propSet.SetProperty("VERSION", ver);
                propSet.SetProperty("AUTHENTICATION_MODE", method.ToUpper());
                if(!(user == null || user=="" || pass==null || pass.ToString()=="") & method.ToLower()=="dbms")
                {
                    propSet.SetProperty("USER",user);
                    propSet.SetProperty("PASSWORD",pass);
                    //Console.WriteLine(pass.ToString());
                }
                wksF.Create(dir, fname, propSet, 0);
            }
            catch(Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
                path = null;

            }
            return path;
        }