Save text from database to text file : Text File Load Save « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » Text File Load Save 
32.55.2.Save text from database to text file
/*
Quote from


Beginning C# 2005 Databases From Novice to Professional

# Paperback: 528 pages
# Publisher: Apress (December 18, 2006)
# Language: English
# ISBN-10: 159059777X
# ISBN-13: 978-1590597774
*/
using System;
using System.Data;
using System.Data.SqlClient;

class RetrieveText
{
   static string textFile = null;
   static char[] textChars = null;
   static SqlConnection conn = null;
   static SqlCommand cmd = null;
   static SqlDataReader dr = null;

   public RetrieveText()
   {
      conn = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;initial catalog = tempdb;");

      // Create command
      cmd = new SqlCommand(@"select textfile,textdata from texttable", conn);

      // Open connection
      conn.Open();

      // Create data reader
      dr = cmd.ExecuteReader();
   }

   public static bool GetRow()
   {
      long textSize;
      int bufferSize = 100;
      long charsRead;
      textChars = new Char[bufferSize];

      if (dr.Read())
      {
         // Get file name
         textFile = dr.GetString(0);
         Console.WriteLine("------ start of file:");
         Console.WriteLine(textFile);
         textSize = dr.GetChars(10, null, 00);
         Console.WriteLine("--- size of text: {0} characters -----",
            textSize);
         Console.WriteLine("--- first 100 characters in text -----");
         charsRead = dr.GetChars(10, textChars, 0100);
         Console.WriteLine(new String(textChars));
         Console.WriteLine("--- last 100 characters in text -----");
         charsRead = dr.GetChars(1, textSize - 100, textChars, 0100);
         Console.WriteLine(new String(textChars))

         return true;
      }
      else
      {
         return false;
      }
   }

   public static void endRetrieval()
   {
      // Close the reader and the connection. 
      dr.Close();
      conn.Close();
   }

   static void Main()
   {
      try
      {

         while (GetRow() == true)
         {
            Console.WriteLine("----- end of file:");
            Console.WriteLine(textFile);
         }
      }
      catch (SqlException ex)
      {
         Console.WriteLine(ex.ToString());
      }
      finally
      {
         endRetrieval();
      }
   }
}
32.55.Text File Load Save
32.55.1.Load Text file to Database
32.55.2.Save text from database to text file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.