How do I create a connection to a database?

To do an interaction with a database system you have to create a connection to the server. This connection basically contains the location of the database, database name, a username and password of a database and some other parameters.

To create a connection object you create an instance of SqlConnection class. You need to pass a connection string to the constructor of this object. After creating a connection you have to call the Open() method to open the database connection.

After having the connection object ready you can start using it to manipulate the database with the help of the SqlCommand object. Let’s see an example below for creating and opening a connection.

How to display a numeric value in hexadecimal format?

To display a numeric value in its hexadecimal format we can use the x or X formatting specifier when using the Console.WriteLine() method. The letter case of the formatting specifier determines whether the hexadecimal letters appear in lowercase or uppercase.

The code snippet below show you how to do this:

The output below show the result of our code snippet:

How to select top node in XML using XPath?

In this example you will learn how to select top node from an Xml document. We can use an XPath expression to select the nodes. For example using the following expression /Users/User[position() <= 3] we can select the first three of the User node.

The code snippet above select the first top three node from the xml document. It will print the following output:

How to find XML node using multiple attribute values?

In this example you will learn how to select xml nodes based on multiple attribute values. The code snippet below selects User nodes that have an Active attribute equals to True and Expired attribute equals to False.

We define this selection criteria using an XPath expression that can be seen in the criteria variable in the snippet. We use the and operator in the XPath expression so that it matches both Active and Expired attributes.

Base on the defined xml document in the code snippet it is going to find two xml nodes. They are the nodes for user with username admin and jdoe. Let’s see the code below:

If you run the snippet you’ll get the following output:

How do I read xml node attribute values?

How do I select xml node by attribute value?

This example show you how to select nodes from an xml document based on the node’s attribute values. We can use the XmlDocument.SelectNodes or XmlDocument.SelectSingleNode method to get a list of nodes or a single node from an xml document. For selecting node based on its attribute values we use an XPath expression in the following format: /Users/User[@UserName='foobar'].

This code snippet will print the following output:

How to select the first matched XmlNode using XPath?

This example show you how to read the first XmlNode that matches the XPath expression. To find the first node we can use the XmlDocument.SelectSingleNode() method. This method takes a string of XPath expression and return the first XmlNode found in the xml document.

This code will give you the first node which is the node that contains the admin user information.