Forcing code-first to always initialize a non-existent database?

Initializer is executed when you need to access the database so if you want to create database on app start use anything of the following: context.Database.Initialize(true); //If set to true the initializer is run even if it has already been run. context.Database.Create() CreateDatabaseIfNotExists An implementation of IDatabaseInitializer that will recreate and optionally re-seed the … Read more

Where are SQL Server connection attempts logged?

You can enable connection logging. For SQL Server 2008, you can enable Login Auditing. In SQL Server Management Studio, open SQL Server Properties > Security > Login Auditing select “Both failed and successful logins”. Make sure to restart the SQL Server service. Once you’ve done that, connection attempts should be logged into SQL’s error log. … Read more

How can I get a trigger to fire on each inserted row during an INSERT INTO Table (etc) SELECT * FROM Table2?

The insert trigger is called once for bulk inserts, but on the trigger you can use the special inserted table to get all the inserted rows. So, imagine you have an insert trigger like this one, that logs all the rows inserted into table create trigger trgInsertTable on dbo.table for insert as insert tableLog(name) select … Read more

T-SQL strip all non-alpha and non-numeric characters

One flexible-ish way; CREATE FUNCTION [dbo].[fnRemovePatternFromString](@BUFFER VARCHAR(MAX), @PATTERN VARCHAR(128)) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @POS INT = PATINDEX(@PATTERN, @BUFFER) WHILE @POS > 0 BEGIN SET @BUFFER = STUFF(@BUFFER, @POS, 1, ”) SET @POS = PATINDEX(@PATTERN, @BUFFER) END RETURN @BUFFER END select dbo.fnRemovePatternFromString(‘cake & beer $3.99!?c’, ‘%[$&.!?]%’) (No column name) cake beer 399c