Code Camp NYC 2016 - MongoDB for Developers

October 8, 2016 by Ben Galluzzo    .NET, C#, Events, SQL, NoSQL, MongoDB, SSIS, Code Camp NYC |    Comments (0)

Code Camp NYC 2016 Presentation

MongoDB For Developers And Accessing MongoDB data

This is where .NET, MongoDB, and SSIS meet. We'll begin with an overview of what MongoDB and NoSQL are and how it is often used in the world. Then, we'll go over some of terminology of MongoDB and how these terms map to the SQL world. Lastly, we'll demonstrate how we can work with MongoDB from SSIS using .NET code to make all of the connections.

CodeCampNYC_2016_MongoDB_Intro.pptx (1.05 mb)

Code Camp NYC

 

Hide Tab Header of Tab Control

November 7, 2012 by Ben Galluzzo    .NET, C#, WinForm |    Comments (0)

There are multiple ways in which the tab header of a tab control can be hidden.  One way is to set the DrawMode of the control to "OwnerDrawFixed," the SizeMode to "Fixed, " and then set the ItemSize to "0, 1"  What's left is somewhat of an abandoned tab header.  A little bit of fussing with the layout is then required to clean up the tab control.  

Another method is to extend the WinForm tab control and override the WndProc method to trap the TCM_ADJUSTRECT message, which is sent when it needs to adjust the size of the tabs, while the control is in DesignMode.  This allows for a nice way of completely eliminating the tab header from the control without any need to clean up the layout.  Once you trap the TCM_ADJUSTRECT message, return the value 1 to specify that the message was handled.

        if (_message.Msg == TCM_ADJUSTRECT && !DesignMode)
            _message.Result = (IntPtr)1;

 

While you're at it, put the class inside your custom library and add it the Visual Studio Toolbox so it can dragged onto a form whenever it is needed. 

using System;

using System.Windows.Forms;

namespace StoreLib
{
    public class NoHeaderTabControl : TabControl
    {
        private const int TCM_ADJUSTRECT = 0x1328;

        protected override void WndProc(ref Message _message)
        {
            if (_message.Msg == TCM_ADJUSTRECT && !DesignMode)
                _message.Result = (IntPtr)1;
            else
                base.WndProc(ref _message);
        }
    }
}

 

References

Control.WndProc Method
TCM_ADJUSTRECT message (Windows)

Reading and Writing to an INI File in Managed Code

August 13, 2012 by Ben Galluzzo    .NET, C#, Quick-and-Dirty |    Comments (0)

Due to the push for the adoption of XML configuration files, INI file handling was not built into the .NET Framework.  However, INI files do still exist in legacy applications and can sometimes be found in newer software as well.  So there needs to be a way to work with them from code.  There are some available libraries, such as Nini and CommonLibrary.NET, which can perform this type of work, however, since this is part of the Quick-and-Dirty Series, here's a very simple example that will work for a lot of scenarios.  

Interop Services to the rescue

Two kernel32.dll functions can be exposed to handle these INI files; WritePrivateProfileString and GetPrivateProfileString.  The class IniFile listed below shows how to utilize these functions from Kernel32 via interop.

using System;
using System.Text;

using System.Runtime.InteropServices;

namespace StoreLib.Configuration.Ini
{
    public class IniFile
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string _section, string _key, string _val, string _filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string _section, string _key, string _def, StringBuilder _retVal, int _size, string _filePath);

        public IniFile(string _path)
        {
            Path = _path;
        }

        //Write to INI File
        public void WriteValue(string _section, string _key, string _value)
         {
            WritePrivateProfileString(_section, _key, _value, Path);
        }

        //Read from INI File
 public string ReadValue(string _section, string _key)
 {
 StringBuilder sb = new StringBuilder(255);
 int i = GetPrivateProfileString(_section, _key, "", sb, 255, Path);

 return sb.ToString();
 }
        public string Path { get; set; }
    }
}

 

Once the IniFile class is ready, reading a value is easy:

IniFile iniFile;
iniFile = newIniFile(@"C:\LegacyApplication\la.ini");

string dataSourceKey = "source1";
string dataSourceName = iniFile.ReadValue("DataSources", dataSourceKey);

 

Similarly, writing a value to the file is just as easy:

IniFile iniFile;
iniFile = newIniFile(@"C:\LegacyApplication\la.ini");

string appLoadKey= "splash";
string appLoadValue = "logo";
iniFile.WriteValue("AppLoad", appLoadKey, appLoadValue);

 

That's all there is to it.  This is just one option for accessing an INI file and should be able to handle most instances of a section's simple key-value pair.  Feel free to give it a try.

                                                                                                                                          ___________

Quick-and-Dirty Series - Hopefully still a sound solution, these may not be the best, fastest, or most appropriate methods of accomplishing a development task, however, they work and are fast to put in place.

Setting Up Debug Mode for a Windows Service Project

January 14, 2011 by Ben Galluzzo    .NET, Debugging, Windows Service, C# |    Comments (0)

If you’ve tried to run a Windows Service project in debug mode, you’ve probably seen the message shown below.  To debug our code by default, we would have to install the service and then attach the debugger to a Process (Ctrl+Alt+P).  Obviously, this can get tedious.  There is a more appropriate and efficient way to debug the Windows Service project in which we’re developing.

DebugModeError_489x199

 

 

 

 

 

 

Let’s start out by constructing a simple Windows Service…  Add a Windows Service class item to the project; in our case, the class is named AgentService

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.Timers;

namespace ProvingGrounds.Agent
{
    partial class AgentService : ServiceBase
    {
        private Timer timer = null;

        public AgentService()
        {
            InitializeComponent();

            timer = new Timer(6000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            timer.AutoReset = true;
        }

        protected override void OnStart(string[] args)
        {
            Start();
        }

        protected override void OnStop()
        {
            Stop();
        }

        public void Start()
        {
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //TODO:Do Something
        }
    }
}

 

Next, update Main() found in Program.cs to execute AgentService.

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AgentService() 
			};
            ServiceBase.Run(ServicesToRun);
        }

We should now have our Windows Service constructed. Feel free to compile it and then go ahead try running it in debug mode. You should be receiving our "Start Failure" error message.

Next, let's get this thing running in debug mode from Visual Studio. We can do this by adding the #if DEBUG preprocessor directive to our Main() function. We simply wrap the existing call to our AgentService with the conditional preprocessor directives.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

using System.Threading;

namespace ProvingGrounds.Agent
{
    static class Program
    {
        static void Main()
        {
            #if DEBUG
            
            AgentService agentService = new AgentService();
            agentService.Start();

            Thread.Sleep(Timeout.Infinite);

            agentService.Stop();

            #else

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AgentService() 
			};
            ServiceBase.Run(ServicesToRun);
            
            #endif
        }
    }
}

 

That's all there is to.  Go ahead and hit F5 from Visual Studio and your Windows Project should be running in Debug Mode.


SQL Saturday - 506 - Baltimore BI Edition

Month List