Tip 7: Check for SQL Server Database
checks for the existence of a SQL Server database. While it may seem a little complex, just break it down by variables and objects and it will be as easy as the rest. It's another good example of using the Object element in your snippet.
Tip 8. Retrieve a Named DataSet
Function SQLDatabaseExists(ByVal objConnection As _
SqlConnection, ByVal strDatabaseName As String) _
' Dim objConn As New SqlConnection _
' ("data source=sqlservername;user " & _
"id=sa;password=mypassword;Initial " & _
' Debug.WriteLine(SQLDatabaseExists( _
' objConn, "MyDatabase"))
Dim objCommand As SqlCommand
Dim i As Integer
objCommand = New SqlCommand( _
"SELECT COUNT(*) FROM master..sysdatabases " & _
"WHERE [name]='" & strDatabaseName & "'", _
i = objCommand.ExecuteScalar
Return i > 0
Catch ex As Exception
I chose this function
because it retrieves a named DataSet using C#, a language that I have not covered in these two snippet articles. Including it makes it pretty easy to see that snippets work the same way for C# as for VB.NET.
Tip 9: Export DataGrid to XML
TheSQLCommand, string TheQueriedTable,
TheSQLCommand.Connection = sqlOpenConn;
System.Data.SqlClient.SqlDataAdapter NewDA =
DataSet NewDS = new DataSet();
is a cool little ASP.NET function written in C# that exports the content of a DataGrid to an XML file and sends that to the client.
Tip 10: Counting TreeView Nodes Recursively
ArrayList al = new ArrayList();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
In this final function
the code sets up a recursive loop to count the number of checked nodes in a given TreeView.
' Return the number of checked nodes within any node
' of a TreeView
Public Function CountCheckedNodes( _
ByVal rootNode As TreeNode) As Integer
Dim count As Integer = 0
' count the root node, if checked
If rootNode.Checked Then count = 1
' check the child nodes, by recursively calling
' this function
Dim tvn As TreeNode
For Each tvn In rootNode.Nodes
count += CountCheckedNodes(tvn)
You can download
and install the tips featured in this article as a preconfigured snippets file, giving you the option to separate the snippets you want into individual snippet files. If you don't want to install all the snippets in the file, here's how to remove the ones you don't want.
Each individual snippet is contained wholly within its own <Snippet>
element. To remove snippets you don't want, first check the <References>
nodes. If any are only used by the snippet you are deleting, then you can delete the corresponding <Reference>
nodes as well; however, bear in mind from the example in Tip 2 that although there were three functions in one snippet file, all of which required the same assembly and namespace, the snippet file included it only once. So, be sure that the <Reference>
elements you delete aren't required by any other code snippets in the file.
After removing those, simply delete all the XML from the opening <Snippet>
to the closing </Snippet>
tags for each item you want to remove. Save the file. You can test your changes both by using the Test feature in the Snippet Editor, or by simply inserting them into a Visual Studio project and taking advantage of the real-time debugging features.