Sunday, September 16, 2018

Difference between TRUNCATE and DELETE in SQL Server?


TRUNCATE:

  • It is a DDL command
  • It does not support WHERE clause/condition
  • Removes all the data all the time
  • Faster than DELETE as it locks entire table
  • It removes the data by deallocating the data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log It does not activate triggers
  • Table identity column is reset to seed value

Syntax:

TRUNCATE TABLE TableName

DELETE:

  • It is DML command
  • It supports WHERE clause/condition
  • Removes data based on conditions specified in the WHERE clause (removes all the data if there is no WHERE clause) Slower than TRUNCATE as it takes row level locks
  • It removes rows one at a time and records an entry in the transaction log for each deleted row
  • It does activate triggers
  • Table identity column is not reset

Syntax:

DELETE FROM TableName WHERE ColName = ‘YourCondition’

Note

Truncate and Delete both are logged operations and both can be rolled back when they are within transactions. It is myth that Truncate is not logged operations. It is indeed logged operations and it locks page level deallocations.

Fundamentals of Garbage Collection


In the common language runtime (CLR), the garbage collector serves as an automatic memory manager. It provides the following benefits:
  • Enables you to develop your application without having to free memory.
  • Allocates objects on the managed heap efficiently.
  • Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.
  • Provides memory safety by making sure that an object cannot use the content of another object.
Click here to read full article by Microsoft

Wednesday, February 8, 2012

Creating Hit Counter for Total Page Views

In Count.aspx
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblCounter" runat="server"></asp:Label>

    </div>
    </form>
</body>

In Count.aspx.cs

  protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
      //  application["activeuser"] = application["activeuser"] + 1;
    }
    private void countMe()
    {

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

        hits += 1;

        tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));


    }


In  counter.xml/
 <?xml version="1.0" standalone="yes"?>
<counter>
  <count>
    <hits>0</hits>
  </count>
</counter>















Sunday, January 8, 2012

Get Stored Procedure Return Value

 SqlConnection con = null;
        try
        {
            string connString = ConfigurationManager.ConnectionStrings["cnTest"].ConnectionString;
            con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_ValidateUser";
            cmd.Parameters.AddWithValue("@Name", txt1.Text);
            con.Open();
            // Return value as parameter
            SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
            returnValue.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(returnValue);

            // Execute the stored procedure
         
            cmd.ExecuteNonQuery();
            con.Close();

            Int32 ret= Convert.ToInt32(returnValue.Value);
            Response.Write(Convert.ToString(ret));
   
        }
        catch (SqlException ex)
        {
            // handle error
        }
        catch (Exception ex)
        {
            // handle error
        }
        finally
        {
            con.Close();
        }






Create Proc usp_ValidateUser
@Name Varchar(50)
AS 
BEGIN 
        if exists(Select Name from tblTestUser where Name=@Name)
         Begin
                return 1
         End
       
        else
         Begin
                return -1
         End
   
       
END





Get selected index, value,text of ASP.Net drop down list in javascript



<%-- In Head section--%>
  <script type="text/javascript" language="javascript">
   
            function getDropdownListValues() {
            var DropdownList = document.getElementById('<%=aspdropdown.ClientID %>');
            var SelectedIndex = DropdownList.selectedIndex;
            var SelectedValue = DropdownList.value;
            var SelectedText = DropdownList.options[DropdownList.selectedIndex].text;

            var LabelDropdownList = document.getElementById('<%=lblDropdownList.ClientID %>');
            var sValue = 'Index: ' + SelectedIndex + '<br/> Selected Value: ' + SelectedValue + '<br/> Selected Text: ' + SelectedText;

            LabelDropdownList.innerHTML = sValue;
        }
    </script>






<%-- In body section--%>
 


 <div>
        <asp:Label runat="server" ID="lblText">Asp.net DropdownList</asp:Label><br />
  
        <asp:DropDownList ID="aspdropdown" runat="server" onchange="getDropdownListValues();">
            <asp:ListItem Value="Test1" Text="Testing1"></asp:ListItem>
            <asp:ListItem Value="Test2" Text="Testing2"></asp:ListItem>
            <asp:ListItem Value="Test3" Text="Testing3"></asp:ListItem>
            <asp:ListItem Value="Test4" Text="Testing4"></asp:ListItem>
            <asp:ListItem Value="Test5" Text="Testing5"></asp:ListItem>
        </asp:DropDownList> <br />
  
        <asp:Label runat="server" ID="lblDropdownList"></asp:Label>
    </div>