Saturday, February 28, 2009

FORVO.com - All the words in the world, pronounced


Share/Bookmark

Thursday, February 26, 2009

T-SQL - DBCC SHRINKDATABASE

The following command uses to shrink data and log files in a database and releasing unallocated spaces:

DBCC SHRINKDATABASE
( database_name | database_id | 0
   [ , target_percent ]
   [ , { NOTRUNCATE | TRUNCATEONLY } ]
)
[ WITH NO_INFOMSGS ]

target_percent
   this parameter is the percentage of free (unallocated) space which you want left in the database file.

NOTRUNCATE
   Just relocate allocated spaces from end of the file to unallocated spaces in front of the file without shrinking and resizing the file.

TRUNCATEONLY
   it causes to release all free space at the end of the file.

WITH NO_INFOMSGS
   This option suppresses all informational messages.

Notes:
  • Database can not be smaller than initial size which you mentioned when you created the database.
  • DBCC SHRINKDATABASE without either the NOTRUNCATE option or the TRUNCATEONLY, runs first with NOTRUNCATE followed by running with TRUNCATEONLY.
  • During backup you can not shrink a database and during shrinking you can not back up a database.
  • Most effective time for shrinking a database is after an operation that creates lots of unused space like TRUNCATE or DROP TABLE

Disadvantage of shrinking a database is about increasing fragmentation.

Examples:
-- It shrinks and keeps 15 percent free space in the database
DBCC SHRINKDATABASE (myDataBase, 15);
GO

-- The following command, shrinks the database to the last allocated extent.
DBCC SHRINKDATABASE (myDataBase, TRUNCATEONLY)
GO

To shrink a database using SSMS, you can right click on database name and choose TASKS from context menu then select SHRINK and point on Database.
            Right click
Database -----------> TASKS -> SHRINK -> Database
Share/Bookmark

Wednesday, February 25, 2009

T-SQL - TRUNCATE and IDENTITY

If you want to delete all records in a table, the fastest way is through TRUNCATE. It will deallocate data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log. but "DELETE FROM tablename" removes records one by one.

Syntax: TRUNCATE TABLE tablename
  • When the table referenced by a FOREIGN KEY constraint, TRUNCATE TABLE is not applicable; instead, use DELETE statement without a WHERE clause.
  • TRUNCATE TABLE doesn't do log, then it cannot activate a trigger.
  • TRUNCATE TABLE will reset IDENTITY value to its base value then if you want to have another value for your IDENTITY enabled column, you need to call DBCC CHECIDENT

Share/Bookmark

Tuesday, February 24, 2009

T-SQL How to drop connections and detach a database?

sp_detach_db system stored procedure do the detaching job on a database from server, also It will run UPDATE STATISTICS on all tables before detaching.

Note: To be able to execute sp_detach_db you need to have right permission and only members of the sysadmin role can execute it.

sp_detach_db
   [@dbname =] 'dbname'
   [, [@skipchecks =] 'skipchecks']

[@dbname =] 'dbname'
It's the name of the database which you want to detach it, it's default value is NULL.
[@skipchecks =] 'skipchecks'
Default value is NULL.
If true, UPDATE STATISTICS will be skipped.
If false, UPDATE STATISTICS will run.
skipchecks data type is nvarchar(10)

If you want to move your database to a read onlu media, this option is useful

0 returns If the procedure executes with success
1 returns If the procedure encounter with error (failure)

An important matter is that, if there is a connection to database, you can't detach it, the following code will drop connections and the detach your database from server:

USE master
GO
ALTER DATABASE myDbName
   SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
EXEC sp_detach_db @dbname = 'myDbName', @skipchecks = 'true'
GO

Share/Bookmark

Sunday, February 22, 2009

T-SQL - DBCC CHECKIDENT

Set a column in a table as IDENTITY will guarantee to have unique value in that column which increment by inserting new records, then such a column is a good candidate to be PRIMARY KEY., but the question is, how to reseed the value or find what's the latest value now?

A table with a column which is set to IDENTITY can take advantage of DBCC CHECKIDENT.
The syntax for this command is as following:

DBCC CHECKIDENT ('table_name'[,{NORESEED |{RESEED[,new_reseed_value]}}])
  • NORESEED returns back the current identity value for the column.
  • RESEED with a value which comes after it with a comma, sets the column to a specific value and increment for the next inserted rows with predefined IDENTITY increment.
Example 1:
DBCC CHECKIDENT ( '[FBtst].[dbo].[Product]', NORESEED )

Output message:
Checking identity information: current identity value '101', current column value '101'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Example 2:
DBCC CHECKIDENT ( '[FBtst].[dbo].[Product]', RESEED, 200 )

Output message:
Checking identity information: current identity value '101', current column value '200'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Find more here:
http://technet.microsoft.com/en-us/library/ms176057.aspx
Share/Bookmark

100 meters long Photo - We are all gonna die!

This image is 100 meters long (100m x 78 cm)
http://www.simonhoegsberg.com/we_are_all_gonna_die/slider.html

178 people, 20 days, Berlin, Summer 2007

By Simon Hogsberg
http://www.simonhoegsberg.com/

Thanks Farzad (My brother) to send it to me.
Share/Bookmark

Wednesday, February 18, 2009

OpenOffice.org 3.0.1

Home page: http://www.openoffice.org/index.html
Download: http://download.openoffice.org/
Extensions: http://extensions.services.openoffice.org/
Download charts: http://marketing.openoffice.org/marketing_bouncer.html

Extensions by Application

Share/Bookmark

Tuesday, February 17, 2009

How to open a database connection to Microsoft SQL Server 2008 using the Microsoft Visual Studio 2005 design tools

When you try to open a connection with SQL Server 2008 through sever explorer in Visual Studio 2005 you will encounter with this error:
"This server version is not supported. Only servers up to Microsoft SQL Server 2005 are supported"

To fix it, you need to download and run VS80sp1-KB954961-X86-INTL.exe on your system:
Microsoft Visual Studio 2005 Service Pack 1 Update for Microsoft SQL Server 2008 Support
http://www.microsoft.com/downloads/details.aspx?FamilyID=e1109aef-1aa2-408d-aa0f-9df094f993bf&displaylang=en

To get some more info around it, take a look at the following page:
The Visual Studio 2005 CTP lets developers use Visual Studio 2005 together with SQL Server 2008
http://support.microsoft.com/default.aspx/kb/942246

Share/Bookmark

Sunday, February 15, 2009

T-SQL - Add a "NOT NULL" new column to table which has rows of data

When you want to add a new column to an existing table that has rows, in each row it expect a value for the new column, and the default value is NULL. If you try to add a new column which is NOT NULL, you will encounter with error.

The easiest solution is to alter the table and define the column to allow NULLs, then add in the default data values using the UPDATE T-SQL command, and next, alter the column to NOT NULL.

ALTER TABLE AdventureWorksLT.SalesLT.Customer
ADD temp int NULL

GO

UPDATE AdventureWorksLT.SalesLT.Customer
SET temp = 0

GO

ALTER TABLE AdventureWorksLT.SalesLT.Customer
ALTER COLUMN temp int NOT NULL

GO

Share/Bookmark

T-SQL - Random password generator

After creating a database in SQL server I created an
Application Role: DBname->Security->Roles->Application Roles

Then when I selected to create the script of that Application Role
in a new query Editor window, I found a part of code to generate a
random password, with some changes, I decided to write it here. To
run it, you can copy paste the code as a query in query editor window
of SSMS and execute the line of codes to see the output which is a
32 character length random generated password.

DECLARE @iCntr AS int
DECLARE @rndmPwd AS nvarchar(32)
DECLARE @rndm AS float

SET @iCntr = 0
SET @rndmPwd = N''

-- @@CPU_BUSY Shows the No. of busy CPU milliseconds
-- since the SQL Server instance was last started.
--
-- @@IDLE Displays the total idle time of the SQL
-- Server instance in milliseconds, since the
-- instance was last

SET @rndm =
   rand(
   ( @@CPU_BUSY % 100 ) +
   ( (@@IDLE % 100) * 100 ) +
   ( DATEPART(ss, GETDATE()) * 10000 ) +
   (
      (cast(DATEPART(ms,GETDATE()) as int) % 100) *
      1000000
   )
   )

WHILE @iCntr < 32
BEGIN
   SET @rndmPwd =
   @rndmPwd +
   char( ( cast( (@rndm * 83) as int ) + 43 ) )

   SET @iCntr = @iCntr + 1
   SET @rndm = rand()
END
PRINT @rndmPwd

Share/Bookmark

Friday, February 13, 2009

T-SQL - How to set a Database to be READ_ONLY?

When you create a database in SQL sever, you need to define a filegroup for it, then you can set it READ_WRIE or READ_ONLY

For example, if you have backup database that users can use to inspect data, and you don't need any update on it, then it would be an ideal case to set the database as READ_ONLY.

In this way, you can be sure that no updates can occur, so it has the security.

ALTER DATABASE [Your DataBase Name] SET READ_ONLY
GO


To make the database to be able to be updated, you can use the following line:

ALTER DATABASE [Your DataBase Name] SET READ_WRITE
GO

Share/Bookmark

Tuesday, February 10, 2009

Visual Studio - How to comment or uncomment current selection

CTRL + K, C or CTRL + E, C
It inserts // in front of current line or all the lines in current selection
Edit CommentSelection


CTRL + K, C or CTRL + E, C
It removes // in front of current line or all the lines in current selection
Edit UncommentSelection
Share/Bookmark

Monday, February 9, 2009

ASP.NET - How to add value to appSettings in Web.Config

We can save some data in Web.Config with a key and its related value, but it's important to know that this place is not our database to keep a lot of data there.

Configuration class make it able to add the key and value pair of data, then it saves them in pre-opened web configuration file.

In the following example, we have two textbox controls which user can enter the key and value and add them to the Web.Config file if they are not empty.

using System.Configuration;
using System.Web.Configuration;
...
  if(txtKey.Text == String.Empty ||
      txtVal.Text == String.Empty)
     return;

  Configuration cnfg =
     WebConfigurationManager.OpenWebConfiguration("~");
  cnfg.AppSettings.Settings.Add(txtKey.Text,txtVal.Text);
  cnfg.Save();
...

Share/Bookmark

Friday, February 6, 2009

C# - How to sort a DataTable?

One way to sort a DataTable is to set "Sort" property of DefaultView of a DataTable and then convert the sorted DataView to DataTable by .ToTable() method.

DataView.Sort Property
http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

DataView.ToTable Method
http://msdn.microsoft.com/en-us/library/a8ycds2f.aspx

Example:

DataTable myDT = new DataTable();
Random rndm = new Random();

myDT.Columns.Add("iValue", typeof(System.Int32) );
for (int i = 0; i < 10; i++)
{
   DataRow myDR = myDT.NewRow();
   myDR["iValue"] = rndm.Next();
   myDT.Rows.Add(myDR);
}

string sortExp = "iValue";
string sortOrder = "DESC";
myDT.DefaultView.Sort = sortExp + " " + sortOrder;
myDT = myDT.DefaultView.ToTable();


Share/Bookmark

Thursday, February 5, 2009

Formatting values using .ToString( string format )

One simple way to format a value is using .ToString( string format )

To get more info look at the following URLs and read the simple example:
Numeric Format Strings

double val1 = 24567.87094;
double val2 = 0.87094;
int iVal01 = 1839;
int iVal02 = 38;

double val = 0;
string str = string.Empty;

// Fixed-point "-ddd.ddd…"
val = double.Parse(val1.ToString("F")); // 24567.87
val = double.Parse(val1.ToString("F3")); // 24567.871

// Number "-d,ddd,ddd.ddd…"
val = double.Parse(val1.ToString("N")); // 24567.87
val = double.Parse(val1.ToString("N3")); // 24567.871

// Scientific (exponential)
// "-d.ddd…E+ddd" or "-d.ddd…e+ddd"
str = val1.ToString("E"); // 2.456787E+004
str = val1.ToString("e4"); // 2.4568e+004

// Round-trip
// supported only for the Single and Double types
// 17 digits of precision for a Double
// 9 digits of precision for a Single
val = double.Parse(val1.ToString("r")); // 24567.87094

// Percent
str = val2.ToString("P"); // 87.09 %

//Decimal - The precision specifier indicates
// the minimum number of digits
str = iVal01.ToString("D9"); // 000001839

// Hexadecimal
str = iVal02.ToString("X"); // 26

// Currency
str = val1.ToString("C"); // $24,567.87
str = val1.ToString("C3"); // $24,567.871

Share/Bookmark

Real time Satellite and space shuttle tracking

If you are interested to tracking a satellite or a space shuttle, or if you want to get latest news about them, take a look at the following URL:
http://www.n2yo.com

In above web site, you can find about:

  • Position of your home by its latitude and longitude
  • Live movement of your favorite satellite
  • Track up to 5 satellites on one map
  • Latest launches
  • Information about satellites
  • Finding satellites based on its category
  • Searching satellite in database by name, space command ID, Int'l designator, or lunch date

Share/Bookmark

Tuesday, February 3, 2009

Windows Internet Explorer 8 RC 1 now available!

The IE8 Release Candidate is now available for public download (since Jan 26, 2009).http://msdn.microsoft.com/en-ca/ie/default.aspx

There is a document which may be you can find answer to many of your questions, specially for developer, to download IE8 RC1 technology overview for developers go here and also take a look at IEBlog.
As Microsoft announced, there are many new features in this release around Reliability, Performance, Compatibility and Security. Let's see how does it work?
Share/Bookmark