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 RowId, LastName INTO [csv://Sheet.txt] FROM [Sheet] WHERE FirstName = 'Bob'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 RowId, LastName INTO [csv://Sheet.txt;delimiter=tab] FROM [Sheet] WHERE FirstName = 'Bob'
C#
String connectionString = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;"; using (ExcelConnection connection = new ExcelConnection(connectionString)) { ExcelCommand cmd = new ExcelCommand("SELECT RowId, LastName INTO [csv://Sheet.txt] FROM [Sheet] WHERE FirstName = 'Bob' ", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;" Using connection As New ExcelConnection(connectionString) Dim cmd As New ExcelCommand("SELECT RowId, LastName INTO [csv://Sheet.txt] FROM [Sheet] WHERE FirstName = 'Bob'", 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 = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;"; using (ExcelConnection connection = new ExcelConnection(connectionString)) { ExcelCommand cmd = new ExcelCommand("SELECT * INTO [Sheet] IN [csv://filename=c:/Sheet.csv;delimiter=tab] FROM [Sheet] WHERE FirstName <> 'Bob'", connection); int rowsAffected = cmd.ExecuteNonQuery(); }
VB.NET
Dim connectionString As [String] = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;" Using connection As New ExcelConnection(connectionString) Dim cmd As New ExcelCommand("SELECT * INTO [Sheet] IN [csv://filename=c:/Sheet.csv;delimiter=tab] FROM [Sheet] WHERE FirstName <> 'Bob'", connection) Dim rowsAffected As Integer = cmd.ExecuteNonQuery() End Using