August 2012: .NET SQL Server Database Code Snippets

| 0 comments

How many times have you looked for a piece of code you’ve written in the past? You probably search high and low on your hard drive, scouring through past projects and code files. Or maybe you’ve tried searching your source code control repository with unsuccessful results. How much time do you waste looking for a particular routine only to not find it, which then causes you to rewrite the routine all over again? Let’s face it, as developers we beg, borrow, and steal as much code as we can in order to get our jobs done as quickly as possible. After all, the grand utopian vision of developers is code reuse – the ability to write blocks of code once and then reuse them again and again without having to rewrite them. But how often does that actually happen? Unfortunately, not often enough. Feel free to add the following snippets to your tool belt. /// <summary> /// Checks if a database exists /// </summary> /// <param name=”Database”>Name of the database</param> /// <param name=”ConnectionString”>Connection string</param> /// <returns>True if it exists, false otherwise</returns> public static bool DoesDatabaseExist(string Database, string ConnectionString) { return CheckExists(“SELECT * FROM Master.sys.Databases WHERE name=@Name”, Database, ConnectionString); } /// …

Continue reading

Series DropDownList: Cascading DropDownList

| 0 comments

This is the second article on the subject of DropDownList. If for any reason you missed the earlier post titled Series DropDownList: Binding XML Data to a DropDownList, I would recommend that you take the time and read that post as well. In part two of this series I will focus on accomplishing cascading selections with your DropDownList. Since the bulk of the work was accomplished in DropDownList: Binding XML Data to a DropDownList we will pick up from there. Web Form Here we will incorporate a small change from the previous example. Notice that in this example I have added a new event titled OnSelectedIndexChanged. <label for=”ddlCountry”>Country:</label> <asp:DropDownList ID=”ddlCountry” runat=”server” AutoPostBack=”True” OnSelectedIndexChanged=”ddlCountry_SelectedIndexChanged” Width=”160px”> </asp:DropDownList> <label for=”ddlRegion”>Region:</label> <asp:DropDownList ID=”ddlRegion” runat=”server” AutoPostBack=”True” OnSelectedIndexChanged=”ddlRegion_SelectedIndexChanged” Width=”160px”> </asp:DropDownList> <label for=”ddlCity”>City:</label> <asp:DropDownList ID=”ddlCity” runat=”server” Width=”160px”> </asp:DropDownList> Code Behind Each selected index change event fires the appropriate method which in turns makes a call back to the server and reads in the appropriate data to return and bound to out DropDownList. protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { ddlRegion.Items.Clear(); string strCountry = string.Empty; strCountry = ddlCountry.SelectedValue; List<string> list = null; if (ddlCountry.SelectedIndex != 0) { list = RetrieveDataFromXml.GetRegionByCountry(strCountry); if (list != null && list.Count != 0) …

Continue reading

Series DropDownList: Binding XML Data to a DropDownList

| 0 comments

Who doesn’t love XML? Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards.  The design goals of XML emphasize simplicity, generality, and usability over the Internet. It is a textual data format with strong support via Unicode for the languages of the world. Although the design of XML focuses on documents, it is widely used for the representation of arbitrary data structures, for example in web services. Bottom line XML is easily created, consumed, and understood. In this article we will focus on the basic idea of binding XML data to a DropDownList. When it is all said and done your DropDownList will look similar to the following example. XML Source First we need a XML file. In this example we have XML that contains countries, regions, and cities. <?xml version=”1.0″ encoding=”utf-8″ ?> <Countries> <Country name=”Korea”> <Region name=”South Korea”> <City> Seoul </City> <City> Taegu </City> <City> Songtan </City> </Region> </Country> <Country name=”USA”> <Region name=”California”> <City> Los Angeles </City> <City> Bakersfield </City> <City> …

Continue reading

Microsoft Enterprise Library: Caching Application Block

| 0 comments

This is a a second article on the topic of the Microsoft Enterprise Library. If you have not read the previous article titled Microsoft Enterprise Library: Data Access Application Block, I recommend you do so. Introduction to the Caching Application Block The Enterprise Library Caching Application Block lets developers incorporate a local cache in their applications. It supports both an in-memory cache and, optionally, a backing store that can either be the database store or isolated storage. The Caching Application Block can be used without modification; it provides all the functionality needed to retrieve, add, and remove cached data. Configurable expiration and scavenging policies are also part of the block. If you have been working with caching outside the Enterprise Library, I believe you will find this application block extremely powerful and easy to use. If you have not taken on the subject of caching before, I believe you also will find this easy to pick up and ultimately boost the performance of your applications. The Enterprise Library Caching Application Block includes the following features: You can use the graphical Enterprise Library configuration tools to manage configuration settings. You can configure a persistent storage location, using either isolated storage or …

Continue reading

Microsoft Enterprise Library: Data Access Application Block

| 0 comments

For those of you who have been using the Enterprise Library from Microsoft then I tip my hat to you. I admit that I have not used this library for a number of years and in most cases the reason is because I have honestly not been in a position to do so. It is a long story so don’t ask. There are a number of reason why you should seriously consider the use of the Enterprise Library and I cannot think of any better reason than those provided directly from Microsoft. The goals of Enterprise Library are the following: Consistency. All Enterprise Library application blocks feature consistent design patterns and implementation approaches. Extensibility. All application blocks include defined extensibility points that allow developers to customize the behavior of the application blocks by adding their own code. Ease of use. Enterprise Library offers numerous usability improvements, including a graphical configuration tool, a simpler installation procedure, and clearer and more complete documentation and samples. Integration. Enterprise Library application blocks are designed to work well together or individually. Now that the groundwork has been laid let us get started. Introduction to the Data Access Library The Data Access Application Block includes a …

Continue reading