Posts

.NET Guid Formats

Image
GUIDs can be a quick (but not secure) way to generate random values in .NET. You can use them in UNIQUEIDENTIFIER columns in SQL Server or the equivalent data type in other database systems. The framework gives you a few formatting options, depending on what you need it for. See the documentation for more about the format strings. Or read more than you ever could possibly want to know about GUID/UUID at Wikipedia. View code on GitHub

.NET Tuples

Image
The new way of writing tuples in C# is much nicer than the old Tuple class. Learn more with the Microsoft docs View code in GitHub

.NET XML Writer

Image
XmlWriter is an easy way to generate XML when you need it. If you're building something more complex, like something for an API, you probably want to go with serialization instead. Otherwise, this is pretty easy to build XML how you want it top to bottom. View code on GitHub

Loops vs HashSets

Image
When looking up data in memory from a large collection, a HashSet or Dictionary is much, much faster than looping through an ordinary collection. This example uses strings - if you run the same code but treat them as GUIDs, the numbers improve quite a bit, but the difference between array and HashSet is still tremendous. View code on GitHub

SQL Server Table Valued Parameters with .NET

Image
private static SqlDataRecord CreatePersonRecord ( int id , string name ) { var metaData = new [ ] { new SqlMetaData ( " Id " , SqlDbType . Int ) , new SqlMetaData ( " Name " , SqlDbType . VarChar , 50 ) } ; var record = new SqlDataRecord ( metaData ) ; record . SetInt32 ( 0 , id ) ; record . SetString ( 1 , name ) ; return record ; } private SqlDataRecord [ ] GetPersonRecords ( ) { retun new [ ] { CreatePersonRecord ( 1 , " John Doe " ) , CreatePersonRecord ( 2 , " Jane Doe " ) } ; } using ( var conn = new SqlConnection ( cs ) ) { conn . Open ( ) ; using ( var comm = conn . CreateCommand ( ) ) { comm . CommandText = " dbo.TestPerson " ; comm . CommandType = CommandType . StoredProcedure ; var peopleParmsValue = GetPersonRecords ( ) ; if ( ! peopleParmsValue . Any ( )

Drop SQL Server Database

Image
 If you're working locally, building a new app from scratch, you might be in a position where you want to wipe the database and start clean – but SQL Server may block you if you've got any open connections or any other issues blocking you. This should set you as the only user of the database, which will clear the way for dropping it without ever getting an error: use master ; go if exists ( select 1 from sys . databases where name = 'SomeDatabase' ) begin alter database SomeDatabase set single_user with rollback immediate ; drop database SomeDatabase ; end ; go View code on GitHub

.NET Passwords

Image
Consider using Identity , Google , domain authentication , or some other solution for authentication, rather than building your own username/password database. But, if you are doing it anyway, here's some code to get you started: View code on GitHub