SQL
Shut down system using C#
by admin on Nov.10, 2009, under SQL, c#
This code shut downs the operating system using the System.Management assembly, but not before obtaining the required security privileges.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// Remember to add a reference to the System.Management assembly
using System.Management;
namespace ShutDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShutDown_Click(object sender, EventArgs e)
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass(“Win32_OperatingSystem”);
mcWin32.Get();
// You can’t shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters(“Win32Shutdown”);
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = “1″;
mboShutdownParams["Reserved"] = “0″;
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod(“Win32Shutdown”, mboShutdownParams, null);
}
}
}
}
SQL SERVER – Simple Example of Cursor
by admin on Jan.05, 2009, under SQL
This is the simplest example of the SQL Server Cursor. I have used this all the time for any use of Cursor in my T-SQL.
DECLARE @Channel1 INT
DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR
SELECT top 10 Channel1
FROM PortData
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @Channel1
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Channel1
FETCH NEXT
FROM @cursor INTO @Channel1
END
CLOSE @cursor
DEALLOCATE @cursor
Remove Unwanted characters
by admin on Dec.30, 2008, under SQL
CREATE FUNCTION dbo.strim (@textin VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @textOut VARCHAR(8000)
SELECT @textOut=”
SELECT @textOut=@textOut+letter
FROM
(
SELECT TOP 8000 i, SUBSTRING(p,i,1) AS LETTER
FROM Tally, (SELECT @textin AS P) A
– BEGIN CALLOUT A
WHERE (ASCII(SUBSTRING(p,i,1)) BETWEEN 48 AND 57)
OR (ASCII(SUBSTRING(p,i,1)) BETWEEN 65 AND 90)
OR (ASCII(SUBSTRING(p,i,1)) BETWEEN 97 AND 122)
– END CALLOUT A
ORDER BY i
) D
RETURN(@textout)
END
SELECT TOP 8000 IDENTITY(INT,1,1) AS i INTO Tally FROM
master.dbo.sysobjects a, master.dbo.sysobjects b
Useful undocumented extended stored procedures
by admin on Dec.24, 2008, under SQL
An extended stored procedure (xp) is a dynamic link library that runs directly in the address space of SQL Server and is programmed using the SQL Server Open Data Services API. You can run extended stored procedures from the Query Analyzer, for example, just as you would normal stored procedures. Extended stored procedures are used to extend the capabilities of SQL Server. You can take advantage of the many extended stored procedures that come with SQL Server, or you can write your own in a programming language such as C or C++.
In this article, I want to tell you about some useful undocumented extended stored procedures. These extended stored procedures work with SQL Server 7.0, as well as with SQL Server 2000.
sp_MSgetversion
This extended stored procedure can be used to get the current version of Microsoft SQL Server. To get the current SQL Server version, run
EXEC master..sp_MSgetversion
Note. A more common way to retrieve the current SQL Server version (this way provides more information) is to use following SELECT statement:
SELECT @@version
xp_dirtree
This extended stored procedure can be used to get a list of all the folders for the folder named in the xp. To get a list of all the folders in the C:\MSSQL7 folder, run:
EXEC master..xp_dirtree ‘C:\MSSQL7′
xp_enum_oledb_providers
This extended stored procedure is used to list of all the available OLE DB providers. It returns Provider Name, Parse Name and Provider Description. To get a list of all OLE DB providers for your SQL Server, run:
EXEC master..xp_enum_oledb_providers
xp_enumcodepages
This extended stored procedure can be used to list of all code pages, character sets and their description for your SQL Server. To get a list of all code pages and character sets, run:
EXEC master..xp_enumcodepages
xp_enumdsn
This extended stored procedure returns a list of all System DSNs and their description. To get the list of System DSNs, run:
EXEC master..xp_enumdsn
xp_enumerrorlogs
This extended stored procedure returns the list of all error logs with their last change date. To get the list of error logs, run:
EXEC master..xp_enumerrorlogs
xp_enumgroups
This extended stored procedure returns the list of Windows NT groups and their description. To get the list of the Windows NT groups, run:
EXEC master..xp_enumgroups
xp_fileexist
You can use this extended stored procedure to determine whether a particular file exists on the disk or not.
Syntax:
EXECUTE xp_fileexist filename [, file_exists INT OUTPUT]
For example, to check whether the file boot.ini exists on disk c: or not, run:
EXEC master..xp_fileexist ‘c:\boot.ini’
xp_fixeddrives
This very useful extended stored procedure returns the list of all hard drives and the amount of free space in Mb for each hard drive.
To see the list of drives, run:
EXEC master..xp_fixeddrives
xp_getnetname
This extended stored procedure returns the WINS name of the SQL Server that you’re connected to.
To view the name, run:
EXEC master..xp_getnetname
xp_readerrorlog
This extended stored procedure returns the content of the errorlog file. You can find the errorlog file in the C:\MSSQL7\Log directory, by default for SQL Server 7.0.
To see the text of the errorlog file, run:
EXEC master..xp_readerrorlog
xp_regdeletekey
This extended stored procedure will delete an entire key from the registry. You should use it very carefully.
Syntax:
EXECUTE xp_regdeletekey [@rootkey=]'rootkey',
[@key=]'key'
|
For example, to delete the key ‘SOFTWARE\Test’ from ‘HKEY_LOCAL_MACHINE’, run:
EXEC master..xp_regdeletekey
@rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Test'
|
xp_regdeletevalue
This extended stored procedure will delete a particular value for a key in the registry. You should use it very carefully.
Syntax:
EXECUTE xp_regdeletevalue [@rootkey=]'rootkey',
[@key=]'key',
[@value_name=]'value_name'
|
For example, to delete the value ‘TestValue’ for the key ‘SOFTWARE\Test’ from ‘HKEY_LOCAL_MACHINE’, run:
EXEC master..xp_regdeletevalue
@rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Test',
@value_name='TestValue'
|
xp_regread
This extended stored procedure is used to read from the registry.
Syntax:
EXECUTE xp_regread [@rootkey=]'rootkey',
[@key=]'key'
[, [@value_name=]'value_name']
[, [@value=]@value OUTPUT]
|
For example, to read into the variable @test from the value ‘TestValue’ from the key ‘SOFTWARE\Test’ from the ‘HKEY_LOCAL_MACHINE’, run:
DECLARE @test varchar(20) EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key='SOFTWARE\Test', @value_name='TestValue', @value=@test OUTPUT SELECT @test |
xp_regwrite
This extended stored procedure is used to write to the registry.
Syntax:
EXECUTE xp_regwrite [@rootkey=]'rootkey',
[@key=]'key',
[@value_name=]'value_name',
[@type=]'type',
[@value=]'value'
|
For example, to write the variable ‘Test’ to the ‘TestValue’ value, key ‘SOFTWARE\Test’, ‘HKEY_LOCAL_MACHINE’, run:
EXEC master..xp_regwrite
@rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Test',
@value_name='TestValue',
@type='REG_SZ',
@value='Test'
|
xp_subdirs
This extended stored procedure is used to get the list of folders for the folder named in the xp. In comparison with xp_dirtree, xp_subdirs returns only those directories whose depth = 1.
This is the example:
EXEC master..xp_subdirs ‘C:\MSSQL7′
Note.Keep in mind that these undocumented extended stored procedures are not officially supported by Microsoft, and that they may not be found in the next version of SQL Server.