Convert a DateTime Object in a SQL field

Categories: C, MySQL

Tags:

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

If we want to insert a date in a SQL string in C#, the solution seems obvious:

DateTime date = DateTime.Now;
string sql = "INSERT INTO tableWithDate VALUES(1, "+ date.ToString() + ")";

Unfortunately this code can’t work! The function DateTime.ToString() returns a string like 23/02/2007 19.00.49, while MySQL wants a string like 2007-02-23 19:00:49. The following static function converts a DateTime object in a valid date field for MySQL:

public static string GetSQLDate(DateTime date)
{
	string sql;
	sql = date.Year.ToString() + "-" + date.Month.ToString() + "-" + date.Day.ToString() + " "
	+ date.Hour.ToString() + ":" + date.Minute.ToString() + ":" + date.Second.ToString();
	return sql;
} 

I inserted the method in a utility class, that i called Utils, so the working code is:

string sql = "INSERT INTO tableWithDate VALUES(1, "+ Utils.GetSQLDate(date) + ")";

Deprecated methods in C#

Categories: C

Tags:

A deprecated method is a method that is no more supported. For example, you wrote an API with a function that saves something on the disk but in the future versions of your program you want to support only Database saves. In this case you can deprecate the method that saves to disk for a lot of versions: the programmers are now warned from the compiler every time they use the function, and have the time to migrate the code.

stop

If you want to deprecate a method in c#, you can simply add the Obsolete attribute in the function.

[Obsolete("This is a deprecated method.")]
public void DeprecatedMethod()
{
	MessageBox.Show(”I’m a deprevated method! XD”);
}

Continue Reading »

How to save a SQL Blob in C#

Categories: C, MySQL

Tags:

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

Today at work i had my daily dilemma: how can i save a blob field in a MySQL DB with c#??? The solution it’s pretty simple, but it can be hard to find.

The Blob is a binary field where data can be saved: the problem is that we must write these binary data in a textual SQL string. To resolve this problem we can convert the binary data in an hexadecimal representation in order to write the data in a textual format.

Binary data

The resulting SQL will look like that:

INSERT INTO tableWithBlob VALUES(1,0xab425fa52d3e13);

Where 0x… are our binary data.

To start, we must first convert the data structure that we want to put in the database in a byte array (byte[]), or in a format in which we can access all structure’s bytes.
Continue Reading »

C++ Timer Class

Categories: C, Source Code, Windows

Tags:

Starting from the article “C++ timer”, we created a simply class that act as a wrapper to the Windows API and that easily allows to start a timer, stop it. and assign the function to call when the timer is elapsed.

Timer

The following is a little example that explains how to use the class:
Continue Reading »

How to create Stored Procedures in MySQL 5.0

Categories: MySQL

Tags:

Stored Procedures are a way to re-use and parametrize long SQL queries.
To create a Stored Procedure you need at least MySql 5.0. Using MySQL Query Browser the stored procedure creation is very quick:

Right click on the database where do you want to create the procedure (from version 5.0.1 procedures are assigned to a specific database, instead in the 5.0 version the procedures are globals)

New Stored Procedure From QueryBrowser

Continue Reading »

Detect when an usb device is inserted or removed.

Categories: C, Source Code, Windows

Tags:

Normally the autorun in usb storage devices like pendrives is disabled for security reasons, so i wrote a little utility in c++ to launch a custom application in my pendrive when I insert it in a usb port.

Logo usb

Normally if you want to detect when a “hotplug” device has been inserted or removed from your system, you have to intercept the system message “WM_DEVICECHANGE” in “WndProc” function of the main window of your application.
Continue Reading »

C++ Timer

Categories: C, Windows

Tags:

To create a timer in c++ on Windows we must invoke the SetTimer() method, defined in windows.h.

UINT_PTR SetTimer(
// handle to the window associated to the timer
HWND hWnd,
// Timer ID
UINT_PTR nIDEvent,
// Elapse time in milliseconds
UINT uElapse,
// callback method to invoke when the timer expires
TIMERPROC lpTimerFunc
);

The hWnd argument is the handle to a window: for semplicity we don’t use any Handle, so we set it to NULL.

The function always returns an uint that represents the ID of the timer. if hWnd is NULL, the second parameter will be ignored and we can’t directly set the timer ID, but it will be assigned from the function. We can check if the creation of the timer didn’t have any problem if the uint returned from the function it’s not zero. Instead, if hWnd is not null, the timer ID will be assigned from the nIDEvent parameter

Add a global variable (ok, it’s not a good programming pratice adding global variabile, but this is only an howto) where we can save the timer ID:

uint m_timerID;

Continue Reading »

Optimized Access to Bitmaps

Categories: C, Source Code

Tags:

If we need image processing in c#, the fastest way for a programmer to access the pixels of a Bitmap is to use the SetPixel() and GetPixel() .NET methods. Unfortunately the fastest way for a programmer, often does’nt correspond to the fastest way for the program: These functions are REALLY slow, so if your objective is to analyze all pixels of an image, or, still worse, to make an elaboration of many images, you are not adviced to use them. Fortunately the c# is a very language flexible: It allows to make things at high levels, but often gives the opportunity to do low-level programming when is necessary.

For semplicity, in this article i will deal only the images at 8bpp (8 bit per pixel): every byte will be a pixel.

The only way to work fast with an image is to use the pointer to the image data. Work with the pointers is like working with the char * in c++: Initialliy, the pointer will point to the memory address of the first pixel of the bitmap. Increasing the pointer we would gradually obtain the values of the next pixels.
Pointer to image example
Continue Reading »

CRC in C#

Categories: .NET, C, Source Code

Tags:

This is a post about the CRC (Cyclic Redundancy Check) in C#. I searched through the internet a C# class that can calculate the crc-12, but I found only links about crc-8, crc-16 and crc-32.

binary matrix

After an hard search, i founded a class that can calculate the crc from 1 to 32 (this is the link), I cleaned it and I added:

  • The crc-12 standard
  • A class that simplifies the insert of the values (CRCSettings)
  • A function that generate non-standard polynomys.

Continue Reading »

Problems with comments using Redoable theme in WP

Categories: Wordpress, php

Tags:

If you have problems in the comments using Redoable theme, you can try this trick(i hope that this post can help someone :) ).

Open the file single.php and modify the following lines at the end of the file from:

</div>
<div id=”rightcolumn”>
<?php get_sidebar(); ?>
</div>
<div class=”clear”></div>
</div> <!– .content –>
<?php comments_template(); ?>
<?php get_footer(); ?>

To:

</div>
<?php comments_template(); ?>
<div id=”rightcolumn”>
<?php get_sidebar(); ?>
</div>
<div class=”clear”></div>
</div> <!– .content –>
<?php get_footer(); ?>

I don’t know why, but invoking comments_template() after get_sidebar() the comments will not display right.

« Prev - Next »