讨论两个关键问题1、如何连接数据库 2、如何创建.sde文件。
1、连接sde数据库
在10.1中数据库的连接默认为直连,但是对于以前的代码没有任何影响,如下面的代码(如果你用的是sde10的32位数据库,连接方法和以前一样,直接用代码连接;如果你用的是64位的数据库,请将32位的数据库客户端放到安装Engine的bin目录下):
publicIWorkspaceGetSDEWorkspace(String_pServerIP,String_pInstance,String_pDatabase,String_pUser,String_pPassword,String_pVersion)
{
IWorkspacepWkspace=null;
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2workspaceFactory=null;
ESRI.ArcGIS.esriSystem.IPropertySetpPropertySet=newESRI.ArcGIS.esriSystem.PropertySetClass();
pPropertySet.SetProperty("SERVER",_pServerIP);
pPropertySet.SetProperty("INSTANCE",_pInstance);
pPropertySet.SetProperty("DATABASE",_pDatabase);
pPropertySet.SetProperty("USER",_pUser);
pPropertySet.SetProperty("PASSWORD",_pPassword);
pPropertySet.SetProperty("VERSION",_pVersion);
workspaceFactory=(ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)newESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
try
{
pWkspace=workspaceFactory.Open(pPropertySet,0);
}
catch(ExceptionEX)
{
//MessageBox.Show(EX.ToString());
}
returnpWkspace;
}
只是在传入参数的时候注意_pInstance一般的格式为:sde:postgresql:localhost等。
2、创建.sde文件
要创建.sde文件需要用到IWorkspaceFactory的Create方法,该方法要有数据库连接的参数信息,在这里我用的是Postgresql数据库,代码如下:
//创建.sde文件并连接数据库
IWorkspaceSDEConnect(stringFolderPath,stringFileName,String_pServerIP,String_pInstance,String_pUser,String_pPassword,String_pVersion,string_pDatabase,bool_pTrue)
{
stringpSDEPath=System.IO.Path.Combine(FolderPath,FileName);
IWorkspaceFactorypWSFactory=newSdeWorkspaceFactoryClass();
IWorkspacepWorkspace=null;
if(!File.Exists(pSDEPath))
{
IWorkspaceNamepWSName=pWSFactory.Create(FolderPath,FileName,GetPropertySet(_pServerIP,_pInstance,_pUser,_pPassword,_pVersion,_pDatabase,_pTrue),0);
}
pWorkspace=pWSFactory.OpenFromFile(pSDEPath,0);
returnpWorkspace;
}
//最后一个参数表示直连还是服务连接
publicIPropertySetGetPropertySet(String_pServerIP,String_pInstance,String_pUser,String_pPassword,String_pVersion,string_pDatabase,bool_pTrue)
{
ESRI.ArcGIS.esriSystem.IPropertySetpPropertySet=newESRI.ArcGIS.esriSystem.PropertySetClass();
pPropertySet.SetProperty("SERVER",_pServerIP);
pPropertySet.SetProperty("USER",_pUser);
pPropertySet.SetProperty("PASSWORD",_pPassword);
pPropertySet.SetProperty("VERSION",_pVersion);
if(_pTrue)
{
if(_pInstance.Contains(":"))
{
pPropertySet.SetProperty("INSTANCE",_pInstance);
pPropertySet.SetProperty("database",_pDatabase);
}
else
{
MessageBox.Show("直连字符串不对!");
}
}
else
{
pPropertySet.SetProperty("INSTANCE",_pInstance);
}
returnpPropertySet;
}
直连
IWorkspacepSDE=SDEConnect("C:\\","sde.sde","localhost","sde:postgresql:localhost","sde","sde","sde.DEFAULT","sde",true);
服务连接:
IWorkspacepSDE=SDEConnect("C:\\","service.sde","localhost","5151","sde","sde","sde.DEFAULT","sde",false);