使用 app.config 文件连接到 SAP 公司对象

Using the app.config file to connect to a SAP company object

我最近学习了如何使用 app.config 为我的 .net 项目保存连接字符串。如果我更改了连接字符串中的某些内容并且项目已经发布,我发现这非常有帮助。

现在,我正在使用来自 SAP 的 B1 SDK 的 company 对象,我不想在硬编码中保留连接细节。因为它不仅仅是一个连接字符串(据我所知),所以我想知道 app.config 是否也可以存储 company 对象的连接详细信息。

这就是我通常与 company 对象建立联系的方式:

        oCompany = new SAPbobsCOM.Company();

        oCompany.Server = "servername";
        oCompany.language = SAPbobsCOM.BoSuppLangs.ln_Spanish_La;
        oCompany.DbServerType = BoDataServerTypes.dst_MSSQL2008;
        oCompany.LicenseServer = "localhost:30000";
        oCompany.UseTrusted = false;
        oCompany.DbUserName = "dbusername";
        oCompany.DbPassword = "dbpassword";
        oCompany.CompanyDB = "companyDB";
        oCompany.UserName = "cusername";
        oCompany.Password = "cpassword";

        lRetCode = oCompany.Connect();

Karol 的评论很中肯,这是我们通常的设置方式。唯一的补充意见是,根据您的部署环境,您可能希望加密 app.config(密码)中的敏感凭据。

// In code
Company company = new Company
{
    Server = ConfigurationManager.AppSettings["DevServer"],
    DbUserName = ConfigurationManager.AppSettings["DevDBUser"],
    DbPassword = ConfigurationManager.AppSettings["DevDBPassword"],
    CompanyDB = ConfigurationManager.AppSettings["DevDatabase"],
    UserName = ConfigurationManager.AppSettings["DevSBOUser"],
    Password = ConfigurationManager.AppSettings["DevSBOPassword"],
    language = BoSuppLangs.ln_English
};

// In your app.config
<appSettings>
   <add key="DevServer" value="DEV-SAP-SRV"/>
   <add key="DevDBUser" value="sa"/>
   <add key="DevDBPassword" value="sapassword"/>
   <add key="DevSBOUser" value="manager"/>
   <add key="DevSBOPassword" value="1234"/>
   <add key="DevDatabase" value="SBO_COMPANY_NAME"/>
</appSettings>