SELECT INTO Statements
You can use the SELECT INTO statement to export formatted data to a file.
Data Export with an SQL Query
The following query exports data into a file formatted in comma-separated values (CSV):
SELECT From, Subject INTO [csv://[INBOX].txt] FROM [[INBOX]] WHERE Subject = 'Test'You can specify other formats in the file URI. The possible delimiters are tab, semicolon, and comma with the default being a comma. The following example exports tab-separated values:
SELECT From, Subject INTO [csv://[INBOX].txt;delimiter=tab] FROM [[INBOX]] WHERE Subject = 'Test'
C#
String connectionString = "Port=993;Server=outlook.office365.com;Password=password;User=user;Protocol=IMAP;SMTP Port=587;SMTP Server=smtp.office365.com;"; using (EmailConnection connection = new EmailConnection(connectionString)) { EmailCommand cmd = new EmailCommand("SELECT From, Subject INTO [csv://[INBOX].txt] FROM [[INBOX]] WHERE Subject = 'Test' ", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "Port=993;Server=outlook.office365.com;Password=password;User=user;Protocol=IMAP;SMTP Port=587;SMTP Server=smtp.office365.com;" Using connection As New EmailConnection(connectionString) Dim cmd As New EmailCommand("SELECT From, Subject INTO [csv://[INBOX].txt] FROM [[INBOX]] WHERE Subject = 'Test'", connection) Dim rowsAffected As Integer = cmd.ExecuteNonQuery() End UsingYou can specify other file formats in the URI. The following example exports tab-separated values:
C#
String connectionString = "Port=993;Server=outlook.office365.com;Password=password;User=user;Protocol=IMAP;SMTP Port=587;SMTP Server=smtp.office365.com;"; using (EmailConnection connection = new EmailConnection(connectionString)) { EmailCommand cmd = new EmailCommand("SELECT * INTO [[INBOX]] IN [csv://filename=c:/[INBOX].csv;delimiter=tab] FROM [[INBOX]] WHERE Subject = 'Test'", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "Port=993;Server=outlook.office365.com;Password=password;User=user;Protocol=IMAP;SMTP Port=587;SMTP Server=smtp.office365.com;" Using connection As New EmailConnection(connectionString) Dim cmd As New EmailCommand("SELECT * INTO [[INBOX]] IN [csv://filename=c:/[INBOX].csv;delimiter=tab] FROM [[INBOX]] WHERE Subject = 'Test'", connection) Dim rowsAffected As Integer = cmd.ExecuteNonQuery() End Using