VS.NET C# Code Snippets RSS Feed http://www.dotnetfun.com/CodeSnippets.aspxVS.NET C# Code Snippets from C# .Net Fun (dotnetfun.com).en-usAdd Record via Stored Procedure to SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Add Record via Stored Procedure to SQL Server DatabaseAdd Record via Stored Procedure to SQL Server Database12/14/2005 4:28:23 PMConverting an Object Array to a Double Array using a Converter.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Converting an Object Array to a Double Array using a ConverterDemonstrates how to convert an Object[] array to a double[] array.12/20/2005 3:51:16 PMDeclaring Readonly and Constant Variables.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Declaring Readonly and Constant VariablesDeclaring Readonly and Constant Variables.12/14/2005 4:28:24 PMDeclaring Static Classes.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Declaring Static ClassesDeclaring Static Classes12/14/2005 4:28:26 PMDeclaring Static Constructors.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Declaring Static ConstructorsDeclaring Static Constructors12/20/2005 1:52:39 PMDelete Unique Record via Stored Procedure in SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Delete Unique Record via Stored Procedure in SQL Server DatabaseDelete Unique Record via Stored Procedure in SQL Server Database12/14/2005 4:28:29 PMDetermining the Number of Indices of a Substring within a String.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Determining the Number of Indices of a Substring within a StringDetermining the Number of Indices of a Substring within a String12/15/2005 12:17:22 PMEmpty Code Snippet Template.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Empty Code Snippet Template2/8/2006 4:54:25 PMEncrypting and Decrypting DataSets.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Encrypting and Decrypting DataSetsProvides two methods for encrypting and decrypting data from a single instance of a DataSet. Calling EncryptDataSet() will cause three files to be generated: 1) a serialized jagged byte array holding the DES key and vector, 2) the encrypted DataSet instance's data and 3) the DataSet instance's schema. Calling DecryptDataSet() will read the preceding three files: 1) it will deserialize the DES key and vector, 2) decrypt and load into the DataSet instance the data and 3) load into the DataSet instance the schema. Note that these two methods will work with one instance of a DataSet. If you call EncryptDataSet() with several instances, you may encounter problems. Therefore, the preceding methods are designed to deal with one instance of a DataSet. If you require encryption and decryption for several DataSet instances then you should pass the methods unique file names for each one accordingly. Please note that this code is the most basic employed. You should implement try-catch blocks as well as conditional checks for the existence of the various files being read and written to. Also, be aware that if you intend to decrypt the DataSet, you must have present all three of the files discussed above. So, for example, if you plan on encrypting the DataSet on computer A and decrypting it on computer B, make sure the files generated are copied to computer B before decryption occurs (ensuring that the paths to the files are properly set as well). 12/15/2005 3:29:03 PMExit Current Process and Write an Error to the Event Log Without Processing Try-Catch-Finally Code Blocks.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Exit Current Process and Write an Error to the Event Log Without Processing Try-Catch-Finally Code BlocksThe System.Environment.FailFast method ends the current process without running any code inside of any try-catch-finally blocks or Finalizer methods and writes an optional error message to the Event Log. Calling FailFast will result in the current aplication exiting in a brute manner. Use FailFast only in situations where executing any additional code in the application is unwanted. The only code that is permitted to execute after a FailFast call is the finalizer of an instantiated class that inherets from the CriticalFinalizerObject class.12/14/2005 4:28:31 PMImplementing Simple Encryption and Decryption with ProtectedData Class.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Implementing Simple Encryption and Decryption with ProtectedData ClassDemonstrates how to implement simple encryption and decryption of data such as database strings with the new ProtectedData class.12/15/2005 11:42:40 AMParsing T-SQL Statements with SQLDMO.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Parsing T-SQL Statements with SQLDMOIllustrates how you can parse T-SQL statements against an SQL Server with SQL-DMO.12/20/2005 12:58:36 PMRetrieving List of SQL Server Databases.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Retrieving List of SQL Server DatabasesIllustrates how to retrieve a list of databases available from a single SQL Server instance.12/20/2005 1:50:48 PMRetrieving List of SQL Servers on a Network via SQL-DMO.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Retrieving List of SQL Servers on a Network via SQL-DMOIllustrates how to retrieve a list of SQL Server instances found on a local area network.12/20/2005 1:50:51 PMSelect All Records from SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Select All Records from SQL Server DatabaseSelect All Records from SQL Server Database12/14/2005 4:28:33 PMSelect All Records from Stored Procedure from SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Select All Records from Stored Procedure from SQL Server DatabaseSelect All Records from Stored Procedure from SQL Server Database12/14/2005 4:28:14 PMSelect All Records with Where Clause from SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Select All Records with Where Clause from SQL Server DatabaseSelect All Records with Where Clause from SQL Server Database12/14/2005 4:28:16 PMSelect Unique Record from Stored Procedure from SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Select Unique Record from Stored Procedure from SQL Server DatabaseSelect Unique Record from Stored Procedure from SQL Server Database12/14/2005 4:28:17 PMSqlDataSourcesHelper Class.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=SqlDataSourcesHelper ClassProvides a type-safe and enumerable custom class that leverages the System.Data.Sql.SqlDataSourceEnumerator class for retrieving a list of active SQL Server instances on a network.12/21/2005 2:57:24 PMStoring Multidimensional Arrays in the Registry.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Storing Multidimensional Arrays in the RegistryShows you how you can store multidimensional arrays in the Windows Registry. Take note that I'm making use of a Memory Stream - meaning that serialization will be written to memory. If your multidimensional arrays are large, you may want to choose a different streaming strategy. 12/20/2005 1:50:50 PMUpdate Unique Record via Stored Procedure in SQL Server Database.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Update Unique Record via Stored Procedure in SQL Server DatabaseUpdate Unique Record via Stored Procedure in SQL Server Database12/14/2005 4:28:18 PMUsing Method Parameter Modifiers.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Using Method Parameter ModifiersIllustrates how to use method parameter modifiers such as ref and output and how certain types are passed by Value and others by Reference.12/27/2005 10:00:20 PMUsing Nullable Types.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Using Nullable TypesDemonstrates how to use nullable types.2/8/2006 4:54:26 PMUsing Pointers.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Using PointersIllustrates how to implement unsafe pointers in C# code.1/6/2006 6:12:52 PMUsing Windows Forms UseWaitCursor Property.snippethttp://www.dotnetfun.com/CodeSnippets.aspx?n=Using Windows Forms UseWaitCursor PropertyDemostrates how to use the Windows Forms' UseWaitCursor property.12/14/2005 4:28:21 PM