cmd = New SqlCommand("SELECT ID, Name, Dept from table", con)
dr = cmd.ExecuteReader
Dim sb as new StringBuilder
While dr.Read()
''Let's read line by line and Append it to our StringBuilder
sb.AppendLine(
String.Format("{0} | {1} | {2} | {3}",
dr.item("ID"), dr.item("Name"), dr.item("Dept") ))
End While
''Now that we have all data in our StringBuilder, lets put into our file
File.WriteAllLines("D:\test.txt", sb.ToString())
P.S. from a C# guy ... please verify if the code it right :) (C# code below)
using (SqlConnection con = new SqlConnection("my Connection String"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT ID, Name, Dept from table";
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (dr.Read())
// Let's read line by line and Append it to our StringBuilder
sb.AppendLine(
String.Format("{0} | {1} | {2} | {3}",
dr["ID"], dr["Name"], dr["Dept"]));
// Now that we have all data in our StringBuilder, lets put into our file
File.WriteAllLines("D:\test.txt", sb.ToString());
}
}