"We live in an exciting time. The Internet has become almost ubiquitous throughout much of the world, bringing with it freedom of information and an unprecedented power to all. My passion is seeing businesses leverage that power effectively and economically." - Dave Ranck

Archive for C#

May
19

Find a String Between 2 Strings

Posted by: Dave Ranck | Comments (0)

Here’s a way to return a substring of string that is between 2 strings in C#. There are other options for doing this, such as RegEx, but this is clean and simple.

// This search returns the substring between two strings, so 
// the first index is moved to the character just after the first string.
int first = str.IndexOf("$string1$") + "$string1$".Length;
int last = str.LastIndexOf("$string2$");
string str2 = str.Substring(first, last - first);
 
Technorati Tags: ,,,

Technorati Tags: C#, Code, Code Sample, Visual Studio

May
17

Restore SQL Database

Posted by: Dave Ranck | Comments (0)

Restoring a SQL Server database from a backup is easy to accomplish in the SQL Server IDE. Well, sometimes. In the real world, I often need to restore a development database, but the restore fails because I cannot gain exclusive access to the database. Some developers will detach the database and reattach it to grab exclusive access. If there are processes running frequently against the database, that may not work. Neither will killing all connections always work. The solution I’ve found is a short snippet of SQL to grab exclusive access, do the restore and then restore multi-user access. It looks like this:

USE master — Must be connected to a different database

-- Get exclusive access
ALTER DATABASE XXXXXX_Stage  SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

-- Restore from disk
-- Note WITH REPLACE http://msdn.microsoft.com/en-us/library/ms178615.aspx

RESTORE DATABASE XXXXXX_Stage  FROM DISK = 'F:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\XXXXXX_Stage _20100505.bak' WITH REPLACE

-- Restore multi user access
ALTER DATABASE XXXXXX_Stage SET MULTI_USER WITH ROLLBACK IMMEDIATE;

Note the WITH ROLLBACK clauses. You should take a production database down and make sure all transactions have completed. Then do a restore through the IDE. If you use the code above, you may lose some transactions or logs. This usually doesn’t matter in a test environment.

Have fun!

Technorati Tags: ,,,,,

Technorati Tags: Code, Code Sample, Database, SQL, SQL Server

May
10

Create Singleton in C#

Posted by: Dave Ranck | Comments (0)

One of the most basic and most useful object patterns is the Singleton design pattern. A Singleton is a class of which there will only be one instance created at any time. All users of the Singleton class will all use the same instance. This is great for a wide range of applications such as cached values and lookups or utilities that execute quickly. The Design Patterns book by Gamma et al . describes the structure of a basic Singleton. We’ll describe how to create one in C#.

Read More→

Technorati Tags: Code, Design, Programming, Software Development, Tips, Visual Studio

May
05

Title Case in C#

Posted by: Dave Ranck | Comments (0)

Here a quick snippet to convert text to Title Case in C# that uses the TextInfo class. Note that strings with all caps will not be converted, so convert to lower case first.

// Defines the string with mixed casing.
string myString = "this iS a StrIng with MIXED CaSe";

// Create a TextInfo based on the "en-US" culture.
TextInfo tInfo = new CultureInfo("en-US", false).TextInfo;

// Changes a string to titlecase.
// Note the ToLower(), needed to convert words in all caps like MIXED
Console.WriteLine("\"{0}\" to titlecase: {1}", myString, tInfo.ToTitleCase(myString.ToLower()));

//Result: This Is A String With Mixed Case
Console.ReadLine();

// Defines the string with mixed casing.

string myString = “this iS a StrIng with MIXED CaSe”;

// Create a TextInfo based on the “en-US” culture.

TextInfo tInfo = new CultureInfo(“en-US”, false).TextInfo;

// Changes a string to titlecase.

// Note the ToLower(), needed to convert words in all caps like MIXED

Console.WriteLine(“\”{0}\” to titlecase: {1}”, myString, tInfo.ToTitleCase(myString.ToLower()));

//Result: This Is A String With Mixed Case

Console.ReadLine();

Sponsored By :

 

 

Material in this site unless otherwise noted is Copyright David Ranck 2009, 2010