Category Archives: ADO.NET 2

MARS, the Multiple Transactions question and performance

Sahil (who is the author of this book on ADO.NET) and I chatted about transactions in MARS after my DotNetRocks show on Friday. I think his curiousity lay in the potential of multiple transactions within one connection in MARS. Rather than theorize, I thought I would just try it in code and my answer is “can’t be done”. You will get a System.InvalidOperation Exception that says “SqlConnection does not support parallel transactions.”

In theory this makes sense. In his MARS FAQ post, Angel (a tester on the ADO.NET team) explains when he likes to leverage MARS:

Q: So if MARS is not for performance what is it good for?

A: I like to use MARS in two core scenarios, 1) When using MARS results in cleaner looking code and 2) when I am using Transactions and need to execute in the same isolation level scope.

Q: When does using MARS result in cleaner looking code?

A: The quintessential MARS example involves getting a datareader from the server and issuing insert/delete/update statements to the database as you process the reader.

 Also, it’s good to note that MARS just is, it’s just there. You don’t really turn it on or off (though you can change the SQLConnection setting – but definitely read the FAQ on that point). It just kicks in when needed. It lets us code in a way that is logical. MARS was one of the most requested features for ADO.NET 2.0. It was really hard to get in there, but people were tired of getting an error message when they did what came naturally, try to use one connection for multiple commands.

But MARS won’t prevent you from writing code that will reduce your performance. In many cases, you will actually have much better performance leveraging connection pooling over forcing multiple queries (or executes) on one connection. So you should definitely consider how you take advantage of this feature.



Posted from BLInk!

MARS and Transactions

I’ve read about MARS, thought about MARS and written a little about it what I learned. Now I’m going to divert my attention from WSE for a while and start playing again wtih ADO.NET 2.0. The first thing I’m going to do is take Angel’s MARS FAQ blog post (he’s on the ADO.NET team) and try out some of this stuff with the new bits, especially with transactions. Someone asked me about them, but I hadn’t played with them yet so I didn’t have much to say. 🙁 

He also references an article on TechNet about MARS and transactions.

So check out Angel’s post. I know I have to experiment before I can truly understand what’s going on, so I will play with MARS a little more and figure out what you can and can’t do and report back!



Posted from BLInk!

Christian Weyer answers my quandry about iXMLSerializable in .NET 2.0

While digging into some ADO.Net 2.0 a few weeks ago, I found that iXMLSerializable, previously (or is that currently) hidden in .NET 1.x and used for datasets has now been exposed so that we can leverage it. I had been “victimized” (I say that tongue-in-cheek) by accepting the magic of my datasets being transformed into XML by web services to send over the wire. But now I didn’t want to just believe in the magic, I want to know how this works!

Christian Weyer has explained iXMLSerializable in much more depth and also acknowledges that there was a little joke among the web services gurus:

Well, to tell it with a little joke, some people tend to call it ISupportDataSet and not IXmlSerializable. This is because until version 2.0 of the .NET Framework this interface was used solely by the System.Data.DataSet type and some checks for this type had been hard coded in one or the other tools. So, let’s forget about those old days, IXmlSerializable has never been supported officially then, but now it is.

Justifying new features in Whidbey

I am reading Pablo Castro’s article on doing Asynchronouos processing in ADO.NET 2.0. Pablo is a/the (?) Program Manager on the ADO.NET team. This is the guy who, in his What’s New in ADO.NET Whidbey session at TechEd said that his team fought over a particular feature “because we love the developers“. They do. Dig deeply into into ADO.NET 2.0 and you will believe this. They love us and they want to make our lives easier.

Anyway, the article …. of course, I can’t really STREAM this article through my brain. I have to keep going back and forth until I grok what he is talking about and my head is spinning but I’m getting it and that’s a great feeling. However this, so far, is my favorite quote from his article:

Designing features based on their “cool factor” is too easy and too tempting, so we try to avoid it by making sure that we have a relevant application scenario for each and every new feature that we ship.

Cool!

serializable datatables and web services in ado.net 2.0

In ado.net 2.0, datatables are serializable (grin grin grin) so that means you can return them from a web service (though you must fill the tablename property first). Below is what the xml looks like (compared below that, to what the same table looks look like  in a dataset)

Not much difference here. But it creates a few less steps on both the web service end and the client end and it bridges a gap for developers. I wonder how many of us tried to return a datatable from a webservice because we didn’t understand the depths of xml serialization and that datatable, up until now, did not implement iSerializable. And then had to do a bit of digging before we found a sentence somewhere that explained that you need to return the dataset. So we save some coding, but also a whole lot of figgerin’ for those folks who don’t need to grok soap and serialization just to move some data around.

 <?xml version=”1.0″ encoding=”utf-8″ ?>

<DataTable xmlns=”http://tempuri.org/>
<xs:schema id=”NewDataSet xmlns=” xmlns:xs=”http://www.w3.org/2001/XMLSchema xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata>
<xs:element name=”NewDataSet msdata:IsDataSet=”true msdata:MainDataTable=”julietable>
<xs:complexType>
<xs:choice minOccurs=”0 maxOccurs=”unbounded>
<xs:element name=”julietable>
<xs:complexType>
<xs:sequence>
  <xs:element name=”au_id type=”xs:string minOccurs=”0 />
  <xs:element name=”au_lname type=”xs:string minOccurs=”0 />
  <xs:element name=”au_fname type=”xs:string minOccurs=”0 />
  <xs:element name=”phone type=”xs:string minOccurs=”0 />
  <xs:element name=”address type=”xs:string minOccurs=”0 />
  <xs:element name=”city type=”xs:string minOccurs=”0 />
  <xs:element name=”state type=”xs:string minOccurs=”0 />
  <xs:element name=”zip type=”xs:string minOccurs=”0 />
  <xs:element name=”contract type=”xs:boolean minOccurs=”0 />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>
<diffgr:diffgram xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata xmlns:diffgr=”urn:schemas-microsoft-com:xml-diffgram-v1>
<DocumentElement xmlns=”>
<julietable diffgr:id=”julietable1 msdata:rowOrder=”0>
  <au_id>172-32-1176</au_id>
  <au_lname>White</au_lname>
  <au_fname>chris</au_fname>
  <phone>408 222-1229</phone>
  <address>10932 Bigge Rd.</address>
  <city>Menlo Park</city>
  <state>CA</state>
  <zip>94025</zip>
  <contract>true</contract>
  </julietable>
  </DocumentElement>
  </diffgr:diffgram>
  </DataTable>

————————

  <?xml version=”1.0″ encoding=”utf-8″ ?>
<DataSet xmlns=”http://tempuri.org/>
<xs:schema id=”NewDataSet xmlns=” xmlns:xs=”http://www.w3.org/2001/XMLSchema xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata>
<xs:element name=”NewDataSet msdata:IsDataSet=”true>
<xs:complexType>
<xs:choice minOccurs=”0 maxOccurs=”unbounded>
<xs:element name=”julietable>
<xs:complexType>
<xs:sequence>
  <xs:element name=”au_id type=”xs:string minOccurs=”0 />
  <xs:element name=”au_lname type=”xs:string minOccurs=”0 />
  <xs:element name=”au_fname type=”xs:string minOccurs=”0 />
  <xs:element name=”phone type=”xs:string minOccurs=”0 />
  <xs:element name=”address type=”xs:string minOccurs=”0 />
  <xs:element name=”city type=”xs:string minOccurs=”0 />
  <xs:element name=”state type=”xs:string minOccurs=”0 />
  <xs:element name=”zip type=”xs:string minOccurs=”0 />
  <xs:element name=”contract type=”xs:boolean minOccurs=”0 />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>
<diffgr:diffgram xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata xmlns:diffgr=”urn:schemas-microsoft-com:xml-diffgram-v1>
<NewDataSet xmlns=”>
<julietable diffgr:id=”julietable1 msdata:rowOrder=”0>
  <au_id>172-32-1176</au_id>
  <au_lname>White</au_lname>
  <au_fname>chris</au_fname>
  <phone>408 222-1229</phone>
  <address>10932 Bigge Rd.</address>
  <city>Menlo Park</city>
  <state>CA</state>
  <zip>94025</zip>
  <contract>true</contract>
  </julietable>
</NewDataSet>
  </diffgr:diffgram>
  </DataSet>

Whidbey System.Data: Some new methods and a class

Since System.Data does not fit into the scope of my BCL talk and I LOVE ADO.NET and LOVE some of the new things in System.Data, I thought I would point them out here quickly for anyone who hasn’t seen it yet.

More user of DataReader
DataSet.GetDataReader
DataSet.LoadfromDataReader
 
So when you want the benefit of minimal stress on your resources(=datareader) yet the ability to pass data objects around (datareader can cause problems), you can now get the best of both worlds. Grab a datareader, shove it into a dataset and go.
 
DataTables are People, Too!!
DataTableReader class
DataTable.Load  (from DataReader)
 
DataTable gets XML
DataTable.ReadXML
DataTable.WriteXML
DataTable.WriteXMLSchema
Oh baby oh baby oh baby! THANK YOU FOR THIS! Need I say more?
 
Merging
DataSet.Merge
DataTable.Merge
These let you pull together datatables, datarows, etc.
 
A little more help with exceptions:
DataAdapterException
 
And for the really meticulous coder
DataSet BeginInit and EndInit
 
If you don’t have whidbey and the help file, you can see some of this stuff in the online longhorn sdk.  Just be sure to note where you are looking at stuff that IS in Whidbey and stuff that is not here until longhorn.
 
I’m so sad because I had to delete these from my deck.