Search This Blog

Tuesday, March 26, 2013

XML query() Method SQL Server

Specifies an XQuery against an instance of the xml data type.

Example:


declare @Doc xml
set @Doc = '<Root>
<Order OrderID="1" CustomerName="John">
       <Items>
             <Item>Item 1</Item>
             <Item>Item 2</Item>
       </Items>
</Order>
</Root>'


SELECT @Doc.query('/Root/Order/Items')

Reult:

<Items>
  <Item>Item 1</Item>
  <Item>Item 2</Item>
</Items>






Cleans all pages in all files of the SQL Server database

SQL Server > System Stored Procedures > sp_clean_db_free_space

Cleans all pages in all files of the database.


Under some circumstances, the deleted rows can physically remain on the data page as a ghost records.
You can use sp_clean_db_free_space to clean these ghost records.
Before you run sp_clean_db_free_space,  it is recommend to create a full database backup.

Example


USE Master


GO


EXEC sp_clean_db_free_space  @dbname = N'YourDBName';








Monday, March 25, 2013

Handle NetworkAvailabilityChanged event in C# Windows Forms

C# > System.Net > NetworkInformation > NetworkChange > NetworkAvailabilityChanged

NetworkAvailabilityChanged occurs when the availability of the network changes.

Example

Check network available.

Edit Program.cs: Add event to check network is available






using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///

        private static void NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            {
                MessageBox.Show ("Network Available");
            }
            else
            {
                MessageBox.Show("Network Unavailable");
            }
        }

        [STAThread]
        static void Main()
        {
            NetworkChange.NetworkAvailabilityChanged += NetworkAvailabilityChanged;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Check network availability c#

C# >System.Net > NetworkInformation > NetworkInterface > GetIsNetworkAvailable


GetIsNetworkAvailable indicates whether any network connection is available.

Example
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();





base keyword C#

C# > Keywords > Base

Base is used to access members of the base class from within a derived class

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public class Animal
        {
            protected string type = "";
            public virtual string GetAnimalInfo()
            {
               return(type); 
            }
        }
        class Dog : Animal
        {
            private string name = "";
            public Dog(string name)
            {
                base.type = "dog";
                this.name = name;
            }
            public string GetAnimalInfo()
            {
                // Calling the base class GetInfo method:
                string type = base.GetAnimalInfo();
                return (name + " is a " + type);
            }
        }
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            Dog dog = new Dog("Tashi");
            string AnimalInfo = dog.GetAnimalInfo(); // "Tashi is a dog"
         }
    }
}










this C#

C > Keywords > This

Refers to the current instance of the class.

Example

1.This is used to qualify the FirstName and LastName

class Person
{
            private string FirstName;
            private string LastName;
            
            public Person(string FirstName, string LastName)
           {
                this.FirstName = FirstName;
                this.LastName = LastName;
            }









Thursday, March 21, 2013

Insert values and create a temporary table from a dynamic query SQL Server

SQL Server > Scripts > Insert dynamic temp table

Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server

DECLARE @sql varchar(max)
IF object_id('tempdb.dbo.##MyTemp') Is Not Null
       DROP TABLE ##MyTemp
SET @sql =
'SELECT * INTO ##MyTemp FROM (select name from sys.all_objects ) as tbl'
Exec (@sql)
go
SELECT * FROM ##MyTemp