Search This Blog

Wednesday, July 16, 2014

ROWCOUNT SQL Server Example

SQL Server > Built-In Functions > @@ROWCOUNT

Returns the number of rows affected by the last statement.

Example:

UPDATE Person
SET Age = 31
WHERE Id = 1
IF @@ROWCOUNT = 0
    PRINT 'No rows were updated';




Tuesday, June 24, 2014

Public keyword C# Example

C# > Keywords > public

Public is the most permissive access level. 

It is no restrictions on accessing public members.






Example


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

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public class Person
        {
            public string Name;
            public int Age;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Person p = new Person();
            p.Name = "John";
            p.Age = 35;
        }
    }
}




Wednesday, June 18, 2014

Implement Strings Enum in C#

C# > Types > Enum > String Enum


Implement Strings Enum in C#









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

namespace EnumString
{
    public partial class Form1 : Form
    {
        public enum Cars
        {
            [Description("Audi")] Audi,   
            [Description("Bmw")] Bmw,   
            [Description("Land Rover")] LandRover
        }
        public static string GetCarDescription(Enum car)
        {
            FieldInfo fi = car.GetType().GetField(car.ToString());
            DescriptionAttribute[] da = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (da != null && da.Length > 0)       
                return da[0].Description;
            else      
                return da.ToString();
        }
        public static IEnumerable EnumToList()
        {   
            Type type = typeof(T);   
            Array vals = Enum.GetValues(type);   
            List valList = new List(vals.Length);   
            foreach (int val in vals)   
            {
                valList.Add((T)Enum.Parse(type, val.ToString()));   
            }
            return valList;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Cars car in EnumToList<Cars>())
            {
                comboBox1.Items.Add(GetCarDescription(car));
            }
        }
    }
}





Friday, June 13, 2014

Format SQL Server Example

SQL Server > Built-in Functions > FORMAT

Formats a values to specific format.






Example:

A.
DECLARE @d DATETIME = '10/17/2014';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result'
      ,FORMAT ( @d, 'd', 'de-de' ) AS 'German Result'

      ,FORMAT ( @d, 'd', 'ro-ro' ) AS 'Romanian Result';

Result

US English Result German Result Romanian Result
10/17/2014         17.10.2014 17.10.2014

B.
DECLARE @d DATETIME = GETDATE();
SELECT FORMAT( @d, 'dd/MM/yyyy')

Result
(No column name)
13/06/2014

C.
SELECT FORMAT(100, 'N', 'en-us') AS 'Number Format',FORMAT(200, 'C', 'en-us') AS 'Currency Format'

Result
Number Format Currency Format
100.00         $200.00





Wednesday, May 28, 2014

Lower SQL Server Example

SQL Server > Built-in Functions > LOWER

Converts uppercase character data to lowercase.

Example

SELECT lower('SQL SERVER')

Result:

sql server




Friday, May 23, 2014

Dictionary C# Example

C# > Generics > Dictionary 

Dictionary is a collection of keys and values.
Retrieving a value by using its key is very fast because the Dictionary class is implemented as a hash table.

Example


    Dictionary<string, string> accesCode = new Dictionary<string, string>();

            accesCode.Add("AAA", "PERSON 1");
            accesCode.Add("BBB", "PERSON 2");
            accesCode.Add("CCC", "PERSON 3");

            string code = "";
            accesCode.TryGetValue("xxx", out code); // code = null
            accesCode.TryGetValue("CCC", out code); // code = PERSON 3

            if (!accesCode.ContainsKey("DDD"))
                accesCode.Add("DDD", "PERSON 4");


            foreach (KeyValuePair<string, string> kvp in accesCode)
            {
                MessageBox.Show(kvp.Key + kvp.Value);
            }





Thursday, May 22, 2014

Join two tables Linq example C#

C# > LINQ > Join

Join is association of objects that share a common attribute.


Example:


DataTable dtOrder = new DataTable("order");
dtOrder.Columns.Add("id", Type.GetType("System.Int32"));
dtOrder.Columns.Add("customer", Type.GetType("System.String"));

DataRow dr = dtOrder.NewRow();
dr["id"] = 1;
dr["customer"] = "X Company";
dtOrder.Rows.Add(dr);

dr = dtOrder.NewRow();
dr["id"] = 2;
dr["customer"] = "Y Company";
dtOrder.Rows.Add(dr);

DataTable dtProduct = new DataTable("product");
dtProduct.Columns.Add("order_id", Type.GetType("System.Int32"));
dtProduct.Columns.Add("product", Type.GetType("System.String"));

dr = dtProduct.NewRow();
dr["order_id"] = 1;
dr["product"] = "Product 1";
dtProduct.Rows.Add(dr);

dr = dtProduct.NewRow();
dr["order_id"] = 1;
dr["product"] = "Product 2";
dtProduct.Rows.Add(dr);

var orderProducts = from order in dtOrder.AsEnumerable()
                    join product in dtProduct.AsEnumerable() on order.Field<int>("id")                               equals product.Field<int>("order_id")
                     select new
                                {
                                    OrderId = order.Field<int>("id"),
                                    Product = product.Field<string>("product")

                                };