1

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

现在,我正在使用companySAP 的 B1 SDK 中的对象,我不想将连接详细信息保留在硬编码中。由于它不仅仅是一个连接字符串(据我所知),所以我想知道是否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();
4

1 回答 1

2

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>
于 2015-12-23T21:31:29.740 回答