Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Have the undernoted code segment which is working ok from a form.

Need to apply it from a table with the undernoted fields in sql server

Table: Mail.dbo

name        email               memo
----------------------------------------
eva         aaa@gmail.com     trialeva
sam         bbb@gmail.com     trialsam
jon         ccc@gmail.com     trialjon
amy         ddd@gmail.com     trialamy


C#
EmailSubj = "Trial Message";
EmailMsg = Convert.ToString(txt_Trial_memo.Text);

string[] emails = new string[] { txt_Email.Text, txt_Zemail.Text, txt_Bemail.Text };
foreach (var email in emails)
{
    if (!string.IsNullOrWhiteSpace(email))
        SendEmail(email, EmailSubj, EmailMsg);
}


Please assist in building the loop

or offer any web refereneces to assist.


Thanks

What I have tried:

Reviewed codes on the internet and earlier codes in another language
Posted
Updated 3-Oct-16 4:34am
v2
Comments
Sinisa Hajnal 3-Oct-16 7:01am    
SO, you need to get the data (emails) from the database and use that? In C#? And you cannot be bothered to find decent example of data retrieval of which there are thousands? 1 vote.
Member 12770648 3-Oct-16 12:36pm    
please its not about the connection. I am new to c# asp.net I just moved from another language.

Its about the looping to send the message

Member 12770648 3-Oct-16 12:46pm    
You have misled the request.

All, I wanted was how to get the filtered records mailed and not about the connection please.

I have searched for such an example but to no avail.

ZurdoDev 3-Oct-16 10:25am    
I agree with Sinisa. It sounds like you are asking how to get data from a database. That is standard and simple but depends on your data structure so we can't tell you how to to it anyway. It would help if you could clarify what you are asking.
[no name] 3-Oct-16 10:26am    
"Reviewed codes on the internet", yeah sure. I find it very hard to believe, no impossible to believe, that you searched the internet for how to connect to and query a SQL Server database and found absolutely nothing?

1 solution

So what you are asking is really how to connect to sql server using c# and access the data. I know nothing about your project/how you intend to access the data. There are many different ways to access sql server (normal ado.net, entity framework, nhibernate...etc.) so I'm going to assume you are using entity framework as that is the easiest way to answer this question.

What you need to do is first, research what it is you are trying to do as this is an extremely basic thing to do in C#. Accessing data, to me, is one step above Hello World and is something you should be familiar with already.

So to get the data you would do something like.

C#
// I added IsSent to your Mail table as a way to filter out mail already sent. You could also just delete these items from your table after mail has been sent.
var mailToSend = context.Mail.Where(m=> !m.IsSent);

foreach(var mail in mailToSend)
{
     SendEmail(mail.email, "You don't have a subject column, static text/passed in variable here", mail.memo);
}


the mailToSend variable is a collection of items from your database. You need to decide how you intend on accessing your data. For more information on using entity framework (and general C# to sql connections) see below links:

connect to sql server c - Google Search[^]

use entity framework C# - Google Search[^]

Getting Started (Entity Framework)[^]

Getting Started with Entity Framework 6 Code First using MVC 5 | The ASP.NET Site[^]

An Introduction to Entity Framework for Absolute Beginners[^]

Entity Framework Tutorial[^]

UPDATE:

In the off change you want to use Sql Server DB Mail...here are some links. You need to evaluate what it is you are trying to do and see which option best fits your needs.

sp_send_dbmail (Transact-SQL)[^]

Configure Database Mail[^]

SQL Database Mail – Send Emails from SQL Server – Microsoft SQL Server on Windows Azure Virtual Machines[^]
 
Share this answer
 
v2
Comments
#realJSOP 3-Oct-16 10:36am    
I think he wants to send email from the database instead of from the form. Of course, it's hard to say due to his mangling of the English language.
David_Wimbley 3-Oct-16 10:52am    
Added some links to DB mail in sql server, don't know if thats a good route but added it anyway and OP can decide what best fits their needs.
#realJSOP 3-Oct-16 11:12am    
Sending email from the database itself is bad practice. You can spoof the entire message. I was working at one place where the DBA was pranking a bunch of female employees and setting the from field to other people's email addresses. In today's overly sensitive office environments, that could get someone fired, or worse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900