2007-09-25

FileInfo Class

Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files

Many of the FileInfo methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific FileInfo members such as Open, OpenRead, OpenText, CreateText, or Create.

Sample Code

Class FileManagement
Private m_pFile As IO.FileInfo
Private m_sTempFile As String = "C:\Test.Txt"


Public Sub SampleCreateFile()
m_pFile = New IO.FileInfo(m_sTempFile)

' Check exist file name
If Not m_pFile.Exists Then
Dim objStream As IO.StreamWriter = m_pFile.CreateText()

objStream.WriteLine("Welcome to My Blog")
objStream.WriteLine("Thank you")

objStream.Flush()
objStream.Close()
objStream.Dispose()
End If

End Sub

Public Sub SampleReadFile()
m_pFile = New IO.FileInfo(m_sTempFile)

If m_pFile.Exists Then
'Read Text
Dim objStream As IO.StreamReader = m_pFile.OpenText()

' Peek returns the next aviable charactor
While objStream.Peek() >= 0
Console.WriteLine(objStream.ReadLine)
End While

End If
End Sub

Public Sub SampleFileInfomation()

' Use class file to get file attributes

' Get Time
Console.WriteLine(IO.File.GetCreationTime(Me.m_sTempFile).ToLongTimeString())
Console.WriteLine(IO.File.GetLastAccessTime(Me.m_sTempFile).ToLongTimeString())
Console.WriteLine(IO.File.GetLastWriteTime(Me.m_sTempFile).ToLongTimeString())


' Set Time a
IO.File.SetCreationTime(m_sTempFile, DateTime.Now)
IO.File.SetLastAccessTime(m_sTempFile, DateTime.Now)
IO.File.SetLastWriteTime(m_sTempFile, DateTime.Now)

End Sub

Public Sub SampleOther()
m_pFile = New IO.FileInfo(m_sTempFile)

' Copy File
m_pFile.CopyTo("C:\test2.txt", True)

' Move File
m_pFile.MoveTo("C:\testMove.txt")

' Delete
m_pFile = New IO.FileInfo("C:\test2.txt")
m_pFile.Delete()
End Sub
End Class

2007-09-17

How to get table sizes for the database

sp_spaceused (Transact-SQL)

You can use this stored procedure (T-SQL) to get a report of the table sizes for the database.

Displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or SQL Server 2005 Service Broker queue in the current database, or displays the disk space reserved and used by the whole database.

Sample Code

1. One table
exec sp_spaceused [tableName]

2. All tables in your database

DECLARE @@TableName Nvarchar(100)

DECLARE my_cursor CURSOR FOR
Select sysobjects.name From sysobjects Where xtype='u' OPEN my_cursor

FETCH NEXT FROM my_cursor INTO @@TableName
WHILE @@FETCH_STATUS = 0
BEGIN

exec sp_spaceused @@TableName

FETCH NEXT FROM my_cursor INTO @@TableName
END
CLOSE my_cursor
DEALLOCATE my_cursor
GO


Result value

Column name Data type Description

name

nvarchar(128)

Name of the object for which space usage information was requested.

The schema name of the object is not returned. If the schema name is required, use the sys.dm_db_partition_stats or sys.dm_db_index_physical_stats dynamic management views to obtain equivalent size information.

rows

char(11)

Number of rows existing in the table. If the object specified is a Service Broker queue, this column indicates the number of messages in the queue.

reserved

varchar(18)

Total amount of reserved space for objname.

data

varchar(18)

Total amount of space used by data in objname.

index_size

varchar(18)

Total amount of space used by indexes in objname.

unused

varchar(18)

Total amount of space reserved for objname but no yet used.

2007-09-04

Database Engine Tuning Advisor

I found this solution in microsoft web site and it's can resolve the problem.

Error message when you use Database Engine Tuning Advisor in SQL Server 2005: "Attempt to initialize the CRT more than once


When you use Database Engine Tuning Advisor to analyze the performance effects of workloads in Microsoft SQL Server 2005, you may receive the following error message:

R6031- Attempt to initialize the CRT more than once. This indicates a bug in your application.

CAUSE
This problem occurs because of the application compatibility settings. The application compatibility settings may be set separately for the application. Alternatively, the application compatibility settings may be inherited from the settings that are specific to the Explorer.exe process. When you explicitly set the application compatibility settings for the Explorer.exe process, all applications that are started from the Explorer.exe process inherit the same application compatibility settings.

If Database Engine Tuning Advisor inherits the application compatibility settings from the Explorer.exe process, the Microsoft Windows loader tries to initialize the C run-time library (CRT) Msvcp80.dll file two times when you start Database Engine Tuning Advisor. Therefore, Database Engine Tuning Advisor stops responding.

Warning Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.

To work around this problem, you must set the Explorer.exe process to stop using the application compatibility technology. To do this, follow these steps:
1. Click Start, click Run, type regedit, and then click OK.
2. Locate the following registry subkey:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
3. In the right pane, right-click the registry entry for the Explorer.exe process, and then click Delete.
4. In the Confirm Value Delete dialog box, click Yes.
5. Exit Registry Editor.
6. Restart the computer.