Database Reference
In-Depth Information
How It Works
You simply load the source code for the LoadText program.
// change this path to the location of text in your computer
static string fileName =
@"C:\VidyaVrat\C#2012 and SQL 2012\Chapter17\Code\Text and Binary Data\LoadText.cs";
Set CommandText with the insert starement and add parameters:
cmd.CommandText = @"insert into TextTable
values (@textfile, @textdata)";
cmd.Parameters.Add("@textfile", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@textdata", SqlDbType.Text, 1000000);
Execute command to drop the existing table and create a fresh one:
ExecuteCommand(@"if exists(select *
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME = 'TextTable')
drop table TextTable ");
ExecuteCommand(@"create table TextTable
(
TextFile varchar(255),
TextData varchar(max))"
);
Note that you first check to see whether the table exists. If it does, you drop it so you can re-create it.
Note The information_schema.tables view (a named query) is compatible with the SQL standard
INFORMATION_SCHEMA view of the same name. It limits the tables you can see to the ones you can access.
Microsoft recommends you use the new catalog views to get database metadata in SQL Server 2012, and SQL
Server itself uses them internally. The catalog view for this query would be sys.tables , and the column name
would be name. We've used the INFORMATION SCHEMA view here because you may still see it often.
Instead of the BinaryReader you use for images, GetTextFile uses a StreamReader (derived from
System.IO . TextReader ) to read the contents of the file into a string .
private string GetTextFile(string textFile)
{
string textBytes = null;
txtLoadText.AppendText("Loading File: " + textFile);
FileStream fs = new FileStream(textFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
textBytes = sr.ReadToEnd();
 
Search WWH ::




Custom Search