ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export data from SQL Server to Text and CSV file using C# and VB.Net in Windows Application<p>Hey <a class="username" rel="RPA"> RPA</a>,</p> <p>Please refer below smaple.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">public DataTable dt = new DataTable(); public Form1() { InitializeComponent(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Rustam Pulodov&#34;, &#34;Tajikistan&#34;); dt.Rows.Add(2, &#34;Sadriddin Sharipov&#34;, &#34;USA&#34;); dt.Rows.Add(3, &#34;Firuzjon Soliev&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Asrori Yatim&#34;, &#34;Russia&#34;); } private void btncsv_Click(object sender, EventArgs e) { string csv = string.Empty; foreach (DataColumn column in dt.Columns) { csv += column.ColumnName + &#39;,&#39;; } csv += &#34;\r\n&#34;; foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { csv += row[column.ColumnName].ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#39;,&#39;; } csv += &#34;\r\n&#34;; } string folderPath = @&#34;D:\File\&#34;; File.WriteAllText(folderPath + &#34;Export.csv&#34;, csv); } private void btntxt_Click(object sender, EventArgs e) { string txt = string.Empty; foreach (DataColumn column in dt.Columns) { txt += column.ColumnName + &#39; &#39;; } txt += &#34;\r\n&#34;; foreach (DataRow row in dt.Rows) { int i = 0; foreach (DataColumn column in dt.Columns) { if (i == 0) { txt += row[column.ColumnName].ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#39; &#39;; } else { txt += row[column.ColumnName].ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#39;;&#39;; } i++; } txt = txt.Remove(txt.Length - 1, 1); txt += &#34;\r\n&#34;; } string folderPath = @&#34;D:\File\&#34;; File.WriteAllText(folderPath + &#34;Result.txt&#34;, txt); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Public dt As DataTable = New DataTable() Public Sub New() InitializeComponent() dt.Columns.AddRange(New DataColumn(2) {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Rustam Pulodov&#34;, &#34;Tajikistan&#34;) dt.Rows.Add(2, &#34;Sadriddin Sharipov&#34;, &#34;USA&#34;) dt.Rows.Add(3, &#34;Firuzjon Soliev&#34;, &#34;France&#34;) dt.Rows.Add(4, &#34;Asrori Yatim&#34;, &#34;Russia&#34;) End Sub Private Sub btncsv_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click Dim i As Integer Dim csv As String = String.Empty For Each column As DataColumn In dt.Columns csv += column.ColumnName + &#34;,&#34;c Next csv += vbCrLf For Each row As DataRow In dt.Rows For Each column As DataColumn In dt.Columns csv += row(column.ColumnName).ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#34;,&#34;c Next csv += vbCrLf Next Dim folderPath As String = &#34;D:\File\&#34; File.WriteAllText(folderPath &amp; &#34;Export.csv&#34;, csv) End Sub Private Sub btntxt_Click(ByVal sender As Object, ByVal e As EventArgs) Dim txt As String = String.Empty For Each column As DataColumn In dt.Columns txt += column.ColumnName + &#34; &#34;c Next txt += vbCrLf For Each row As DataRow In dt.Rows Dim i As Integer = 0 For Each column As DataColumn In dt.Columns If i = 0 Then txt += row(column.ColumnName).ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#34; &#34;c Else txt += row(column.ColumnName).ToString().Replace(&#34;,&#34;, &#34;;&#34;) + &#34;;&#34;c End If i += 1 Next txt = txt.Remove(txt.Length - 1, 1) txt += vbCrLf Next Dim folderPath As String = &#34;D:\File\&#34; File.WriteAllText(folderPath &amp; &#34;Result.txt&#34;, txt) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><strong>TextFile</strong></p> <p><strong><img src="https://imgur.com/k2rg1ck.jpg" alt="" width="283" height="166" /></strong></p> <p><strong>CSV File</strong></p> <p><strong><img src="https://i.imgur.com/iOVuApa.jpg" alt="" width="265" height="238" /></strong></p>https://www.aspforums.net:443/Threads/198513/Export-data-from-SQL-Server-to-Text-and-CSV-file-using-C-and-VBNet-in-Windows-Application/https://www.aspforums.net:443/Threads/198513/Export-data-from-SQL-Server-to-Text-and-CSV-file-using-C-and-VBNet-in-Windows-Application/Wed, 28 Nov 2018 03:13:01 GMT