Tuesday, April 3, 2012

ADO.NET's basic methods to run queries with SqlCommand

SqlClient namespace in ADO.NET provides 3 basic methods to run queries with SqlCommand against MSSQL:

ExecuteReader: When database query is going to provide a set of records.

ExecuteNonQuery: When we are talking about a single database record - in Update, Insert, Delete and Get by Id. In all these cases we can use input/output/input-output parameters

ExecuteScalar: When database query returns a single value and this value cannot be defined as output parameter, because of T-SQL type limitation for variables. For example type image cannot be output parameter in MSSQL.

Tuesday, February 21, 2012

DELETE, TRUNCATE and DROP table commands in SQL

1. DELETE only deletes the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. It does not free the space containing the table.

TRUNCATE is used to delete all the rows from the table and free the space containing the table. It does not delete the table structure.

DROP is used to remove an object from the database. All the indexes and privileges also get removed. If you drop a table, all the rows in the table is deleted and the table structure is removed from the database. Once a table is dropped we cannot get it back. When a table is dropped all the references to the table become invalid.

2. Syntax:

DELETE FROM table_name [WHERE condition];
To delete all rows:
DELETE FROM table_name;

TRUNCATE TABLE table_name;

DROP TABLE table_name;

3. DELETE does not reset the identity seed but TRUNCATE does reset it. DROP deletes every thing including the identity seed, of course!

4. After performing a DELETE operation, COMMIT or ROLLBACK is needed to make the changes permanent or to undo them.
In case of TRUNCATE, there is no roll back. Same is the case for DROP - no roll back.
In other words, DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.

During DELETE, all the data get copied into the Rollback Tablespace first and then the delete operation gets performed. That is why ROLLBACK can get back the data even after deleting a table. But during TRUNCATE,it removes data directly without copying it anywhere. That is why TRUNCATE is faster but it has the disadvantage that you cannot retrieve the data.

5. A trigger doesn’t get fired in case of TRUNCATE or DROP whereas Triggers get fired in DELETE command.

6. DELETE and TRUNCATE both are logged operation. But DELETE is a logged operation on a per row basis and TRUNCATE logs the deallocation of the data pages in which the data exists. There is no logging in case of DROP.

7. If you have to delete all the rows of a table you should perform TRUNCATE command with DROP STORAGE option. Truncate drops the storage held by this table which can then be used by this table again or some other table. This is useful for improved performance.

Tuesday, October 4, 2011

Malware

Malware: Malware, short for malicious software, consists of programming (code, scripts, active content, and other software) designed to disrupt or deny operation, gather information that leads to loss of privacy or exploitation, gain unauthorized access to system resources, and other abusive behavior.

A few examples of malware:

1. Virus: A computer program that can copy itself and infect a computer.

2. Worm: A self-replicating malware computer program, which uses a computer network to send copies of itself to other computers on the network.

3. Spyware: A malware that collects small pieces of information about users without their knowledge.

4. Adware: Any software package which automatically plays, displays, or downloads advertisements to a computer.

5. Trojan horse: A destructive program that masquerades as an application. The software initially appears to perform a desirable function for the user prior to installation, but steals information or harms the system.

6. Rootkits: Once a malicious program is installed on a system or a human attacker breaks into a computer, it is essential that it stays concealed, to avoid detection and disinfection. Techniques known as rootkits allow this concealment, by modifying the host's operating system so that the malware is hidden from the user. Rootkits can prevent a malicious process from being visible in the system's list of processes, or keep its files from being read.

7. Backdoors: It is a method of bypassing normal authentication procedures. Once a system has been compromised (by one of the above methods, or in some other way), one or more backdoors may be installed in order to allow easier access in the future. Backdoors may also be installed prior to malicious software, to allow attackers entry.

Wednesday, October 14, 2009

Configuring IIS - A name was started with an invalid character. Error processing resource.

If you are getting this error: "A name was started with an invalid character. Error processing resource" when you try to access your ASP.NET page after a fresh IIS installation,

Then, try to configure IIS by doing this this:

Go to RUN and type this:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

In some cases this also helps: In the IIS properties (by right clicking "Properties" of "Web Sites" under "IIS" in services), under the ASP.NET tab, the version of ASP.NET is not selected. Select one for your ASP.Net version.

Wednesday, June 24, 2009

Environment variables in C#

C# syntax:
To get the logged-in username:
string username = System.Environment.GetEnvironmentVariable("USERNAME");

To get the name of the client machine:
string machinename = Environment.MachineName;

Friday, May 1, 2009

C# - Difference between Convert and Parse

Convert.ToInt32(string) and Int32.Parse(string) yield identical results except when the string is actually a null. In this case, Int32.Parse(null) throws an ArgumentNullException; but, Convert.ToInt32(null) returns a zero.

Wednesday, April 15, 2009

Response.Redirect exception in .NET 2.0

In .NET 2.0, using Response.Redirect gives an exception,
"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."

To solve the problem, use:
Response.Redirect ("Success.aspx", false)

or to end the response, write,
Response.Redirect ("Success.aspx", true)