Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

01 December 2005

Don't mix ExecuteScalar with For XML EXPLICIT

In doing SQL Server data retrieval, it is very handy to issue a ‘For XML EXPLICIT’ Select query in the stored procedure. Then in the DB application code use ExecuteScalar to read it.

So far so good – until recently - I decide to increase a char field length by 10 bytes. Suddenly a few unit test cases break (thanks God, they break…). Return Xml string is truncated at 2033 characters.

This behaviour occurs because, for XML results greater than 2,033 characters in length, SQL Server returns the XML in multiple rows of 2,033 characters each.

This article
ASP.NET Resources - ExecuteScalar Truncates XML at 2,033 Characters investigates and describes the symptom in detail.

16 September 2005

Automated SQL Server database objects scripting and deployment in continuous integration environment


The initiatives

In the normal development lifecycle, database objects (tables, views, stored procedures and etc) evolve as implementation progressed. The despaired changes in each developer’s environment need to aggregate into the central CI build server then cascade to each developer’s environment. Normally this is done manually as there is at lack of source control mechanism for database objects.
What we would like to have is an automated db objects scripting and release processes that we can plug into CC.Net or NAnt build process.

The Requirements:
Iteration one:
1) Automated (SQL Server) database scripting (DBGen.sql)
2) Version control DBGen.sql. – Only update source/roll out changes when new changes are made.
3) Assuming only one development machine is making changes to DB – so there is no need to consider builder server to development machines synchronisation.

Iteration two: SynchronisationSynchronous build server with development machines in a controlled way, i.e. only when a local development tasks has completed (build and tested) and the developer do a ‘Get Latest’ to sync the source code. The contrary to this is whenever changes to build server db is made and tests has been successful, using SQL backup/ replication/publish/subscription (?) mechanism to roll out the changes immediately.

Story break down (iteration one)
1. Post Build Event.
DevEnv (Vs.net) uses post build event to trigger user database schema objects (tables, views, store procedures etc) scripting.
1) Post-build event command line that kicks off the db object scripting. This can be done in two ways:
a. Scpriting (WScript/CScript) with ActiveX object SQLDMO. This option is more fine grain control on what objects to script.
b. Command line executable using SQL Server upgrade facility. Scripts entire database in one go. (Scripting Database Objects has detail introduction.)
2) Check file size (not sum size) on the generated DBGen.sql, auto check out the source control version if there are difference, - but do not check it in yet.

(Developer checks in all changes)
2. Release decision.
Decide whether there is a need to release new db schema by checking the source safe version and time stamp. This process needs to be built into Nant build process.

3. Run DBGen.sql
(If release required) DBGen.sql is executed by Nant using WMI and blah blah blah…
DBGen.sql is run after successful build, before unit tests.

Scripting Database Objects

DBAzine.com: Scripting Database Objects have some very good advice on command line Sql Server database objects (tables, index, store procedures etc) scripting. This is extremely useful on automated build process using NAnt. Basically you can run this command to generate db objects except database itself.
The script is created by executed this command:


C:\Program Files\Microsoft SQL Server\MSSQL\Upgrade>scptxfr.exe /s /d /I /f DBExtract.sql /q /r /H /Y

Then concatenate it with the 'standard' database drop and creation script produced with Enterprise Manager - All Tasks - Generate SQL Script (with only script database option)

16 November 2004

Setting Up Trusted Connection between Sql Server and Web Server

Initiatives: MS Pattern and Practices recommendation: Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication:
Extranet Security and Access Security.

Scenario 1 SqlServer and Web Server are running on the same box
Likely be the home pc or standalone development environment-the configuration is quite simple:
1. IIS6 (Windows 2003 Server)
Follow this article to configurate IIS 6 access to SqlServer.
In my experiments, it shows that the IIS anonymous account setting in the web app's '(Virtual) Directory Security setting | Authentication and access control' and disable/enable integrate Windows authentication is not relevant- it will always be an IIS_WPG member account used to access SqlServer. You can run EXEC sp_who to find out which account is used.

The only exception to above is you specify <identity impersonate="true"/> in web.config. I tried to use IUSER_(Internet Guest Account). However, it failed to get access to Sql Server (SP3)mysteriously, even I have grant the correct access rights to it.

2. IIS5 (Windows 2000 Server)
If running on IIS5(Windows 2000)it is IUSER_ used as the anonymous account. I guess it will be fine to use it to access SqlServer. Will try out soon. At the end of this blog is a vbs script which shows the credentials of IIS anonymous account and ASPNET Procs account.

Scenario 2 SqlServer and Web Server are running on two machines
In short, the MS recommended way is:
1)Don't put Web server on the trusted domain, but DMZ.
2)Create a NT account that impersonate as IIS anonymous account on web server.
3)Create the exact same account(username/password) on the Sql Server machine.
4)Grant access to this account.

How to use Trusted Connection when SQL server and web Server are on two separate machines has step by step guide on this.

Following vbs script shows credentials for iis anonymous user account


Dim IIsObject
Set IIsObject = GetObject ("IIS://localhost/w3svc")
WScript.Echo "AnonymousUserName = " & IIsObject.Get("AnonymousUserName") & vbCrlf
WScript.Echo "AnonymousUserPass = " & IIsObject.Get("AnonymousUserPass") & vbCrlf
WScript.Echo "WAMUserName = " & IIsObject.Get("WAMUserName") & vbCrlf
WScript.Echo "WAMUserPass = " & IIsObject.Get("WAMUserPass")
Set IIsObject = Nothing

23 November 2003

To use or not to use: Stored Procedures

There is a hot debate between Frans Bouma and Rob Howard on whether using SP in data driven application is a good/bad solution. Here are summaries of the crossfire:

What stung Frans most is the productivity. He admitted 2 months of development had been lost because tried to create a DAL with SP's and a flexible usage of that DAL in C#/VB.NET code. "I failed miserably, because the SPs API is too inflexible."

1. Flexibility, Productivity.
Rob argued using Dynamic SQL (DSQL) introducing high coupling between code and backend while SP provides an abstraction between Business Layer (BL) and DB, hence reduce the coupling.
Frans pointed out that a relational model has always coupling with the application. On the contrary to what Rob said, change to DB schema involves more work: change SPs signature, API that expose SP, caller components that target the API.
My experience coheres to Peter's comment The main bottleneck ... is programmer productivity...Stored procedures are hard to debug, hard to deploy, inflexible.
Code is very difficult to maintain. I have done an intranet ASP.NET application. One-month work, this includes about 110 SPs (24 tables) and corresponding DAL to expose them, all need to be comprehensively tested. And each time when change request comes in, it is an arduous task than a challenging one to dig in the dependencies. This pattern is similar to what Paul Wilson described, but I am the only one on the whole project. I reckon by using DSQL, I can cut down 30-50% of the development time yet boost the morale. (DAL and CRUD SPs are not fun in any sense.)

2. Performance
This could be the major selling point of SP over DSQL on two commobeliefses: 1) SP is pre-complied and execution plan is cached. 2) Cut the client call round trip to DB.
Guess what they are not true or partially not true.
-- In SQL Server (V7, 2000), SP is compiled at execution time and execution plan is cached. So as the DSQL. (SqlServer's Books Online: SQL Stored Procedures)--We are talking parameterized queries not ad hoc SQL statements.
-- In Oracel SP are precompiled. So point taken.
-- Round trip could be an issue. However, we also need to consider BL, DAL server boxes are standing next to DB server behind the proxy. Particularly, we can have a whole batch of T-SQL commands in a single call so to cut the round trip.
My take on is: performance issue need to decide by application profiling. Where if it is impossible to have two solutions side by side, we can stress test application (demo-ware). The result should be able to help us justify other pros and cons.

3. Security
It is now clear that both parameterized queries and SP are immune from injection attack.
Frans suggested using fine granulate role base security as a mean to control data access which is common to both SPs and DSQL. However, Phil Scott felt that for even the smallest set of role base security defined on columns level will be a nightmare. A task-base (rule-base?) is more easy to define and to maintain.
Frans also suggested using view to control security. I think this is a good point. DSQL can still taking benefits of indexed view, so does SP.