Datasheet
Klein c01.tex V3 - 12/13/2007 1:48pm Page 4
Part I: Introduction to Project LINQ
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT LastName, FirstName FROM
Person.Contact";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
/ / do something
}
}
}
If you wanted to use the same code to execute a stored procedure that takes one or more parameters, it
might look like this:
private void Form1_Load(object sender, EventArgs e)
{
string ConnectionString = @"Data Source=(local);
Initial Catalog=AdventureWorks;UID=sa;PWD=yourpassword";
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "uspGetBillOfMaterials";
cmd.Parameters.Add("@StartProductID", SqlDbType.Int).Value =
324;
cmd.Parameters.Add("@CheckDate", SqlDbType.DateTime).Value =
"07/10/2000";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
// do something
}
}
}
While you and I have probably coded something like this many, many times, it isn’t ‘‘friendly’’ on several
levels. First, you are combining two languages into one. You have the language you are coding (in this
case C#), plus you have the SQL language in quotation marks, which is not understood in the context
of .NET. With the .NET language you have IntelliSense, but you don’t get IntelliSense in the embedded
SQL syntax.
More importantly, however, there is no compile-time type checking, which means you can’t tell if
something is broken until run time. Every line of code has to be QA’d just to see if it even begins
to work.
Microsoft also packed a lot of features into the .NET Framework that enable developers to work with
XML. The .NET Framework contains the
System.Xml
namespace and other supporting namespaces,
such as
System.Xml.XPath
,
System.Xml.Xsl
,and
System.Xml.Schema
, which provide a plethora of
functionality for working with XML. The namespaces contain many classes and methods that make up
the XML .NET API architecture. The main classes are the
XmlDocument
,
XmlReader
,and
XmlWriter
.
To add to the complexity of working with different technologies, parsing an XML document isn’t the eas-
iest thing to do, either. Your tools of choice to work with XML are the Document Object Model (DOM),
4