Wednesday, November 08, 2006

Get Domain name of the system

System Name or NetBIOS Name

//Retrieve the NetBIOS name.
System.Environment.MachineName;

System.Net.Dns.GetHostName();


DNS name or Fully Qualified name

//It retrieves the domain name of the current user
System.Windows.Forms.SystemInformation.UserDomainName;

System.Environment.GetEnvironmentVariable("USERDNSDOMAIN"));

//Retrieve the DNS name of the computer.
System.Net.Dns.GetHostByName("LocalHost").HostName;

//WMI Script to retireve only domain name of current computer
ManagementPath path = new ManagementPath();
path.Server = System.Net.Dns.GetHostName();
path.NamespacePath = @"root\CIMV2";
path.RelativePath = "Win32_ComputerSystem.Name='" + path.Server + "'";
System.Management.ManagementObject o = new ManagementObject(path);
Console.WriteLine(o["Domain"]);

//Read domain and host name from windows registry in VB.NET
Dim rk As RegistryKey = Registry.LocalMachine
Dim rkSub As RegistryKey
rkSub = rk.OpenSubKey("SYSTEM\CurrentControlSet\Services\Tcpip\Parameters")
If rkSub Is Nothing Then
MessageBox.Show("Program error, invalid registry key", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error)
Exit Sub
End If
mDomain = CType(rkSub.GetValue("Domain"), String)
mHostName = CType(rkSub.GetValue("HostName"), String)
If mDomain.Length > 0 Then
mFullHostName = mHostName & "." & mDomain
Else
mFullHostName = mHostName
End If

Monday, November 06, 2006

T-SQL Script to kill users of DB

Kill Users of DB

DECLARE @SPID smallint
DECLARE @Loginame varchar(32)
DECLARE @KILLSTATE varchar(32)

-- CREATE TEMPORARY TABLE FOR SP_WHO TO POPULATE
CREATE TABLE #usrkill ( spid smallint,
ecid int,
status varchar(32),
loginame varchar(32),
hostname varchar(32),
blk char(8),
dbname varchar(32),
cmd varchar(255)
)

-- INSERT INTO TEMP TABLE
INSERT #usrkill exec sp_who

-- BEGIN CURSOR
DECLARE KILL_USERS CURSOR FOR
SELECT SPID, Loginame from #usrkill where UPPER(dbname) IN ('MyDB1','MyDB2')
OPEN KILL_USERS
FETCH NEXT FROM KILL_USERS into @spid, @Loginame
IF @@FETCH_STATUS <> 0 GOTO Last_User_Record
WHILE @@FETCH_STATUS = 0
BEGIN
Print 'Killing User: ' + @Loginame
select @KILLSTATE = 'KILL ' + CONVERT(char, @spid)
exec(@KILLSTATE )
FETCH NEXT FROM KILL_USERS into @spid, @Loginame
END
Last_User_Record:
CLOSE KILL_USERS
DEALLOCATE KILL_USERS

-- CLEAN UP TEMPDB
DROP TABLE #usrkill

The above T-SQL statements will all kill the users of the DB MyDB1 and MyDB2. It uses the sp_who proceudre which returns all the users of DB.

T-SQL Script to find n/w port of SQL Server

T-SQL Script


set nocount on
DECLARE @test varchar(20), @key varchar(100)
if charindex('\',@@servername,0) <>0
begin
set @key = 'SOFTWARE\MICROSOFT\Microsoft SQL Server\'
+@@servicename+'\MSSQLServer\Supersocketnetlib\TCP'
end
else
begin
set @key = 'SOFTWARE\MICROSOFT\MSSQLServer\MSSQLServer \Supersocketnetlib\TCP'
end

EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key=@key,@value_name='Tcpport',@value=@test OUTPUT

SELECT 'Server Name: '+@@servername + ' Port Number:'+convert(varchar(10),@test)


The above T-SQL reads the SQL Server Port number from the registry for the current SQL Server instance.

T-SQL to list all the user tables and row counts in a DB

SELECT distinct B.[name] as [Table Name], max(A.rowcnt) as [Row Count]FROM sysindexes AINNER JOIN sysobjects BON A.id = B.idWHERE B.type = 'U'GROUP BY B.[name]ORDER BY B.[name]

The above query returns all the user tables and the count of all rows in the current DB.