export database records to tab delimited file. asp.net/vb

Last post 10-19-2005 9:42 PM by devnull. 6 replies.

Sort Posts:

  • export database records to tab delimited file. asp.net/vb

    10-19-2005, 3:21 AM
    • Member
      280 point Member
    • nivram
    • Member since 06-16-2005, 1:43 AM
    • Posts 56
    hello everyone.

    I have a small project that need to export a database record stored in mssql server 2000 to a tab delimited text file. I see some example in the web but not exactly what i want.

    Here is the explanation: (I still dont have a sample program)

    There should be a link for download record. When you click this it will prompt you to save it to your computer in a txt format. The user will have option of what filename he/she would choose. Then this filename will be use to sotore the actual data coming from the database. I need to do this because this tab-delimited textfile will then use by our user to upload to another system.

    Please provide an example on how to do this. Additional tutorial site are also welcome.

    thanks

    marvz
  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 4:22 AM
    • Participant
      980 point Participant
    • devnull
    • Member since 10-18-2005, 9:16 PM
    • New Zealand
    • Posts 195
    StringBuilder output = new StringBuilder(); // will hold the record
    SqlDataReader dr = sqlCmd.ExecuteReader();
    DataTable dt = dr.GetSchemaTable(); // get column names
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    output.Append('"');
    output.Append(dt.Rows[i ]["
    ColumnName"].ToString().Trim());
    output.Append('"
    ');
    output.Append(',');
    }

    while (dr.Read())
    {
    output.Append("\r\n");
    for (int i=0; i<dr.FieldCount; i++)
    {
    output.Append('"');
    output.Append(dr[i ].ToString());
    output.Append('"
    ');
    output.Append("\t"); // tab-delimited
    }
    }

    that should do the trick
  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 4:53 AM
    • Member
      5 point Member
    • altunbay
    • Member since 10-19-2005, 8:37 AM
    • Posts 1
    //You should get data into datatable
    DataTable dt=new DataTable;
    // Write data in datatable to a text file
    string path = Server.MapPath("")+"\\files\\temp.txt";
    StreamWriter sw = new StreamWriter(path,false);
     foreach(DataRow dr in dt.Rows)
    {
        string line="";
        foreach(DataColumn dc in dt.Columns)
        {
           line+=dr[dc.Name].ToString()+"\t";
        }
        sw.WriteLine(line);
    }
    sw.Close();

    //Read the file to a byte array
    Stream s = File.OpenRead(path);
    Byte[] buffer = new Byte[s.Length];
    s.Read(buffer, 0, (Int32) s.Length);
    //Clear the headers and write the byte arras as file to buffer
    Response.ClearHeaders();
    Response.ClearContent();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
    Response.BinaryWrite(buffer);
    Response.End();

  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 6:02 AM
    • Member
      42 point Member
    • Muller2
    • Member since 01-07-2005, 10:35 AM
    • Posts 10
    Does anyone have this code in c#?It would be incredible handy!

    Thanks

    Muller2

    SILVERLIGHT FORUMS: HAVE A QUESTION? ASK IT HERE!
  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 7:30 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs
    Both posts that have code in them are in C#.

    NC...
  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 8:59 PM
    • Member
      280 point Member
    • nivram
    • Member since 06-16-2005, 1:43 AM
    • Posts 56
    Actually what i want is to do this in vb like i have specified in my subject. But anyway, i have no problem with how to save data into textfile what i want is after having save it will continue to save to users computer. Ofcourse with a option to choose a filename.

    Here is again the flow:

    After clicking a link like "download record".
    1. Do a program that will save the record from database to textfile and save first to the webserver.
    2. Then will continue to save to users computer by choosing what filename and where dorectory. It is just like you are downloading a file from the internet.


  • Re: export database records to tab delimited file. asp.net/vb

    10-19-2005, 9:42 PM
    • Participant
      980 point Participant
    • devnull
    • Member since 10-18-2005, 9:16 PM
    • New Zealand
    • Posts 195
    using (StreamWriter sw = new StreamWriter(Server.MapPath("~/exported/file.txt")))
    {
    // assume my previous post method is named: RetrieveRecord() and returns a StringBuilder
    sw.Write(RetrieveRecord().ToString());
    sw.Close();
    }

    Response.Write("right click <a href=\"exported/file.txt\">here</a> then save as to download the record");
Page 1 of 1 (7 items)