Posts

Showing posts from September, 2012

C# select data using join to temp table

This is useful if you want to select a lot of data by key and writing IN statement is not efficient (too much data etc..). You load the keys to temp table and then select data using a join. CREATE TABLE [dbo].[Table_1]( [Id] [ int ] NOT NULL , [Name] [ varchar ](50) NULL ) [TestFixture] public class TestSelectWithTempTableJoin { [Test] public void Test() { const string nbTempCreate = @" CREATE TABLE #Ids( Id INT ) "; const string nbTempDrop = @" DROP TABLE #Ids "; const string query = @" SELECT * FROM Table_1 t JOIN #Ids temp ON t.Id = temp.Id "; var ids = new List< int >( new int []{1,3,4}); var rows = new List<KeyValuePair< int , string >>(); var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(); sqlConnectionStringBuilder.DataSource = " (local) "; sqlConnectionStringBuilder.InitialCatalog = ...

Get email address from active directory username in C#

* Add reference to System.DirectoryServices static string GetMail( string user) { using (var connection = new DirectoryEntry()) { using (var search = new DirectorySearcher(connection) { Filter = " (samaccountname= " + user + " ) ", PropertiesToLoad = {" mail "}, }) { return ( string ) search.FindOne().Properties[" mail "][0]; } } }