.Net adding directory security with Directory.CreateDirectory()

Quick post on how to set security permissions on a directory in C#.

  const string yourDirectory = @"c:\directory to create\";
  
  var securityRules = new DirectorySecurity();
  
  securityRules.AddAccessRule(
               new FileSystemAccessRule(@"yourdomain\identity", FileSystemRights.FullControl, AccessControlType.Allow)
               );
    
  Directory.CreateDirectory(yourDirectory, securityRules);

SQL server query demonstrating inner and outer joins

Here’s an example of a SQL Server query demonstrating inner and outer joins.


SELECT
q.[field1]
,REPLACE(REPLACE(CONVERT(nvarchar(max),q.[field2]), CHAR(13), ''), CHAR(10), '') AS 'friendly column name'
,(u.field3 + ' ' + u.field4) AS 'Name'
,ld.[field5]
,doc.[field6] AS 'Document ref'
,doc.[field7]
FROM [FreedomOfInformation].[dbo].[Table1] q
INNER JOIN [Database1].[dbo].[Table2] u ON u.[UserID] = q.[UserID]
INNER JOIN [Database1].[dbo].[Table3] l ON l.[LogID] = q.[LogID]
INNER JOIN [Database1].[dbo].[Table4] lt ON lt.[topicId] = q.[TopicID]
LEFT OUTER JOIN [Database1].[dbo].[Table5] ld ON ld.[qaid] = q.[QAID]
LEFT OUTER JOIN [Database2].[dbo].[Table6] doc ON ld.[DocumentID] = doc.[DocumentID]
WHERE q.[DatetimeAdded] BETWEEN DATEADD(YEAR, -3, GETDATE()) AND GETDATE()
ORDER BY q.[DatetimeAdded]

.Net simplest possible MSSQL database connection

We’re all using ORMS/Micro ORMS/some other framework to abstract away the data layer so I had to remind myself of how to connect to a database the ‘old-fashioned’ way.

using System;
using System.Data.SqlClient;

namespace DBConnTest
{
    class DataBaseConnection
    {
        static void Main(string[] args)
        {
            const string connStr = @"Data Source=XXXX\XXXX, [XXX];Database=XXXXX; Integrated Security=SSPI;";

            using (var conn = new SqlConnection(connStr))
            {
                var cmd = new SqlCommand("select..", conn) { CommandTimeout = 400 };
                conn.Open();

                var dataReader = cmd.ExecuteReader();

                while (dataReader.Read())
                {
                    Console.WriteLine(dataReader[0].ToString());
                    // ...
                }

            }
        }
    }
}