Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Monday, October 26, 2009

INSERT's inside transactions

Are you a geek? Then read on. :-)

Reading Inside SQL Server 2005: Query Tunning and Optimization, I stumbled upon this beaultiful code sample. The guy (yes, a guy - it seems Ms. Delaney is subcontracting for some chapters...) creates a big sample table with 250,000 rows on it.


And there's not a single shred of comment about the code. He's doing 250,000 INSERT's on a table. In order to optimize it, he opens a transaction right before the loop starts, and on every 1,000 INSERT's he does a COMMIT then opens another transaction. This prevents SQL Server from opening a transaction on each INSERT. (Yes, Luke. When a data modification command to SQL Server you send, a transaction it automatically opens if the command is not already inside one. This allows you, for example, to change your mind and issue a ROLLBACK inside a trigger - even without a preceding BEGIN TRAN).

By using the explicit transactions around the INSERT's, the code runs them on 250 transactions, instead of 250 thousand ones. As I've said before, beaultiful. Simply beaultiful.

And in case you didn't notice: yes, I'm a geek. :-)

Thursday, October 22, 2009

SQL Server Compact is not intended for ASP.NET development

SQL Server Compact is a very restricted SQL Server edition in terms of features. But it runs in-process, wich means it does not run as a service being accessed from your application, like others SQL Sever editions. It runs inside your app. This allows for simplified deploy (non-administrative accounts can install an application using SQL Server CE databases), and reduced RAM, CPU and disk usage. It runs even on Windows Mobile!

Well, if you try to use it on an ASP.NET site, you end up with the error "SQL Server Compact is not intended for ASP.NET development". The reason is, like the message says, SQL Server CE is not made to be used on a multi-user environment like a web aplication. But if you want to use it anyway, for exampe for a quick test, place the following code on Global.asax:


Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", True)
End Sub

This way you are "taking full responsability" for using SQL Server CE on the ASP.NET site, and the error won't show up again.

Monday, October 19, 2009

AdventureWorks: just the MDF, please!!!

I find the K.I.S.S principle kind of offensive, but sometimes people ask for it. There's a bunch of guys developing a whole project on Codeplex to create a setup program for the AdventureWorks family of SQL Server sample databases. I wonder, is it so complicated to install a few databases? After all, people who will use it are very likely to be dba's or developers, and they certainly have some knowledge on SQL Server administration, or can understand some half-page instructions on how to restore a database backup or how to attach a database file.

Well, I needed the database. Downloaded the MSI for SQL Server 2008, and ran it. On setup things started to go south: setup asks for the instance on wich to install the databases, and presented me a list with the instances on my machine. I had only a default SQL Server Express instance. I selected it, but the "Next" button kept grayed, no matter what.

"Ok". Cancel setup. Go to Google and asks for "SQL Server Express AdventureWorks". There's a bunch of posts with some tortuous ways of doing it. One said, "run script instblablabla.sql installed with the MSI from Management Studio". Loaded the script - 190Kb (!). It creates the empty database structures and loads data from several CSV files that comes with the MSI too. Nope. Few errors, some restriction on having to enable FILESTREAM on SQL Server Express - unfortunatelly, my SQL Server Express installation is a x86 on top of Vista x64, and after some more Googling, I learned that this FILESTREAM stuff does not work on this scenario. But WHY do I need it??? Seems it is used to load data from the CSVs into the tables. [Sigh]. Think "complicated". Then double it.

Finally I lost my patience and downloaded the SQL Server *2005* version of AdventureWorks - from the Codeplex site itself. Extracted the MSI content and there they are, sitting beautifully on some subdir, the MDF and LDF files to the database. Attached them to my SQL Server Express that, inspite of being a 2008 installation, reads the 2005 MDF files without problems. Quick. Simple.

The MSI with the database files can be downloaded from http://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=4004. Select the "AdventureWorksDB.msi" option.

To extract the MSI content, run the following command from a command prompt:
msiexec -a FileName.MSI -qb TARGETDIR=extract_location_full_path
To attach a MDF file to SQL Server Express, open Management Studio and, on Object Browser, right click on "Databases" and select "Attach..."

Is it really necessary a setup project for that???

Thursday, October 8, 2009

SQL Server Compact is not intended for ASP.NET development

The exception "SQL Server Compact is not intended for ASP.NET development" is thrown when you try to use a SQL Server Compact Edition Database on an ASP.NET web site. That's because, as the message says, SQL Server CE was not made to be used on ASP.NET, because of its several restrictions relating "normal" SQL Server editions. If you cannot install a Standart or Developer Edition SQL Server on your development machine, try using Express Edition. It has everything a developer needs and it's free.

If you really want to use Compact Edition, add the following to your Global.asax file and the exception will go away:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", True)
End Sub