Sunday, May 3, 2015

SQL Cross JOIN or Cartesian Product

This type of JOIN returns the cartesian product of rows of from the tables in Join. It will return a table which consists of records which combines each row from the first table with each row of the second table.
Cross JOIN Syntax is,
SELECT column-name-list
from table-name1 
CROSS JOIN 
table-name2;

Example of Cross JOIN

The class table,
IDNAME
1abhi
2adam
4alex
The class_info table,
IDAddress
1DELHI
2MUMBAI
3CHENNAI
Cross JOIN query will be,
SELECT *
 from class,
 cross JOIN class_info;
The result table will look like,
IDNAMEIDAddress
1abhi1DELHI
2adam1DELHI
4alex1DELHI
1abhi2MUMBAI
2adam2MUMBAI
4alex2MUMBAI
1abhi3CHENNAI
2adam3CHENNAI
4alex3CHENNAI

SQL Types Of Joins

The following are the types of JOIN that we can use in SQL.
  • Inner
  • Outer
  • Left
  • Right

SQL Joins

SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. SQL Join is used for combining column from two or more tables by using values common to both tables. JoinKeyword is used in SQL queries for joining two or more tables. Minimum required condition for joining table, is(n-1) where n, is number of tables. A table can also join to itself known as, Self Join.

Friday, May 1, 2015

asp.net Convert Bytes To Megabytes

  public static string ConvertBytesToMegabytes(long bytes)
        {
            return ((bytes / 1024f) / 1024f).ToString("0.00");

        }

asp.net Create File From Base64

  public static bool CreateFileFromBase64(string fileName, string Base64Data)
        {
            try
            {
                FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);
                byte[] data = Convert.FromBase64String(Base64Data);
                fs.Write(data, 0, data.Length);
                fs.Close();

                return true;
            }
            catch (Exception ex)
            {
                logger.LogError("Error: " + ex.Message);
                return false;
            }


        }

asp.net get json from datatable

    public static string GetJson(DataTable dt)
        {
            StringBuilder JsonString = new StringBuilder();


            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"Head\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        else if (j == dt.Columns.Count - 1)
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");

                    }
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                        JsonString.Append("} ");
                    else
                        JsonString.Append("}, ");
                }
                JsonString.Append("]}");

            }



            return JsonString.ToString();
        }

asp.net Show User Zone Date

  public static string ShowUserZoneDate(string IDbdate, double zone)
        {
            if (!string.IsNullOrEmpty(IDbdate))
            {
                DateTime Dbdate = Convert.ToDateTime(IDbdate);
                TimeSpan tms = new TimeSpan(0, Convert.ToInt32(zone * 60), 0);
                return Dbdate.Add(tms).ToString();
            }
            else
                return "";
        }