Database Reference
In-Depth Information
How It Works
After querying the database, like so:
// Create connection
conn = new SqlConnection(@"data source = .\sql2012;integrated security = true;
initial catalog = SQL2012Db;");
// Create command
cmd = new SqlCommand(@"select textfile, textdata
from TextTable", conn);
// Open connection
conn.Open();
// Create data reader
dr = cmd.ExecuteReader();
you loop through the result set (but here there is only one row), get the file name from the table with
GetString() , and print it to show which file is displayed. You then call GetCharsQ with a null character
array to get the size of the VARCHAR(MAX) column.
if (dr.Read())
{
// Get file name
textFile = dr.GetString(0);
txtRetrieveText.AppendText("------ start of file\n");
txtRetrieveText.AppendText(textFile);
txtRetrieveText.AppendText("\n");
textSize = dr.GetChars(1, 0, null, 0, 0);
txtRetrieveText.AppendText("--- size of text: " + textSize + " characters ---
");
txtRetrieveText.AppendText("\n--- first 100 characters in text -----\n");
charsRead = dr.GetChars(1, 0, textChars, 0, 100);
txtRetrieveText.AppendText(new String(textChars));
txtRetrieveText.AppendText("\n");
txtRetrieveText.AppendText("\n--- last 100 characters in text -----\n");
charsRead = dr.GetChars(1, textSize - 100, textChars, 0, 100);
txtRetrieveText.AppendText(new String(textChars));
return true;
}
else
{
return false;
}
Rather than print the whole file, you display the first 100 bytes, using GetChars() to extract a
substring. You do the same thing with the last 100 characters.
Otherwise, this program is like any other that retrieves and displays database character data.
 
Search WWH ::




Custom Search