hello guys, i have the following question/problem:

-what i need to do is to get some data from sqlserver and send it to a dataset(no problem there)
after i get the dataset filled i need to send the columns and rows to a textfile but i have to separate the columns on the dataset with this "|"

Exmaple:
field1|field2|field3

can someone help me on this?, i searched already but no solution on this, any help will be appreciated. thanks

its simple..


first Import followings:

Imports System.Data.SqlClient
Imports System.IO

Now Suppose u have a button2 to perform the operation u said. . so just write following code to button's click event

Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        conn = New SqlConnection("Data Source=server;Initial Catalog=table;Persist Security Info=True;User ID=user;Password=pass")

        Dim strQ As String = String.Empty

        strQ = "SELECT * FROM table"
        cmd = New SqlCommand(strQ, conn)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "Table")

        Dim strRow As New System.Text.StringBuilder

        Dim i, j As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1

            For j = 0 To ds.Tables(0).Columns.Count - 1
                strRow.Append(ds.Tables(0).Rows(i)(j).ToString())
                strRow.Append("|")
            Next
            strRow.Append(vbCrLf)

        Next

        File.WriteAllText("test.txt", strRow.ToString())

thanks for your help, i did something like that i got it to work. but i`ll keep this code for future references. thanks again


its simple..


first Import followings:

Imports System.Data.SqlClient
Imports System.IO

Now Suppose u have a button2 to perform the operation u said. . so just write following code to button's click event

Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        conn = New SqlConnection("Data Source=server;Initial Catalog=table;Persist Security Info=True;User ID=user;Password=pass")

        Dim strQ As String = String.Empty

        strQ = "SELECT * FROM table"
        cmd = New SqlCommand(strQ, conn)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "Table")

        Dim strRow As New System.Text.StringBuilder

        Dim i, j As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1

            For j = 0 To ds.Tables(0).Columns.Count - 1
                strRow.Append(ds.Tables(0).Rows(i)(j).ToString())
                strRow.Append("|")
            Next
            strRow.Append(vbCrLf)

        Next

        File.WriteAllText("test.txt", strRow.ToString())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.