📄 sdeconn.cs
字号:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Xml;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
namespace EngineData
{
/// <summary>
/// 名 称:SdeConn
/// 功能描述: SDE连接属性设置
/// 作 者:胡振彪
/// 创建日期:2005.4.1
/// </summary>
public class SdeConnProp
{
public string Server;
public string Instance;
public string DataBase;
public string user;
public string password; //真实密码
public string version;
public SdeConnProp()
{
}
public void SetConn(string Server,string Instance,string DataBase,string user,string password,string version)
{
this.Server = Server;
this.Instance = Instance;
this.DataBase = DataBase;
this.user = user;
this.password = password;
this.version = version;
}
}
public class SdeConn
{
private SymmetricAlgorithm mobjCryptoService;
private string Server;
private string Instance;
private string DataBase;
private string user;
private string password; //加密后的密码
private string version;
public SdeConnProp ConnProp;
public SdeConn()
{
//
// TODO: 在此处添加构造函数逻辑
//
mobjCryptoService = new DESCryptoServiceProvider();
ConnProp = new SdeConnProp();
}
//设置SDE连接属性
public void SetConn(string Server,string Instance,string DataBase,string user,string password,string version)
{
this.Server = Server;
this.Instance = Instance;
this.DataBase = DataBase;
this.user = user;
this.password = Encrypting(password,"SdeConn");
this.version = version;
ConnProp.SetConn(Server,Instance,DataBase,user,password,version);
}
private byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
// key sizes are in bits
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
// convert the secret key to byte array
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
private string Encrypting(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
// create a MemoryStream so that the process can be done without I/O files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
// get the output and trim the '\0' bytes
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
}
private string Decrypting(string Source, string Key)
{
// convert from Base64 to binary
byte[] bytIn = System.Convert.FromBase64String(Source);
// create a MemoryStream with the input
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create a Decryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
// create Crypto Stream that transforms a stream using the decryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
// read out the result from the Crypto Stream
System.IO.StreamReader sr = new System.IO.StreamReader( cs );
return sr.ReadToEnd();
}
//将SDE配置保存到文件
public bool WriteToSDE(string fileName)
{
try
{
XmlTextWriter tw=new XmlTextWriter(fileName,null);
tw.Formatting=Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("SdeConn");
tw.WriteElementString("Server",this.Server);
tw.WriteElementString("Instance",this.Instance);
tw.WriteElementString("DataBase",this.DataBase);
tw.WriteElementString("user",this.user);
tw.WriteElementString("password",this.password);
tw.WriteElementString("version",this.version);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
return true;
}
catch
{
return false;
}
}
//从SDE文件读取连接配置信息
public bool ReadFromSDE(string fileName)
{
try
{
XmlDocument xml = new XmlDocument();
xml.Load(fileName);
XmlNode root = xml.DocumentElement;
this.Server = root.ChildNodes[0].InnerText;
this.Instance = root.ChildNodes[1].InnerText;
this.DataBase = root.ChildNodes[2].InnerText;
this.user = root.ChildNodes[3].InnerText;
this.password = root.ChildNodes[4].InnerText;
this.version = root.ChildNodes[5].InnerText;
ConnProp.SetConn(Server,Instance,DataBase,user,this.Decrypting(password,"SdeConn"),version);
return true;
}
catch
{
return false;
}
}
//返回一个工作空间
public IWorkspace GetCorkSpace()
{
try
{
IWorkspace ws = null;
IPropertySet pPropSet = new PropertySetClass();
IWorkspaceFactory pSdeFact = new SdeWorkspaceFactoryClass();
pPropSet.SetProperty("Server",this.Server);
pPropSet.SetProperty("Instance",this.Instance);
pPropSet.SetProperty("Database",this.DataBase);
pPropSet.SetProperty("user",this.user);
pPropSet.SetProperty("password",this.Decrypting(this.password,"SdeConn"));
pPropSet.SetProperty("version",this.version);
ws = pSdeFact.Open(pPropSet,0);
return ws;
}
catch
{
return null;
}
}
public IVersionedWorkspace GetVersionedWorkspace()
{
try
{
IWorkspace ws = null;
IPropertySet pPropSet = new PropertySetClass();
IWorkspaceFactory pSdeFact = new SdeWorkspaceFactoryClass();
pPropSet.SetProperty("Server",this.Server);
pPropSet.SetProperty("Instance",this.Instance);
pPropSet.SetProperty("Database",this.DataBase);
pPropSet.SetProperty("user",this.user);
pPropSet.SetProperty("password",this.Decrypting(this.password,"SdeConn"));
pPropSet.SetProperty("version",this.version);
ws = pSdeFact.Open(pPropSet,0);
IVersionedWorkspace pVersionWorkspace = (IVersionedWorkspace)ws;
return pVersionWorkspace;
}
catch
{
return null;
}
}
public void Close()
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -