Posts

Showing posts from February, 2012

Custom LINQ to SQL Query with dynamic result

[TestFixture] publicclass TestDynamicLinq2Sql { [Test] publicvoid TestDynamicLinq() { var ctx = new MyDataContext("<connection string>"); var result = ctx.ExecuteQuery("SELECT USER [User], SUSER_SNAME() [SysUser]"); foreach (dynamic row in result) { Console.WriteLine("{0},{1}", row.User, row.SysUser); } } [Test] publicvoid TestDynamicRow() { dynamic d = new DynamicRow(); d.FirstName = "Albert"; d["LastName"] = "Einstein"; Console.WriteLine(d.FirstName); Console.WriteLine(d.LastName); } } publicstaticclass LinqExtensions { publicstatic IEnumerable<dynamic> ExecuteQuery(this DataContext ctx, string query) { using (DbCommand cmd = ctx.Connection.CreateCommand()) { cmd.CommandText = query; ctx.Connection.Open(); using (DbDataReader rdr = cmd.ExecuteReader(CommandBe…

Create chart in C# Windows Forms and save it to file

Image
staticclass Program { [STAThread] staticvoid Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (var chartForm = new ChartForm()) { FillData(chartForm.Chart); chartForm.Show(); //save chartForm.Chart.SaveImage(@"Chart.png", ChartImageFormat.Png); chartForm.Close(); } } publicstaticvoid FillData(Chart chart) { chart.Titles[0].Text = "Price/Volume Chart"; chart.Series[0].Name = "Price"; chart.Series[1].Name = "Volume"; var random = new Random(); for (int i = 0; i < 10; i++) { var date = DateTime.Today.AddDays(i); chart.Series[0].Points.Add(new DataPoint(date.ToOADate(), random.NextDouble() * 1e3)); chart.Series[1].Points.Add(new DataPoint(date.ToOADate(), random.Next() / 1e3)); } } }//Designer Code privatev…

Open Windows Form from nunit test

[Test] publicvoid Test() { var form = new Form1(); Application.Run(form); }

Create excel spreadsheet with C#

Image
* Download EPPlus library http://epplus.codeplex.com/[Test] publicvoid Test() { var file = @"Sample.xlsx"; if (File.Exists(file)) File.Delete(file); using (var excel = new ExcelPackage(new FileInfo(file))) { var ws = excel.Workbook.Worksheets.Add("Sheet1"); ws.Cells[1, 1].Value = "Date"; ws.Cells[1, 2].Value = "Price"; ws.Cells[1, 3].Value = "Volume"; var random = new Random(); for (int i = 0; i < 10; i++) { ws.Cells[i + 2, 1].Value = DateTime.Today.AddDays(i); ws.Cells[i + 2, 2].Value = random.NextDouble() * 1e3; ws.Cells[i + 2, 3].Value = random.Next() / 1e3; } ws.Cells[2, 1, 11, 1].Style.Numberformat.Format = "dd/MM/yyyy"; ws.Cells[2, 2, 11, 2].Style.Numberformat.Format = "#,##0.000000"; ws.Cells[2, 3, 11, 3].Style.Numberformat.Format = "#,##0"; …

SQL Server GROUPING SETS

Image
DECLARE @Products TABLE ( Product varchar(50), Category varchar(50), Price float) INSERTINTO @Products SELECT 'Sugar','Groceries',2.35 UNIONSELECT 'Bread','Groceries',1.09 UNIONSELECT 'Ecomonist','Magazines',4.99 UNIONSELECT 'FT','Magazines',2.99 SELECT Product,Category, SUM(Price) AS Price, COUNT(*) AS [Count] FROM @Products GROUPBY GROUPING SETS((Product),(Category),()) ORDERBY Product DESC, Category DESC, Price DESC, [Count] DESCSELECT Product,[Range], SUM(Price) AS Price,

C# Compare default(T) values

[TestFixture] publicclass TestDefaultValue { [Test] publicvoid Test() { Assert.IsTrue(Helper<string>.IsDefault(default(string))); Assert.IsTrue(Helper<string>.IsDefault(null)); Assert.IsFalse(Helper<string>.IsDefault("")); Assert.IsFalse(Helper<string>.IsDefault("hello")); } } publicclass Helper<T> { publicstaticbool IsDefault(T value) { if (EqualityComparer<T>.Default.Equals(value, default(T))) { returntrue; } returnfalse; } }