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.CREATETABLE [dbo].[Table_1]( [Id] [int] NOTNULL, [Name] [varchar](50) NULL)[TestFixture] publicclass TestSelectWithTempTableJoin { [Test] publicvoid Test() { conststring nbTempCreate = @" CREATE TABLE #Ids( Id INT ) "; conststring nbTempDrop = @" DROP TABLE #Ids "; conststring query = @" SELECT * FROM Table_1 t JOIN #Ids temp ON t.Id = temp.Id "; var ids = new List<int>(newint[]{1,3,4}); var rows = new List<KeyValuePair<int, string>>(); var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(); sqlConnectionStringBuilder.DataSource = "(local)"; sqlConnectionStringBuilder.InitialCatalog = "Tests"; sqlConnectionStringBuilder.ApplicationName = "TestTempTabl…

Get email address from active directory username in C#

* Add reference to System.DirectoryServicesstaticstring 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]; } } }