Gokul's Blog


Leave a comment

C# Task Library Parallel.Foreach

Sample Usage
var arrayStrings = new string[1000];
Parallel.ForEach<string>(arrayStrings, someString =>
{
    DoSomething(someString);
});


To set the Parallelism
var options = new ParallelOptions { MaxDegreeOfParallelism = 2 };
Parallel.ForEach(dtResult.AsEnumerable(), options, dr =>
		{
		//Do stuff
		});

 


Good Reads:
Advertisements


Leave a comment

Alter table for column Length

IF EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[MyTable]') AND type in (N'U'))
BEGIN
	--Increase the size of the [Name] column.
	IF EXISTS (SELECT name, TYPE_NAME(system_type_id), max_length, precision, scale
	FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.MyTable') AND name = 'ColName'
	AND max_length = 200)
	BEGIN
		PRINT '-- Increasing the size of the Name nvarchar column to 255.'
		ALTER TABLE MyDatabase.dbo.MyTable 
		ALTER COLUMN [ColName] nvarchar(255);
	END
END

MSDN Article: http://msdn.microsoft.com/en-us/library/ms190273(v=SQL.90).aspx


Leave a comment

Paging query helper C#

Code snippet which generates paging query 

int RecordCount = 3435354535647829;
int PageSize = 50000;
int MaxThreadCount = 5;
int PageSizebaseThreadSize = (RecordCount / MaxThreadCount) + 1;

PageSize = PageSizebaseThreadSize;

Console.WriteLine(PageSizebaseThreadSize);

int i = 0;
while(i i = i + PageSize;
}


Leave a comment

Generate XSD for a XML file

Original Link: http://www.gibmonks.com/c_sharp/csharpckbk2-CHP-15-SECT-13.html

[sourceode language=”csharp”]
public static void GenerateSchemaForDirectory(string dir)
{
// Make sure the directory exists.
if (Directory.Exists(dir))
{
// Get the files in the directory.
string[] files = Directory.GetFiles(dir, “*.xml”);
foreach (string file in files)
{
// Set up a reader for the file.
using (XmlReader reader = XmlReader.Create(file))
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schemaInference =
new XmlSchemaInference();

// Get the schema.
schemaSet = schemaInference.InferSchema(reader);

string schemaPath = “”;
foreach (XmlSchema schema in schemaSet.Schemas())
{
// Make schema file path.
schemaPath = Path.GetDirectoryName(file) + @”\” +
Path.GetFileNameWithoutExtension(file) + “.xsd”;
using (FileStream fs =
new FileStream(schemaPath, FileMode.OpenOrCreate))
{
schema.Write(fs);
}
}
}
}
}
}
[/sourcecode]


Leave a comment

Creating a log file from Stored Procedure

http://www.codeproject.com/KB/database/Stored_Procedure_Log_File.aspx


In this example I am going to show how to create log file for stored Procedure.

Log File will be created on Database Server.

Give write permission in c: drive or folder where to create the logfile.

Create a table Person

create table Person
(
PID int Primary Key identity (1, 1)
,Person_Name varchar(50)
,Person_Address varchar(150) not null
)

This is test table used in the stored procedure

create procedure CreateLog
(
@Msg varchar(500)
,@Start bit
)
as
begin
declare @cmd varchar(2000)
if(@Start = 1)
begin
set @cmd = ‘echo ————–‘+ convert(varchar(10),getDate(),101) +’——- ————— > C:\MyLog.txt’
exec master..xp_cmdshell @cmd
end

set @cmd = ‘echo ‘ + @Msg + ‘ >> C:\MyLog.txt’
exec master..xp_cmdshell @cmd
end

Now I am going to create a stored procedure in which i will insert one record and then update the Person_Address with null value which will throw error and i will record that in logfile.

create procedure PersonUpdate
(
@PID int
,@Address varchar(50)
)
as
begin
declare @LineNumber varchar(500)

begin transaction

insert into Person values (‘Mack’,’New York’)

if(@@error 0) goto ErrorHandler

exec CreateLog ‘Mack , New York inserted in Person table’,1

Update Person set Person_Address = @Address where PID = @PID

if(@@error 0) goto ErrorHandler

exec CreateLog ‘city has been update For give PID’,0

commit transaction
return
ErrorHandler:
begin
Rollback transaction
set @Msg = ‘Transaction Rollbacked, Error occured’
exec CreateLog @Msg,0
End
End

now execute the command

exec PersonUpdate 1,null

Now go to C:\MyLog.txt
and open this file messages are recorded here.