
In this example, we are connecting to the local SQL Server and the AdventureWorks2016CTP3 database: Specify the connection the SQL Server and Database. In SSDT, go to File>New Project and select Report Server Project Wizard:Ī Welcome wizard will be displayed. If the presentation is important, Reporting Services is the best option. Reporting Services allows you to save reports in PDF, Excel, XML, MHTML, Word, CSV, PowerPoint and TIFF format.
#Output sql 2017 json query to text file code
This code is used when an exception error is generate by the package.įinally, we will chose the file and SQL Server connection with Reader.Close and myFile.Close:ĭts.TaskResult = (int)ScriptResults.Failure Īnother option is to create a Report in SQL Server Reporting Services and save it as CSV. MessageBox.Show will show the error and Dts.TaskResult will show a failure red color if it fails. "Catch" is used to catch the exception errors. SqlDataReader reader = command.ExecuteReader() SqlCommand command = new SqlCommand(query, connection)

StreamWriter myFile = new (SqlConnection connection = new SqlConnection(connectionSql)) String connectionSql = "Server=(local) Database=AdventureWorks2016CTP3 Integrated Security=true"

In the Script in the region where is says add your code here, add the following code: Sysem.IO is used to write information to a file (in this scenario a txt file) and Data.SqlClient is used to connect to SQL Server: In #region Namespaces add System.IO and Data.SqlClient. To do this, in SSDT, drag and drop the Script Task:ĭouble click Script Task and press the Edit Script button: This option is very useful if you are writing code and you need to integrate this task to the code. In this example, we will use the Script task included in SSDT. You could also perform a similar task using Visual Basic. You can export from SQL Server to a text file using C#. The package will generate a text file with the CSV format. The next steps are the same than in SSMS when we call the Import/Export wizard, but at the end, you do not have the option to run immediately. In Solution Explorer, right click SSIS Packages and select SSIS Import and Export Wizard: Go to SSDT and then go to File>New Project and select Integration Services Project: This method is similar to the Import/Export Wizard in SSMS, because SSMS calls SSIS to import and export Data. The file created will be similar to this one: Select Run immediately to export the data immediately: A message specifying that the statement is valid should be displayed: Specify the query of the file myquery.sql (used in other methods) and press parse to verify that the query is OK. Select the option "Write a query to specify the data to transfer": Once that you have the file name and path, press next: In this example, the flat file name will be exportwizard.txt: In Destination, select Flat File Destination and press browse to specify the file name and path: Specify the Server name and the connection information if necessary: Select the Microsoft OLE DB Provider as the Data Source: We will export from SQL Server to a Flat file. You will open the SQL Server Import and Export wizard: There is an option to import or export data. In SSMS, when you right click a database. Invoke-Sqlcmd will call the script myquery.sql created at the beginning of this article and store the results in a file named powershelloutput.txt. Invoke-Sqlcmd -InputFile "C:\sql\myquery.sql" | Out-File -filePath "C:\sql\powershelloutput.txt" We can export the SQL Server query results to a txt file by executing the following cmdlets: PowerShell is an extremely popular command line shell to automate tasks. The command used the myquery.sql file created before and the output is store to the file myoutput.txt: Sqlcmd -i c:\sql\myquery.sql -o c:\sql\myoutput.txt This option is useful when you are using batch files to automate tasks. You can save the results in a file from here. SQLCMD is the SQL Server Command Line utility. We will call the results in a file named Results.rpt: An option to specify the name and path will be displayed. Go to Tools>Options:Ĭreate a query and execute the query. If you want to save the results in a txt file, you can do this in SSMS. The result displayed in SQL Server Management Studio (SSMS) is the following: We will use the following script, named myscript.sql: In the first option, we will configure SSMS to display the query results to a txt file.


Let's look at each of the ways we can export the results of a query. You need a SQL Server Installed with SSIS and SQL Server Data Tools (SSDT).
