Skip to content
Archive of posts filed under the SQL Server category.

Entity Framework Thoughts

I have been thinking for a little while about the future of Entity Framework, feature set on the horizon, and trying to ascertain where I would like to concentrate my next “free time” project.

If you have not been keeping up with Entity Framework, please read the last few posts on the Entity Framework design blog about the present and the future of the product.

The more I have been thinking the more I see a benefit in creating a complementary solution that would enhance features available in Entity Framework, and possibly gather some community input as to what features are most needed.  Just like a number of folks, I looked at the migrations feature outline that will likely be using existing database project API.  I worked with database projects for quite some time, and I am not a giant fan of the product.  I did submit a handful of suggested enhancements, but I am not sure now if any of them will see a light of day.  API that the project exposes is different from Visual Studio UI of course, and I am sure that API will be good to write migration code against.  On the other hand, I worked with Red Gate product called SQL Compare and related .NET SDK since about 2005 on and off.  When I used that product last, our company saved countless amounts of money by using Red Gate API to synchronize our product database with existing databases in the field.  You can read more about SQL Compare here and SDK here.  You can also buy both products as a bundle.  If you look at the price of SDK, it is about $700 per developer, and it includes 10 distribution licenses.  I encourage you to read more about licensing process and maybe even call Red Gate, if you decide to use the product.  There is also a competing product from APEX.  You can read more about that product here.

But back to Entity Framework.  I confess, I have been a fan of the product since 1.0 beta.  I used it on production projects, and I saved countless hours not having to code stored procedures and DAL layers.  I became quite excited when Code First came out on top of 4.0 version.  It offered very clean programming interface with a number of options to handle many use cases that developers might have.  It did lack a few features, namely migrations, pluggable conventions, indexes, and default values.  I have been thinking about how to address this, given that I am not a Microsoft employee.   After thinking about it for quite some time, I decided to start a CodePlex project in attempt to address these issue on my own.  I am going to rely on Red Gate based on my expedience with the product and the fact that the price is right in my opinion.

Stay tuned for the announcement.  I suspect that the initial alpha version that would include migration story will be out in a few weeks.  I am going to “productize” my blog posts on pluggable conventions and roll them into the same CodePlex project next.

I am welcoming any suggestions, including your thoughts on buying $700 product.

Thank you.

Post to Twitter

SSRS Report Viewer Error

I was working on a particular production problem yesterday. Sometimes when a user clicked on print button in SSRS Web Report Viewer control, and error was shown, and report is only partially printed, maybe 2 pages out of 4 are printed. Specific error that is shown to the user stated “An error occurred during printing (0×80004005)”.
Don’t you hate problems that start with “it only breaks sometimes”? Moreover, those that have totally nondescript errors?

One thing I learned a while ago, is that is web farm environment these types of problems are often related to configuration issues on one server vs. the other. So, I took a look at all the servers, and they all had SSRS Report Viewer redistributable installed. What was also interesting, is that preview worked fine, the error only occured when print button is pushed.
It took me a bit to replicate the problem, and then I did what I often do when debugging web problems – I fired up Fiddler. What Fiddler was reporting is that I was getting 404 Not Found error for Reserved.ReportViewerWebControl.axd. I could not understand why because report viewer redistributable should have installed the HTTP handler for me. I checked first production machine, and it was there. So, I checked the rest of the production servers. I found one that was missing the handler. No idea why, the same install was run on all the servers.
So, I added the handler in IIS 7 Management IIS Console -> root server node -> Handler Mapping -> Add Managed Handler with the following entries:
Request Path: Reserved.ReportViewerWebControl.axd
Type:Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Name=ReportViewerWebControlHandler
Note: I am using SQL Server 2008 R2, hence the assembly version is 10.0

And voila, the problem went away!
Thanks.

Post to Twitter

Selecting Comma Delimited Data From SQL Server

Today I had to accomplish the following task.  I had to create a report that selects data from primary table and also selects data from a related table , comma delimited into a column on a report.  I keep bumping into this problem periodically, but always have to research the solution.  Today I am documenting the solution. 

Example

I have the following database on my SQL Server:

USE [master]

GO

/****** Object:  Database [RolodexEFCtp]    Script Date: 01/17/2011 20:37:17 ******/

CREATE DATABASE [RolodexEFCtp] ON  PRIMARY

( NAME = N’RolodexEFCtp’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RolodexEFCtp.mdf’ , SIZE = 2304KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )

 LOG ON

( NAME = N’RolodexEFCtp_log’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RolodexEFCtp_log.LDF’ , SIZE = 504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)

GO

ALTER DATABASE [RolodexEFCtp] SET COMPATIBILITY_LEVEL = 100

GO

IF (1 = FULLTEXTSERVICEPROPERTY(‘IsFullTextInstalled’))

begin

EXEC [RolodexEFCtp].[dbo].[sp_fulltext_database] @action = ‘enable’

end

GO

ALTER DATABASE [RolodexEFCtp] SET ANSI_NULL_DEFAULT OFF

GO

USE [RolodexEFCtp]

GO

/****** Object:  Table [dbo].[Companies]    Script Date: 01/17/2011 20:37:19 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Companies](

      [CompanyId] [int] IDENTITY(1,1) NOT NULL,

      [CompanyName] [nvarchar](40) NOT NULL,

      [DateAdded] [datetime] NOT NULL,

PRIMARY KEY CLUSTERED

(

      [CompanyId] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

 

/****** Object:  Table [dbo].[CompanyContacts]    Script Date: 01/17/2011 20:37:19 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[CompanyContacts](

      [CompanyContactId] [int] IDENTITY(1,1) NOT NULL,

      [FirstName] [nvarchar](30) NOT NULL,

      [LastName] [nvarchar](50) NOT NULL,

      [CompanyId] [int] NOT NULL,

PRIMARY KEY CLUSTERED

(

      [CompanyContactId] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

/****** Object:  ForeignKey [Company_Contacts]    Script Date: 01/17/2011 20:37:19 ******/

ALTER TABLE [dbo].[CompanyContacts]  WITH CHECK ADD  CONSTRAINT [Company_Contacts] FOREIGN KEY([CompanyId])

REFERENCES [dbo].[Companies] ([CompanyId])

ON DELETE CASCADE

GO

ALTER TABLE [dbo].[CompanyContacts] CHECK CONSTRAINT [Company_Contacts]

GO

 

In the database above I would like to select company name in one column and semi-colon delimited list of employees.  Something like:

image

I am going to accomplish this task by using XML functionality of SQL Server, specifically FOR XML clause.

Here is what my select looks like that accomplish this task:

SELECT Companies.CompanyName,

      STUFF(

            (SELECT ‘; ‘ + CompanyContacts.LastName + ‘, ‘ + CompanyContacts.FirstName

                  FROM CompanyContacts

                  WHERE CompanyContacts.CompanyId = Companies.CompanyId

                  FOR XML PATH()), 1, 1, ) As AllContacts

FROM Companies

ORDER BY Companies.CompanyName

 

 

 

You can of course customize contacts query in a number of ways, sorting and filtering that data.  Primarily, I want to illustrate that you can accomplish seemingly hard task with a single query.

Thanks.

Post to Twitter

Getting Started with PowerPivot

Last week I took a stab at creating my first PowerPivot spreadsheet, and I wanted to document the steps.  Maybe this post will help someone, but it will help me remember, that is for sure!

Before you get started, you will need to download all the software required.  Here is my list:

Of course, you will need and instance of SQL Server 2008.  You can just use Express edition if you do not have one installed.

Now that I have everything installed and configured, I can start with PowerPivot.  One note – look for Hand shaped cursor in the screens below.s

Step 1 – Start up Excel 2010 and look for PowerPivot menu, then click on PowerPivot Window to launch pivot.

image

Step 2 – click on From Database in the window, then Select from SQL Server

image

Step 3 – Fill in SQL Server details.  I am choosing to use Data Warehouse DB.  Click Next when done.  On the next screen select an option to Select for a list of tables and views.

image

Step 4 – Select dimension tables and fact table to analyze.  Here is my list in alphabetical order

  • DimCustomer
  • DimDate
  • DimProductCategory
  • DimSalesTerritory
  • FactInternetSales

You can select any tables you like of course.  Wait while data is imported into PowerPivot, then click Close.  PowerPivot window will now have five tabs with data and columns directly imported from the database.  Here is what it looks like.

image

Now click on PivotTable menu and select  Chart and Table – Vertical option.  We will simply create a chart and an analysis table to go with it.  Click on New Worksheet when prompted.  This step will return you back to main Excel window.  You will see chart, table and on the left hand side Gemini task pane – listing of imported data.  Gemini used to be Microsoft code name for this product/functionality.

image

Step 5 – Building a chart.  We are going to analyze sales based on customer marital status by linking it to sales amount.  Click on the chart first, then in Gemini pane expand DimCustomer table and select MaritalStatus.  Then, expand FactInternetSales and select SalesAmount.  PowerPivot will guess what we are trying to do and MaritalStatus as Axis field and Sum(SalesAmount) as Values.  Here is what it looks like:

image

 

Step 6 – building table with filters. 

Click on table first, then check the following tables/fields in Gemini pane:

  • DimCustomer – MaritalStatus and Gender
  • DimDte – CalendarYear
  • DimProductCategory – EnglishProductCategoryName
  • DimSalesTerritory – SalesTerritoryCountry
  • FactInternetSales – SalesAmount

At this point your bottom of Gemini pane should look like this:

image

if you have anything else in Values, click on that item and select “Move to Row labels”.  What you will see also is that your have a report built for you in table area that uses all the analysis points.  You could leave it as is, but I will do more.

 

Step 7 Now I can analyze the data by “slicing it”.  To do this we will create two groups of slices – horizontal and vertical.  We will split our analysis categories as follows:

  • Horizontal Slices: Marital Status and Gender
  • Vertical Slices –English Product Category and Calendar Year.

I will leave Sales Territory country as a column in my table. To perform this I will click on each of my slices and select “Move to Slices Horizontal (or Vertical) as appropriate.  Then I click on each slice and choose Move Up or down until my pane looks like this:

image

Now here is what my table looks:

image

I will go ahead and move things around to make spreadsheet look better:

image

 

Step 8 – Use slices for analysis.  This process is super simple – just click on a desired slice(s) to enact filter on rows of a table.

image

To clear the filter just click on a funnel picture with x across it:

image

As you do so, you will see the chart and table both updating with your filter values.  One thing I did notice at the end – Catergory is lon linked to internet sales, so category filter has no effect on data.  If you see similar behavior, your data has the same issue.  You can always manually build relationships in PowerPivot by click on Table tab in pivot and choosing Manage Relationships menu:

image

 

As you can see, using PowerPivot is extremely easy and the results are very powerful.  You can save your spreadsheet, open it later and refresh the data by clicking on a filter!  You also have an option to show or hide Gemini panels in Excel under PowerPivot menu.  In the same menu you also have an option to show or hide Gemini panels.

Thanks.

Post to Twitter

Silverlight 4 + COM + SQL Server = Cool!

Today is the day to talk about COM possibilities in Silverlight 4.  One would question the titles of my post.  COM and Cool in the same sentence?  Let me proof this title to you.

First, let me take a closer look at COM.  In order to access COM, you must install Silverlight application on a local machine.  The access to COM is not enabled when a Silverlight application is run in the browser.  So, first of all you have to enable this by checking “Enable running application out of browser” checkbox in project properties.  Second of all, you have to check “Require elevated trust…” checkbox in “Out-of-browser settings” area in project properties.  Now, you are ready to install you application and test COM support.  How about Word automation:

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
var doc = word.Documents.Add();
var paragraph = doc.Paragraphs.Add();
paragraph.Range.Text = "Some text";

Now, let’s talk about database access.  Parts of System.Data.SqlClient namespace are not exposed to COM by default.  So, to enable database access we must write an assembly that is exposed to COM that wraps database access.  To do so, check the property “Register for COM Interop” in project properties for the .NET (NOT Silverlight) based project that will fire off database queries for us.  Once the assembly is built, I can just use RegAsm to register my DLL with COM on a machine.  Of course, this would be a prerequisite to use Silverlight application for local data access.  The other prerequisite is to have .NET Runtime installed on that machine as well.  I could write an install project of course to make this process easier.

The next step is to write a Silverlight assembly that would use COM Interop similar to the one above for Word to talk to my database access .NET based assembly.

dynamic sqlDB = ComAutomationFactory.CreateObject("COMSQLClient.COMSqlDatabase");

Now the most exciting part.  I created and published the project on CodePlex that does exactly what I just talked about.  Check out the project and let me know what you think.  You can download the source code and look into the implementation details.  Here is the link to it:

http://silverlight4sqllib.codeplex.com/

Thanks.

Post to Twitter

Talk at GGMUG

As always I had fun presenting at Gwinnett Georgia Microsoft Users Group on Thursday.  The subject of my talk was “Getting started with SQL Azure”.  I documented all the steps in this blog post.  There are also useful links that I mentioned in this post to help you get started.

There are many advantages to use SQL Azure.  Number of reason is probably the fact that one can eliminate the need to house expensive hardware and software on premises.  As long as you are fine with living with certain limitations of SQL Azure compared to SQL Server 2008, you can take advantage of this great technology.  The primary limitation is probably the size of the database.  You cannot have a database bigger than 10 GB.

Feel free to ask any questions on the topic.

Post to Twitter

Windows Azure Application

This is purely a bragging post :-)

I just deployed a test application to the cloud (Microsoft Azure): http://rolodex.cloudapp.net/

Here is the technology stack for it:

  1. Windows Azure
  2. SQL Azure
  3. CSLA 3.8.1
  4. Silverlight
  5. Entity Framework
  6. WCF
  7. Prism (Composite Application Guidance)
  8. Silverlight Toolkit

I promise to write a blog entry in the near future, the steps one has to follow through to create an Azure application.

Post to Twitter

SQL Saturday #25

I spoke yesterday at SQL Saturday event in Gainesville, GA.  My topic was CLR Integration in SQL Server. 

CLR stands for Common Language Runtime or .NET Framework.  This SQL Server 2005 or higher feature allows developers to write .NET assemblies and deploy them in SQL Sever.  You can implement scalar and table-values functions, stored procedures, triggers, user defined types and aggregates.

In my talk I spoke of features that I worked or had a need for.  Those included interacting with OS, such as file access or registry access.  I also talked about implementing fuzzy matching logic, creating custom bitmaps and utilizing them in SSRS reports.

You can download the sides and sample project here.

Thank you.

Post to Twitter

SQL Saturday Presentation

My talk at SQL Saturday just ended. My topic was CLR integration – Integrating CLR assemblies to support custom image creation and encompassing complex business logic in SQL server/SSRS.  I posted all the code and slides here

As always it was a pleasure to talk in front of my fellow developers.

Post to Twitter