Blog Home  Home Feed your aggregator (RSS 2.0)  
What did you learn today? - Friday, August 18, 2006
Phil Denoncourt's Technology Rants
 
 Friday, August 18, 2006

I was preparing for the SQL Server Exam 70-441 (Designing Database Solutions by Using Microsoft® SQL Server™ 2005) and I kept coming across a whole bunch of small features that you don’t hear a lot about.  I’m not talking about the big ones everyone already knows about like CLR Integration, DDL triggers, XML, and ranking functions.  These are ones that took me by pleasant surprise.  I think they’re really cool, so I wanted to point them out.

 

Included Columns / Covering Indexes

Say you had a critical/oft-used report that ran a statement like:

 

SELECT Name, ProductNumber

FROM Production.Product

WHERE Style='U' AND Class='L'

 

If you wanted to put an index on the table to speed things up, you’d put an index on Style, Class, Name, and ProductNumber.  That would remove the need to go from the index page to the table page to get all the information for the query.  SQL 2005 has added the include clause to the Create Index statement.

 

CREATE INDEX IX_Product_Class_Style

ON Production.Product (Class,Style)

INCLUDE (Name, ProductNumber);

 

What this is doing is indexing just the Class and Style fields of the table.  The Name and ProductNumber are stored at the leaf nodes of the index.  This saves space in the index pages, meaning you can get more rows in an index page, reducing I/O and Disk Space.

 

Dynamic Management Views

Seriously, these are pretty cool.  Really.  If you haven’t looked at these, take the time to play around with a few. 

 

With these new views, you are able to get high visibility into the SQL Server engine.  Wondering what’s about to be written to the disk?  Query sys.dm_io_pending_io_requests.  Troubleshooting execution plans?  Use sys.dm_exec_cached_plans and sys.dm_exec_plan_attributes.  Need to see what’s running on the server right now?  sys.dm_os_tasks.  How often is an index used?  There’s sys.dm_db_index_usage_stats that will answer that question.  There are over 80 views that provide very useful information when you are troubleshooting problems or just curious as to how SQL Server organizes itself.

 

sp_create_plan_guide

Every now and then, I have a query that runs like a jackrabbit on my development machine, but runs like 3 legged turtle on a production machine.  This is usually because it decided not to use an index on the production machine due to differences in data density or because running the query with parallelism is slower with some statements.  To troubleshoot these situations, I end up modifying the stored procedure to add various hints.

 

Rather than modify the stored procedure, you can specify a hint for an existing query plan using the sp_create_plan_guide stored proc.  This way you can experiment without modifying the stored procedure and the hint doesn’t become part of your codebase.  MSDN documentation can be found here.

 

OUTPUT Clause

A lot of people already know about this pretty cool feature, but I didn’t.  In the past, if you do an insert to a table, and the table has an identity column, after the insert, you would look at @@IDENTITY or scope_identity().  That always seemed hackish to me.

 

You can now add an OUTPUT clause to the end of an INSERT, UPDATE, or DELETE statement.  That will return the rows changed by the statement.

 

INSERT INTO Person.Address

      (AddressLine1,AddressLine2,City

      ,StateProvinceID,PostalCode,rowguid,ModifiedDate)

VALUES(@AddressLine1,@AddressLine2,@City

      ,@StateProvinceID,@PostalCode,@rowguid,@ModifiedDate)

OUTPUT INSERTED.AddressID;

 

You use the pseudo tables INSERTED and DELETED, just like in triggers.

 

Storing BLOBs in Table page

Inevitability, on each project I work on, there is a requirement to have a field capable of storing more than 8k in a table.  It’s usually a field that the user can enter unlimited comments about a customer.  So I define it as a text field.  I always feel guilty about it because the user’s text will rarely be more than 100 bytes and I know it’s going to be stored on a different page.

 

In SQL 2005, if the data will fit in the same page as the rest of the record, it will place it in the same page.  This works with varchar/varbinary/nvarchar(max) and XML datatypes.  The behavior is described here at MSDN.

Persistence of Computed Columns

This is kind of neat.  We’ve had the ability to define computed columns in tables for a while.  They worked exactly as they’re named.  When the column was needed, it was computed.  Now the results of the computed column can be saved to disk.  This allows you to index computed columns.  You can also feel free to create calculated columns that can be computationally intense.

 

CREATE TABLE LineItem (

      LineItemID int IDENTITY(1,1),

      OrderID int,

      ItemDesc varchar(512),

      Qty int,

      Cost money,

      LineTotal as Qty * Cost PERSISTED

)

 

Those are all the ones I found.  What you have noticed as small new features in SQL that make life easier?

Friday, August 18, 2006 9:01:27 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Development | SQL  | 
 Wednesday, June 14, 2006

Tomorrow I'll be speaking at the New Hampshire .NET User's group.  More information can be found at www.nhdn.com

6:00 - 7:00 PM Xml Serialization.  XML Serialization is the process of transforming a complex object to and from XML.  This is made straightforward with the .NET framework.  We'll take a look out how to serialize a simple object and some of the attributes used to control the output.  Then we'll take a look at some of the more advanced features like capturing processing errors, overrides and unknown elements.  Finally we'll take complete control of the serialization process using the new features of the 2.0 Framework.  Be the envy of all your coworkers by mastering XML serialization

7:00 - 8:00 PM Xml in SQL 2005.  There are a variety of different ways that SQL Server can work with XML data.  Phil will demonstrate the various methods such as the XML datatype, OpenXML, How to return XML data in queries, Annotated XSDs, and interoperability with .NET

Phil Denoncourt is a .NET consultant, who over the past 4 years has developed a wide range of .NET applications and has over 20 years experience writing software. He is the leader of the New England C# User Group and has acquired the MCPD, MCTS, MCSD, MCDBA, MCSE, MCAD, MCSA and MCP+SB certifications. When not coding, or spending time with his wife and 5 children, Phil is an avid fan of the Boston Red Sox and New England Patriots.

Wednesday, June 14, 2006 11:15:28 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Speaking Engagements  | 
 Thursday, March 09, 2006

UPGRADE: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 2

Create a client application to access a remote object.

  • Create a remote object.
  • Configure a client application programmatically.
  • Configure a client application manually by using configuration files.
  • Access the remoting service by calling a remote method.
  • Call a remote method synchronously in a client application.

To create a remote object, configure the client application to use remoting.  This can be done using a config file, or programatically.  MSDN has information on setting up a client application.

You can see how to progmatically setup a client on Jibin Pan's article at c-sharpcorner

MSDN has information on how to setup a client using configuration files.

To call a remote method, create the object as usual (if you have remoting configured for the type) or use Activator.GetObject.

By default remoting calls are synchronous.  So I think they meant to say "Call a remote method asynchronously in a client application".  MSDN has information on how to do invoke a method asynchronously.

Thursday, March 09, 2006 8:52:12 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
UPGRADE: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 2

Create and configure a server application.

  • Create a server application domain.
  • Configure a server application programmatically.
  • Configure a server application by using configuration files.
  • Compile and build a server application.

In general, it's good to look at the Remoting FAQ at thinktecture.

To create a server app domain, look at these instructions on MSDN.  Use a configuration file, and use RemotingConfiguration.Configure to read it in.  The objects are available at that point.

To do it programically, look at this article at Express Computer

The MSDN article has instructions on how to configure the server application.

I don't see why building a server application is any different that other types of applications.  You just compile to get an executable.  Make sure that the configuration file (if you're using a config file) gets named to <app>.exe.config and is deployed with the executable.

Thursday, March 09, 2006 8:36:00 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
 Tuesday, March 07, 2006
70-554 Section I Part I by phildenoncourt

UPGRADE: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 2

Configure SOAP messages.

  • Configure the formatting of SOAP messages for a Web service method.
  • Specify the basic information for a Web service application.
  • Specify the bindings of a Web service application by using the WebServiceBindingAttribute attribute.
  • Configure a Web service application by using a Machine.config file.
  • Configure a Web service application by using a Web.config file.

MSDN has a section on formatting SOAP messages.  Know about the SoapDocumentMethod attribute and SoapDocumentService attribute.  Know that you can control the formatting using XML Serialization attributes.

I guess the basic information for a web service application would be specified using the WebService attribute.  You can control the name of the web service, and the namespace.

Binding defines a concrete set of operations.  It is implemented by the WebServiceBinding attribute.  You can specify what WSI spec your service conforms to (as of now, Basic Profile 1.1 or none), whether or not the service emits conformance claims, the location of the binding (defaults to your web service), and the namespace associated with the binding.  Christian Weyer has some thoughts about the attribute on his blog.

The XML Web Services repository has a good FAQ.  In in, some of the things controlled by the web/machine.config are pointed out.  Here's how to enable/disable WSDL generation.  Also you can control the location of a dynamic reference to a web service in the config file.

Tuesday, March 07, 2006 8:16:16 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
 Thursday, March 02, 2006

PRO: Designing and Optimizing Data Access by Using Microsoft SQL Server 2005

Design client libraries to write applications that administer a SQL Server service.

  • Design server management objects (SMO) applications.
  • Design replication management objects (RMO) applications.
  • Design automation management objects (AMO) applications.
  • Design SQL Server Networking Interface (SNI) for asynchronous queries.

Here's MSDN's section on SMO.  Darshan Singh has an overview of SMO at YukonXML.comDavid Hayden has an example of how to create a table using SMO.

MSDN's section on RMO.  There is not a lot of community activity in respects to RMO.  Here's my take.  Probably good to know how to create a publication (EnabledTransPublishing on ReplicationDatabase, Create a TransPublication object), create a subscription (TransPullSubscription, or TransSubscription), how to cause a syncronization (TransSynchronizationAgent).  Keep in mind I've never use these objects, these are the ones that strike me as important.

It appears that AMO really means Analysis Management Objects.  There appears to be no reference anywhere to "automation management objects" anywhere.  Here's MSDN's section on AMOLiu Tang and Paul Bradley wrote a overview for MSDN magazine.

SNI For Async?  It looks like SNI is used for tracing.  Here's an article at MSDN that mentions SNI.

Next up-> MARS

Thursday, March 02, 2006 1:43:43 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
70-442 - Part II - Caching by phildenoncourt

PRO: Designing and Optimizing Data Access by Using Microsoft SQL Server 2005

Design caching strategies.

  • Select ADO.NET caching.
  • Design custom caching functionality.
  • Design a refresh strategy for cached data.

ADO.NET doesn't have a cache.  ASP.NET does.  ADO.NET retrieves results into objects like DataSets that can be cached in the ASP.NET cache or your own Cache app.

Designing your caching functionality seems like a lot of work.  You have to manage the size of the cache, object expiration, object invalidation.  I use the Enterprise library and that provides a lot of the plumbing.

Refreshing cached data is done using Query Notifications via the SqlDependency or SqlNotificationRequest objects.  There is a good article on MSDN outlining how to set this up.  Steve Smith mentions the changes that have occured since Beta 2 on his blog.

Next up->Client Libraries

Thursday, March 02, 2006 1:07:33 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 

PRO: Designing and Optimizing Data Access by Using Microsoft SQL Server 2005

Design appropriate data access technologies.

Design an appropriate data access object model.

Design a cursor strategy for a data access component.

  • Decide when to use cursors.
  • Decide how to maximize cursor performance.
  • Detect which applications are using cursors and evaluate whether to remove them.

I just looked at my testing schedule and realized I'm taking this test in two days.  Time to get cramming...

The first two items are kind of vague.  I don't think there's much to be said there.

My cursor strategy is... don't use them.  However it appears there are times when it is faster.  Andy Machanic has found that they solve the running sum problem much faster than traditional relational methods.  However he found using CLR worked much better.

Maximizing performance would be centered around opening it as FAST_FORWARD.

There's a couple of ways that I can think of to detect whether or not an "app" is using cursors.  You can scan through the system tables(sys.procedure) looking for the word OPEN CURSOR.  That won't work if a) the stored procs are encrypted, b) the stored procs utilize CLR, or c) the app uses dynamic SQL. The best option would be to profile the database, looking for cursor events.

Thursday, March 02, 2006 12:28:08 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
 Wednesday, March 01, 2006

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

Copy a Web application to a target server by using the Copy Web tool.

Precompile a Web application by using the Publish Web tool.

Optimize and troubleshoot a Web application.

  • Customize event-level analysis by using the ASP.NET health-monitoring API.
  • Use performance counters to track the execution of an application.
  • Troubleshoot a Web application by using ASP.NET tracing.
  • Optimize performance by using the ASP.NET Cache object.

The Copy Web Tool allows you to copy your website from your machine to another machine.  It can connect to a remote machine through http (using Frontpage extensions), FTP.  It can also deploy to your local IIS machine, or your file system.  It does a bi directional sync, meaning changes from your machine are uploaded to the server and changes from the server are downloaded to your machine.

The Publish web tool allows you to precompile your website.  You can then deploy the website without the accompanying source code.  You publish to either a local file path, ftp or http.  If you select the "Allow this precompiled site to be updateable" option, the HTML is not compiled into the assembly.  If you select the "Use fixed naming and single page assemblies" option, you will get an assembly for each page.  Skins and Themes are still compiled into a single assembly.  The last option available to you is whether or not you want to strongly name the assemblies.

In the last section of this MSDN article, Jeff Prosise describes the health monitoring API.  There is also a Patterns and Practices article describing best uses of the feature.

There are quite a few performance counters.  Look (in perfmon) under ASP .NET Apps v2.0.50727 .  Counters that look interesting to watch are Anonymous Requests / Sec, Error Events Raised / Sec, Request Execution Time, Request Wait Time, Requests Executing,  Requests / Sec, Sessions Total.

To get tracing information from an ASP.NET application, you still have to enable tracing in the web.config file.  You request http://localhost/myapp/trace.axd to view the trace logs.  There is an article at ExtremeExperts.com on the new tracing features.

Thiru Thangarathinam has a overview of the new caching features at 15seconds.com.  Important changes appear to be the SqlCacheDependency.

Next up->Master Pages

Wednesday, March 01, 2006 7:40:22 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
 Monday, February 27, 2006

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

Create a composite Web application control.

  • Create a user control.
  • Convert a Web Forms page to a user control.
  • Include a user control in a Web Forms page.
  • Manipulate user control properties.
  • Handle user control events within the user control code-declaration block or code-behind file.
  • Create instances of user controls programmatically.
  • Develop user controls in a code-behind file.
  • Create a templated user control.

Add New Item->Web User Control.

There is an article on MSDN on how to convert a page to a user control.  It's a little easier than in 1.1 if you are using single-file pages.  Rename to .ascx, change @Page to @Control. Remove HTML, Body and Form tags from the HTML Code.

There is also an article on MSDN on how to include user controls in a page.  Add a Register directive to the top of the page, specifying a prefix, tagname and a relative URL to the user control.  You can also now drag the user control from your project onto the page and it will wire it up.

You manipulate a user control as you would any other control, through a member variable of the page.

To handle a user control event within the user control code declaration block, you add it the event as an attribute to the tag.  Here is an example of a usercontrol that has an event named TextStringChanged.

<uc1:MyUserControl ID="MyUserControl1" runat="server" TextString="Phil was here" OnTextStringChanged="TextStringChanged"/>

To handle it in the code-behind file, you treat it like a event on any other type of control.  Add a event handler delegate to the event.

To programatically create a usercontrol, use the LoadControl method of the Page.  That returns a reference to a new instance of the user control.  Then you must add it to a controls collection in order for it to appear on a page.

Creating templated usercontrols sounds tricky, but it's not that bad.  Try the walkthough on MSDN .

Next up->Copy / Publish Web tool

Monday, February 27, 2006 7:33:43 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
 Sunday, February 26, 2006

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

Create, delete, and edit data in a connected environment.

  • Retrieve data by using a DataReader object.
  • Build SQL commands visually in Server Explorer.
  • Build SQL commands in code.
  • Create parameters for a command object.
  • Perform database operations by using a command object.
  • Retrieve data from a database by using a command object.
  • Perform asynchronous operations by using a command object.
  • Perform bulk copy operations to copy data to a SQL Server computer.
  • Store and retrieve binary large object (BLOB) data types in a database.

To use a DataReader, you create a command object, open the associated connection, and call the ExecuteDataReader method.

To build SQL Commands visually in Server Explorer, right click on the Database in the Server Explorer and select "New Query".

To build SQL Commands in code, you create a SQLCommand instance, set the commandtext property with the SQL Statement, add parameter objects to the parameters collection, and associate a connection to the command object.

Creating parameters is done by creating SqlParameter objects, setting the name, dbtype and value, and append to the command's parameters collection.

To perform database operations using a command object, invoke the ExecuteNonQuery method.  It executes the SQL statement of the command object and returns the number of rows affected.

To retrieve data using a command object, you can call the ExecuteDataReader, ExecuteScalar, or ExecuteXMLReader methods.

The command object now supports async operations.  There are BeginExecuteNonQuery, BeginExecuteDataReader, or BeginExecuteXmlReader methods.  Vishnu Prasad has a writeup on this at DevX.

They've added managed support for SQL Bulk Copies in the SqlBulkCopy class.  David Hayden has an example of how to use this class.

Working with Blobs isn't as hard as it used to be.  You just set the parameter's dbtype to SqlString and assign the large string to the parameter's value property.  No AppendChuck, GetChunk.  There is another method that I found interesting from Vadivel's blog.  I don't think that will be on the test.

Next up-> Composite Web Controls

Sunday, February 26, 2006 6:15:49 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

Manage connections and transactions of databases.

  • Configure a connection to a database graphically by using the Connection Wizard.
  • Configure a connection by using Server Explorer.
  • Configure a connection to a database by using the connection class.
  • Connect to a database by using specific database connection objects.
  • Enumerate through instances of Microsoft SQL Server by using the DbProviderFactories.GetFactoryClasses method.
  • Open a connection by using the Open method of a connection object.
  • Close a connection by using the connection object.
  • Secure a connection to protect access to your data source.
  • Create a connection designed for reuse in a connection pool.
  • Control connection pooling by configuring ConnectionString values based on database type.
  • Use connection events to detect database information.
  • Handle connection exceptions when connecting to a database.
  • Perform transactions by using the ADO.NET Transaction object.

Again, I still haven't found a "Connection Wizard".  Referring to my earlier post, There is a Data Source Configuration Wizard. 

To configure a connection in the server explorer, right click on Data Connections node, and select Add New Connection.

Also, there is no Connection class.  There are a few objects that have a Connection property.  There are classes that implement IDbConnection, but strictly speaking, (unless we talking about sharepoint) there is no Connection class.  However, if you have a class that implements IDbConnection (SqlConnection, OleDbConnection...), you configure it using the ConnectionString property.

To connect to a SQL Server database, use the SqlConnection object.  Call the Open method after setting the connection string.

Alex Homer has an example of how to use DbProviderFactories.GetFactoryClasses at Devx.com.  It returns a datatable of all the classes that registered as DbProviderFactories in the machine.config.

To close the connection, invoke the close method on the connection object.  Obviously, an exception will be thrown if the connection is no longer open.  You can inspect the state property to see if the connection is still active.

To protect Datasource information, you're supposed to put the ConnectionStrings in the ConnectionStrings section of the web.config.  Then you encrypt the contents using the method outlined by David Hayden's site.

To create a connection that will stay in a pool, make sure your connections are connecting to the database with the same login.  Reportedly having minor differences in the connection string will cause it not to pool. 

There is information about controlling connection pooling at MSDN via ConnectionString values.

There are two events worth listening to on a SqlConnection.  InfoMessage (fired when warnings or informational messages are returned by SQL Server), and StateChanged (fired when the state of the connection has changed).

To handle exceptions when connecting to a database, catch SqlException (if SQL rejected the connection), or InvalidOperationException (The connection was already opened, or you haven't given the connection object enough info.)

To Begin a transaction on an open connection, call the BeginTransaction method on the SqlConnection object.  That returns a transaction object that you control the Commit or Rollback with.  There is a new locking level, snapshot isolation which bears understanding.

There is also a new Systems.Transaction namespace.  This is used for distributed transactions.  John Papa has a writeup on MSDN covering the feature.

Next up-> Data objects

Sunday, February 26, 2006 5:34:24 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework

Implement data-bound controls.

  • Use tabular data source controls to return tabular data.
  • Use hierarchical data source controls to display hierarchical data.
  • Display data by using simple data-bound controls.
  • Display data by using composite data-bound controls.
  • Display data by using hierarchical data-bound controls.
  • Use the FormView control to display the values of a single table record from a data source.

Tabular data source controls would be the SqlDataSource, AccessDataSource, and ObjectDataSource.  SqlDataReader and AccessDataSources should be self explanatory.  The ObjectDatasource allows the binding of custom business objects, including datasets.  GridViewGuy has a walkthrough on using the component on his site.

There are two hierarchical data source controls.  SiteMapDataSource, and the XmlDataSource.  The SiteMapDataSource reads SiteMap info from the SiteMapProvider for the website.  It keeps current as the user navigates the website.  The XmlDataSource allows you to bind XML.  Keyvan Nayyeri has a tutorial on the XMLDataSource at CodeProject.

Most web controls can be bound.  You should know the difference between Eval (Readonly data) and Bind(Two way binding; data might be changed).  There is an article at MSDN on this.

Composite Databound controls are the Gridview, DataList,  DetailsView and the Repeater.  Dino Esposito has an article at MSDN on the GridView.  It's old, but it still applies.  Wei Meng Lee has an article on the Datalist.  The data reader doesn't seem to have changed.   GridViewGuy has an article on the DetailsView.

To display hierarchical data, you can use the new TreeView.  Technically, you could also use the SiteMapPath or the Menu.  Thiru Thangarathinam has a very comprehensive article on binding with this control at 15seconds.

The FormView is a templated control that works on a record by record basis.  You provide a readonly layout in the ItemTemplate, and a layout for edits in the EditItemTemplate. There is an overview at ASP.NET.

Next up -> Database Connections

Sunday, February 26, 2006 5:01:15 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
Copyright © 2010 Phil Denoncourt III. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: