麥克斯做個不宅的工程師

Maxi’s idiotic programming.

Auto increment column int MS SQL Server

I sometimes want to do this to create a field with auto-increment value to use as a sorting index. It is easy to achieve with MYSQL, I am going to show you how to achieve it with MS SQL 2008

Go to design view of your table, select the column which you want it to auto-increment. There will be a ‘Column Properties’ in the lower half of the designer. If you use Right-Click on the table and view its properties, there you can’t do any modification, which is strange to me. Anyway, go find ‘Identity Specification’ in ‘Column Properties’ located at the lower half of the designer.
Choose ‘Yes’ to (is identity)
Set ‘Identity Increment’ to some number, I usually use 1
Set ‘Identity Seed’ to some number, I usually use 1 too.
Save it, Then this column will generate auto-increment value when you insert new record to this table.

This is another very common things that people wants to do, IMHO.
enjoy it and cheers.

February 17, 2009 , Tuesday Posted by maxi326 | Server | , | No Comments Yet

Encrypt your password and save to database

There are two sets of functions provided by MS SQL Server to encrypt and decrypt your data. I am using MS SQL 2008, But I think 2005 provide the same functionality. What I want to do is insert my data into database in encrypted form and when I get it out, it is un-encrypt data. Kind of common thing people would like to do. For in-deep explaination, please see these great articles from 4GuysFromRolla (Article1, Article2), I always get great walk-through there. I am going to just use one from two sets because they are pretty much the same. And something different from most examples you’ve got from web resource, I am doing it in C# code, not T-SQL code. Because I really don’t like to use Store-Procedure. Wherever you insert your data with SqlCommand, use this to pass parameters.

//Instead of
sqlCmd.CommandText = "...@ColumnName...";
//Use this one
sqlCmd.CommandText = "...EncryptByPassPhrase(@PassPhrase,@ColumnName)...";

Wherever you want to get encrypted data out, do this

//Instead of
sqlCmd.CommandText = "...TableName.ColumnName...";
//Use this one
sqlCmd.CommandText = "...CONVERT(ColumnDataType, DecryptByPassPhrase(@PassPhrase, TableName.ColumnName) ) AS ColumnName)...";

PassPharse is a string that you want to use as a key to mask your data. That’s all, cheers.

February 17, 2009 , Tuesday Posted by maxi326 | Programming | , , | No Comments Yet

Add Javascript to page OnLoad event

I used to add start up javascript to a page dynamically by assigning it to body tag OnLoad attribute. But this approach will get in trouble because it runs before all other web elements are rendered. Also it will need to set runat=”server”.

ASP.NET provide a better approach and I discover it just recently.

In your code-behind

Page.ClientScript.RegisterStartUpScript(this.GetType(), "functionName", "TheScript", true);

This function will put your script at the end of that page and run after the page is completely loaded.

February 17, 2009 , Tuesday Posted by maxi326 | Programming | , , , | No Comments Yet