如何使用存储过程编辑我们从数据库中获取的值?

时间:2012-09-12 14:36:58

标签: c# sql-server winforms

我有点卡在这里。我想编辑使用存储过程从数据库中获取的某些字段。我正在尝试发送电子邮件信息,我已将所有内容存储在“电子邮件”表中。一旦我从“电子邮件”表中获取信息,我想编辑从数据库获取的正文信息,然后使用我创建的电子邮件服务将其发送给用户。以下是我到目前为止所做的事情。我创建了一个Hash表,并在那里存储了值:

MembershipUser qiUser = ((GenericMembershipProvider)Membership.Provider).GetUser(objSubAccount.CreatedByUser, false);

            Hashtable mailKeywords = new Hashtable();
            mailKeywords.Add("[AccountNumber]", objSubAccount.AccountNumber);
            mailKeywords.Add("[AccountSubmissionDate]", objSubAccount.SubmittedOn.ToString());
            mailKeywords.Add("[FBONameTitle]", objSubAccount.FBO1FirstName.Trim().Length == 0 && objSubAccount.FBO1MiddleInitial.Trim().Length == 0 && objSubAccount.FBO1LastName.Trim().Length == 0
            ? objSubAccount.FBO1AccountTitling : objSubAccount.FBO1FirstName + " " + objSubAccount.FBO1MiddleInitial + " " + objSubAccount.FBO1LastName);
            mailKeywords.Add("[QIName]", objSubAccount.CompanyName);
            mailKeywords.Add("[QIUserName]", qiUser.UserName);
            mailKeywords.Add("[QIUserEmailAddress]", qiUser.Email);
            mailKeywords.Add("[WireEmailNotification1]", objSubAccount.WireEmailNotification1);
            mailKeywords.Add("[WireEmailNotification2]", objSubAccount.WireEmailNotification2);
            mailKeywords.Add("[WireEmailNotification3]", objSubAccount.WireEmailNotification3);

现在我已将这些值分配给EmailContent类的不同属性:

EmailContent objEmailContent = new EmailContent();
            objEmailContent = EmailContent.GetEmailContentType(EmailContentType.WireroomGroup);
            objEmailContent.To = BancorpConfig.WireroomReceiverEmail;
            objEmailContent.Sender = BancorpConfig.WireRoomSenderEmail;
            objEmailContent.Subject = EmailContent.ReplacePlaceHolder(objEmailContent.Subject, mailKeywords);
            objEmailContent.Body = EmailContent.ReplacePlaceHolder(objEmailContent.Body, mailKeywords);
            objEmailContent.From = BancorpConfig.WireRoomSenderEmail;
            objEmailContent.MailDate = System.DateTime.Now;

现在我想要做的是当我在objEmailContent.Body中获取值时,我想要编辑它,好像它们在WireEmailNotificaion1列中没有值,然后应删除括号“()”。如果它们是WireEmailNotification1,则该值应显示为(aa@gmail.com)否则也不应显示删除括号的值。

电子邮件正文如下:

New QI Sub Account Application Submitted<br />
Account Number: ([AccountNumber])<br />
FBO Name/Account Title: ([FBONameTitle]) <br />
Application Submitted Date: ([AccountSubmissionDate])<br />
QI Name: ([QIName])<br />QI User Name: ([QIUserName])<br />
QI User Email Address: ([QIUserEmailAddress])<br />
Wire Email Notification 1: ([WireEmailNotification1])<br/>
Wire Email Notification 2: ([WireEmailNotification2])<br/>
Wire Email Notification 3: ([WireEmailNotification3])<br/>
Please add account to the Caller ID’s on Schedule A of the Wire Agreement.

如果在WireEmailNotification1,WireEmailNotification2,WireEmailNotification3中没有值,我将如何从主体中删除括号?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你有一个模板,你想从数据库中取一些值,并把它放在消息体中。 当我有这个要求时,我实际创建了HTML模板和你要放置动态内容的地方,定义了一些标签(就像你已经完成的那样),然后找到每个标签并用相应的字段替换它。

其他选项(不是很好),那么既然你拥有数据库中的所有东西,你就可以编写一个程序给你最后的消息体(很容易在程序中映射字段)。您可以使用Exec'select * from'来实现此目的。

答案 1 :(得分:0)

这对你有用吗?:

  • 从模板中删除括号。
  • 使用三元运算符仅在需要时添加它们

示例:

mailKeywords.Add("[WireEmailNotification1]", objSubAccount.WireEmailNotification1 != "" ? "(" + objSubAccount.WireEmailNotification1 + ")" : "" );

这与说(通知括号)相同:

        if (objSubAccount.WireEmailNotification1 != "")
        {
            mailKeywords.Add("[WireEmailNotification1]", "(" + objSubAccount.WireEmailNotification1+ ")" ); 
        }
        else
        {
            mailKeywords.Add("[WireEmailNotification1]","");
        }