C#
C++
SQL

Logging in automatically in Windows

On every media center PC I've used, most of the usage didn't involve a keyboard. With Windows Media Center Edition 2005 I mostly used the MCE remote control and with my new Windows Vista machine I use a touch screen.

One obstacle for getting the perfect setup on your media center is the logging in process. On Windows XP or Vista you'll have to click a Windows XP user icon (or enter a username) and enter the password (if applicable) to log in. I want to have a password to protect shared content but I don't want to have to actually log in or click the frog Windows XP user icon next to the name "Brian". I want my user account to automatically log in and have it run a startup script, open up commonly used programs, etc. There is a fix available for Windows XP / Windows Vista.

Use the Start Menu -> Run to pull up the run box (or on Vista, just pull up the start menu and click on the "start search") and type in "control userpasswords2". Hit enter and you will be presented with a "User Accounts" dialog. Simply uncheck the "Users must enter a user name and password to use this computer" box. When you click "Apply" or "OK", the dialog will prompt you with the credentials you want to use to automatically log in. Enter the user you would like to be automatically logged in and you're ready to go.

Labels: , ,

posted by Brian at | 0 Comments

 

SCOPE_IDENTITY

In SQL Server, there are three ways to get the identity of the row you had just inserted: IDENT_CURRENT(), @@IDENTITY, and SCOPE_IDENTITY().

IDENT_CURRENT() is not limited by scope and session; it is limited to a table. For example, you can use IDENT_CURRENT('TableName') to get the last identity generated for table 'TableName'.

SCOPE_IDENTITY() and @@IDENTITY will return last identity values generated in any table in the current session. However, only SCOPE_IDENTITY() returns values inserted within your current scope (the scope of the SQL you just executed). @@IDENTITY is dangerous to use in my opinion because even though it is limited to session, it doesn't take into account identities that could have been made by triggers. SCOPE_IDENTITY() is always safe to use after making your insert to get exactly the identity you need.

For more information, check out the official MSDN documentation:
http://msdn.microsoft.com/en-us/library/aa259185(SQL.80).aspx

Labels: ,

posted by Brian at | 0 Comments

 

Randomizing your selection in SQL Server

Sometimes you might want to randomize your dataset. In the SQL driven MP3 player I worked on, I wrote a stored procedure to handle putting together a playlist for the user (since all the song data is already in the database). There are probably a lot of other good uses for random selections and the code sample below works great.

In your select clause you can add an ORDER BY statement and use NEWID().
SELECT * FROM
TableName
ORDER BY NEWID()

Labels: ,

posted by Brian at | 0 Comments

 

Date formatting using MySQL

By default MySQL stores its dates in 'YYYY-MM-DD HH:MM:SS' format. When you have to integrate with .NET, it's nice to get the date in a format which is readable by DateTime.Parse().

In your SQL query, you can format the date by using the date_format function.
SELECT date_format(YourDateHere, '%a %D %b %Y') FROM TableName;
Here are some example formatting strings and examples of their output (replace the formatting in the red part of the query above).

ExampleFormat String
1/28/2008'%c/%e/%Y'
01/28/2008'%m/%d/%Y'
1/28/2008 12:30'%c/%e/%Y %H:%i'
01/28/2008 12:30'%m/%d/%Y %H:%i'
1/28/2008 12:30:59'%c/%e/%Y %T'
01/28/2008 12:30:59'%m/%d/%Y %T'

Labels: ,

posted by Brian at | 0 Comments

 

C++ String Stream

After using C++ for many years, I found myself still using the printf family of C functions for formatting. When I started my new job this last March, I noticed the other folks are big on using string streams. I've been trying to ween myself off sprintf and wanted to share some very simple examples.
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc,char** argv){
    ostringstream buffer;
    buffer << "Testing " << 1 << 2 << 3 << endl;
    cout << buffer.str();
    return(0);
}

This code will output:Testing 123
That code isn't too bad. If that's all there was too it, I wouldn't mind using the string streams. However, advanced formatting is not so easy. But of course you can always use defines to help.

Let's say that we want a program to display today's date in MM/DD/YYYY format. Using C, this is extremely trivial.
int main(int argc,char** argv){
    time_t time1 = time(0);

    struct tm* time2 = localtime(&time1);

    printf("Today's date is: %02d/%02d/%04d\n",time2->tm_mon,time2->tm_mday,(1900+time2->tm_year));
    return(0);
}

However, I don't think the code very elegant using string streams.
#include <iostream>
#include <sstream>
#include <time.h>
#include <iomanip>

using namespace std;

#define FormatInt(number,spaces) setw(spaces) << setfill('0') << int(number)

int main(int argc,char** argv){
    time_t time1 = time(0);

    struct tm* time2 = localtime(&time1);

    ostringstream buffer;
    buffer << "Today's date is: " << FormatInt(time2->tm_mon,2) << "/" << FormatInt(time2->tm_mday,2) << "/" << FormatInt(1900+time2->tm_year,4) << endl;
    cout << buffer.str();
    return(0);
}

If there is a better way to do this I'd love to hear about it. For now, I'll use string streams at work and continue using sprintf in code at home.

Labels:

posted by Brian at | 0 Comments

 

Bugzilla Install Guide for Windows

In December I installed Bugzilla on my file server to help track bugs and enhancements for all of my personal projects. Bugzilla is a pleasure to use, especially after years of using Mercury Quality Center at work. In my opinion it doesn't have any bloat and is easy to modify to your needs. The developers did a great job.
http://www.bugzilla.org/

Installing the program was not easy. But after an hour or so of beating my head on the desk I found an excellent Windows install guide by Byron Jones. The install guide gives an overview of locating and configuring Bugzilla, Perl, Apache, and MySQL.
http://www.bugzilla.org/docs/win32install.html

Labels:

posted by Brian at | 0 Comments

 

Bjarne Stroustrup: The creator of the C++ programming language

Last August I stumbled upon the personal website of Bjarne Stroustrup, the creator of the C++ programming language. I am a pretty big fan of his and it was great to read the articles and FAQ on his personal web site.
http://www.research.att.com/~bs/

I was surprised to see his email listed so I wrote him a quick message:
Hi Bjarne,

I found your homepage randomly today and wanted to let you know how
much I enjoy the C++ language. I own your book (which I've read
through many times) , "The C++ Programming Language", and I really
enjoy programming in C++.

This might be an odd request, but would I be able to get an
autographed photo of you? I would proudly display this in my office
everyday, especially if it had a silly line like "Brian, you'll never
be as good as me at C++ but thanks for buying my book - Bjarne".

I'm a 26 year old software engineer and my entire career was founded
on the knowledge I gained by becoming an expert at C++. If you were
up for that, it would mean a lot. If not, that's ok too. Thanks for
everything

Cheers
Brian

About a week later I was checking the mail and noticed a large envelope from TAMU
(click for larger).


"To Brian Clifton, All the best with C++"

Labels: ,

posted by Brian at | 0 Comments