How to view LINQ Generated SQL statements?

It’s multiple options how we can view generated SQL from LINQ to SQL or Entity framework. My favorite is by SQL Server profiler. BUT sometimes I need something quicker. And here is the option how to look directly at the DB (SQL Server) to the last executed query:

SELECT TOP 5 
	deqs.last_execution_time AS [Time], 
	dest.text AS [Query]
FROM 
	sys.dm_exec_query_stats AS deqs
	CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE 
	dest.text like '%vw_Visitor%'
ORDER BY 
	deqs.last_execution_time DESC

Only one note: Beneficial can by the WHERE condition … I suppose that the query looking under particular Table or View or SP so I can help to SQL server by adding WHERE condition like dest.text like '%vw_Visitor%' = By this I said something like : hey server, give me only query where is mentioned something related with particular View called vw_Visitor.  

InsertOnSubmit throwing NullReferenceException

Be careful when you are using your own constructor in partial classes on entities generated for LINQ to SQL (Data context entities). When you create your own constructor is really important to call the default constructor in it. Example :

Default contructor on LINQ to SQL entities in partial class


Why? Because is important to execute the default generated constructor while execution of your program. This constructor contains some Initialization under your entity:

Generated contructor of LINQ to SQL object
Otherwise you will getting following error:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance) +18
   System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance) +56
   System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +128
   System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +130
   System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level) +269
   System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +113
   System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity) +383

Microsoft SQL Server - Import wizard fails with incomprehensible message

Many times the confused/unknown error has a simple resolution. I tried to import big .csv file with simple GEO information to database. The columns were “IP Address“, “Country Code”, “Region”, “City”. When I open the file all columns look simple with length less then 50 characters. And that was the problem. I tried to import this file by following wizard:

MsSQL Server - Import Data Wizard

And when you specifying the source, look at the “Advanced” section and increase the length of your columns (string columns)

MsSQL Server - Import Data Wizard - Csv file as Source

MsSQL Server - Import Data Wizard - Csv file as Source

Otherwise, when the length of your columns will be short you see en error similar as on this screen (but this is error about wrong encoding Smiech):

MsSQL Server - Import Data Wizard - Error view

Text version of error:

Validating (Error)
Messages
Error 0xc02020f4: Data Flow Task 1: The column "IPFrom" cannot be processed because more than one code page (65001 and 1250) are specified for it.
 (SQL Server Import and Export Wizard)
 
Error 0xc02020f4: Data Flow Task 1: The column "IPTo" cannot be processed because more than one code page (65001 and 1250) are specified for it.
 (SQL Server Import and Export Wizard)
 
Error 0xc02020f4: Data Flow Task 1: The column "Code" cannot be processed because more than one code page (65001 and 1250) are specified for it.
 (SQL Server Import and Export Wizard)
 
Error 0xc02020f4: Data Flow Task 1: The column "Region" cannot be processed because more than one code page (65001 and 1250) are specified for it.
 (SQL Server Import and Export Wizard)
 
Error 0xc02020f4: Data Flow Task 1: The column "City" cannot be processed because more than one code page (65001 and 1250) are specified for it.
 (SQL Server Import and Export Wizard)
 
Error 0xc004706b: Data Flow Task 1: "Destination - dbip-cityT" failed validation and returned validation status "VS_ISBROKEN".
 (SQL Server Import and Export Wizard)
 
Error 0xc004700c: Data Flow Task 1: One or more component failed validation.
 (SQL Server Import and Export Wizard)
 
Error 0xc0024107: Data Flow Task 1: There were errors during task validation.
 (SQL Server Import and Export Wizard)
 

And when I optimized the length … the interesting thing was, that problematic column was the most unlikely - the “Code”. Code which is in 99% with 2 characters Diabol