Retrieving data from TEXTcolumns is just like retrieving it from the smaller character data types. You’ll now write a simple console program to see how this works.
Try It Out: Retrieving Text Data
Follow these steps:
1. Add a C# Console Application project named RetrieveTextto the solution.
2. Rename Program.csto RetrieveText.cs, and replace the code with that in Listing 16-5.
C H A P T E R 1 6 ■ W O R K I N G W I T H T E X T A N D B I N A RY D ATA 425
Listing 16-5.RetrieveText.cs using System;
using System.Data;
using System.Data.SqlClient;
namespace RetrieveText {
public class RetrieveText {
string textFile = null;
char[] textChars = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader dr = null;
public RetrieveText() {
// Create connection conn = new SqlConnection(@"
data source = (local)\netsdk;
integrated security = sspi;
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 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(1, 0, null, 0, 0);
Console.WriteLine("--- size of text: {0} characters ---", textSize);
Console.WriteLine("--- first 100 characters in text ---");
charsRead = dr.GetChars(1, 0, textChars, 0, 100);
Console.WriteLine(new String(textChars));
Console.WriteLine("--- last 100 characters in text ---");
charsRead = dr.GetChars(1, textSize - 100, textChars, 0, 100);
Console.WriteLine(new String(textChars));
return true;
} else {
return false;
} }
public void endRetrieval() {
// Close the reader and the connection.
dr.Close();
conn.Close();
}
C H A P T E R 1 6 ■ W O R K I N G W I T H T E X T A N D B I N A RY D ATA 427
static void Main() {
RetrieveText rt = null;
try {
rt = new RetrieveText ();
while (rt.GetRow() == true) {
Console.WriteLine("--- end of file:");
Console.WriteLine(rt.textFile);
Console.WriteLine("======================================");
} }
catch (SqlException ex) {
Console.WriteLine(ex.ToString());
} finally {
rt.endRetrieval();
} } } }
3. Make it the startup project and run it with Ctrl+F5. You should see the result in Figure 16-5.
Figure 16-5.Retrieving text from a table
How It Works
After querying the database:
// Create command cmd = new SqlCommand(@"
select textfile, textdata from
texttable
", conn);
// Open connection conn.Open();
// Create data reader dr = cmd.ExecuteReader();
you looped through the result set (but here there was only one row), got the filename from the table with GetString(), and printed it to show which file was displayed. You then called GetChars()with a null character array to get the size of the VARCHAR(MAX)column:
if (dr.Read()) {
// Get file name
textFile = dr.GetString(0);
Console.WriteLine("--- start of file:");
Console.WriteLine(textFile);
textSize = dr.GetChars(1, 0, null, 0, 0);
Console.WriteLine("--- size of text: {0} characters ---", textSize);
Console.WriteLine("--- first 100 characters in text ---");
charsRead = dr.GetChars(1, 0, textChars, 0, 100);
Console.WriteLine(new String(textChars));
Console.WriteLine("--- last 100 characters in text ---");
charsRead = dr.GetChars(1, textSize - 100, textChars, 0, 100);
Console.WriteLine(new String(textChars));
C H A P T E R 1 6 ■ W O R K I N G W I T H T E X T A N D B I N A RY D ATA 429
return true;
} else {
return false;
}
Rather than print the whole file, you displayed the first 100 bytes, using GetChars() to extract a substring. You did the same thing with the last 100 characters.
Otherwise, this program is like any other that retrieves and displays database char- acter data.
Summary
In this chapter, you practiced storing and retrieving binary and text data using data types for large objects. There’s more to learn about large objects, particularly about issues that affect performance, but you’ve now seen the fundamental ADO.NET techniques.
In the next chapter we’ll look at another special kind of object (which can be as large as the objects we discussed here): the XML document. You’ll see how SQL Server sup- ports it with the XML data type.
Using XML
XML and its related technologies are major foundations of both the Internet and .NET.
How much about XML you need to know depends on what kind of programming you do, and you may not need to know anything about it. However, you’ll be doing yourself a dis- service if you don’t learn at least something about XML, and the more you know about it, the more valuable you can be to a database programming team. Our goal here is to intro- duce you to the most essential XML concepts and terminology and the most basic tech- niques for using XML with SQL Server. This will enable you to handle the most common programming tasks and to quickly learn more about XML as the need arises.
In this chapter, we’ll cover:
• What XML is
• How to generate XML from tables with T-SQL’s TO XMLclause
• How to query XML documents with T-SQL’s OPENXMLfunction
• How to store and retrieve XML documents using the xmldata type