RSS

SQL Interview Questions

Some of the Sql Server Questions
1.What is Log Shipping and its purpose


In Log Shipping the transactional log file from one server is automatically updated in backup database on the other server and in the case when one server fails the other server will have the same DB and we can use this as the DDR(disaster recovery) plan.


2.What is the advantage of SET NOCOUNT ON


When we use SELECT and DML statement in SQL .SQL server return a message which specify the number of rows effected by these statements. This information helps coder when they are debugging the code other wise this is not useful we can disable this by typing SET NOCOUNT ON. It is very helpful when we are doing on store procedure contains lots of statements,loops its also increase in performance and boost network traffic.


3.How to load .NET code in SQL SERVER 2005


First of all write managed code and compile it to DLL/Assembly.Now we can load the assembly in SQL SERVER.Here i have given a example of SQL SERVER. CREATE ASSEMBLY percode FROM 'c:/percode.dll'


4.What are the Global Temporary Tables


We can create global temporary tables but these are not using much in sql an the name of these table start with two pound signs. For example, ##interviewqsn is a global temporary table.As the name suggest these table is Global temporary tables and visible to all SQL Server connections. When we create any one of these all users can see it.

5.What are Checkpoint in SQL Server


When we done operation on SQL SERVER that is not commited directly to the database.All operation must be logged in to Transaction Log files after that they should be done on to the main database.CheckPoint are the point which alert Sql Server to save all the data to main database if no Check point is there then log files get full we can use Checkpoint command to commit all data in the SQL SERVER.When we stop the SQL Server it will take long time because Ch eckpoint is also fired.

read comments Read User's Comments

Generics and Nullables in C# 2.0

Generics in C# 2.0
1. The Generics was added to C# 2.0 to remove the need for casting, improve type safety, reduce the amount of boxing required, and to make it easier to create generalized classes and methods.
2. Generic classes and methods accept type parameters, which specify the type of objects that they operate on.
3. Version 2.0 of the .NET Framework Class Library includes generic versions of many of the collection classes and interfaces in the System.Collections.Generic namespace
EX:

using System.Collections.Generic;
...
Queue myQueue = new Queue();
Circle myCircle = new Circle();
myQueue.Enqueue(myCircle);
...
myCircle = myQueue.Dequeue();


NOTE:
• The use of the type parameter between the angle brackets, , when
declaring the myQueue variable.
• The lack of a cast when executing the Dequeue method.
* The compiler will check to ensure that types are not accidentally mixed,
generating an error at compile time rather than runtime if you try to
dequeue an item from circleQueue into a Some other object.
- The bellow class explains the Generic with Delegate of Type T

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public delegate void dele1(T x);

class Class1
{
public void fn1(int x)
{
Console.WriteLine(x);
}
public void fn2(string x)
{
Console.WriteLine(x);
}
}
class myclass
{
static void Main(string[] ar)
{
Class1 c = new Class1();
dele1 ob = new dele1(c.fn1);
dele1 ob1 = new dele1(c.fn2);
ob(10);
ob1("stg");
}
}
}

Nullable Types in C# 2.0
We can now assign the null value to value types by defining nullable types. We do this by adding a question mark to the immediate right of the type name when defining a variable. Nullable types derive from the generic type System.Nullable. Instead of defining types from this generic class, We can simply just use T? (where T is the type (class, struct, etc.).

Two properties make nullable types quite useful: HasValue and Value. HasValue evaluates to true if the type is not null, otherwise it's false. Value will return the underlying value, whether it's null or not.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Class3
{
static void Main(string[] a)
{
int? myInt1 = null;

if (myInt1 == null)
Console.WriteLine("myInt1 is null.");
else
Console.WriteLine("myInt1 is Not null.");
// Or, we can check for a value this way:
myInt1 = 1;
if (myInt1.HasValue)
Console.WriteLine("myInt1 has a value = {0}", myInt1.Value);
else
Console.WriteLine("myInt1 is null.");
}
}
}

read comments Read User's Comments

About Partial Classes in C#

The C# Partial Class
• C# 1.1 requires you to put all the code for a class in a single file
• C# 2.0 allows you to split the definition and implementation of a class or a
struct across multiple files. You can put one part of a class in one file and
another part of the class in a different file, noting the split by using the new
partial keyword
* Normally Partial Classes are classes that can be defined at more than one
location, but which contain different members.
* Partial Classes also allow you to store the same class, with different members, in
different physical locations.

using System;
using System.Collections.Generic;
using System.Text;
namespace PartialClassExample
{
public partial class class1
{
public void fn1()
{
Console.WriteLine("Hi");
}
}

public partial class class1
{
public void fn2()
{
Console.WriteLine("Hello");
}
}

public partial class class1
{
public void fn3()
{
Console.WriteLine("Bye");
}
}

public class myclass
{
static void Main(string[] ar)
{
class1 ob = new class1();
ob.fn1();
ob.fn2();
ob.fn3();
}
}
}

• Partial type support is available for classes, structures, and interfaces, but you
cannot have a partial enum definition.
• ASP.NET 2.0 uses partial classes for the code-beside class (the evolution of
codebehind), storing the machine-generated part of the page separately.
• Windows® Forms uses partial classes to store the visual designer output of the
InitializeComponent method as well as the member controls.
• Partial types also enable two or more developers to work on the same type while
both have their files checked out from source control without interfering with
each other.
• C# 2.0 supports partial types as follows: when the compiler builds the assembly,
it combines from the various files the parts of a type and compiles them into a
single type in Microsoft intermediate language (MSIL). The generated MSIL has no
recollection which part came from which file. Just like in C# 1.1 the MSIL has no
record of which file was used to define which type.
• Also worth noting is that partial types cannot span assemblies, and that a type
can refuse to have other parts by omitting the partial qualifier from its
definition.
Note: Classname should be the same in different files
changes made to the file will be lost if you regenerate the wrapper class. Using a partial class, you can factor those changes into a separate file.

read comments Read User's Comments

Some of the Basic Javascript user defind functions

To Identify of there is a any Item is available in a List box Using Javascript


function CheckSelected() {
var sMsg = "";
if (document.getElementById('<%=lstMinorErrors.ClientID %>') != null) {
//alert(document.getElementById('<%=lstMinorErrors.ClientID %>').options.length);
if (document.getElementById('<%=lstMinorErrors.ClientID %>').options.length < 0) {
sMsg = "No Items Found to Insert!"
}
}
if (sMsg != "") {
alert(sMsg);
return false;
}
else
return true;
}

To valid Gridview if any of the Checkbox item Selected or Not:

//To check whether the user checked any of the item in a Gridview for Delete Options
function ClientCheck(sMsg) {
var valid = false;
var gv = document.getElementById('<%=gvMinorErrorMaster.ClientID%>');//gvMinorErrorMaster- denotes gridview Name

for (var i = 0; i < gv.all.length; i++) {
var node = gv.all[i];
if (node != null && node.type == "checkbox" && node.checked) {
valid = true;
break;
}
}
if (!valid) {
alert("Please select a checkbox to continue.");
}
else {
var sConform = confirm(sMsg);
if (sConform == true)
valid = true;
else
valid = false;
}
return valid;
}


//In code behind of the page_load Event:
btnDelete.Attributes.Add("OnClick", "return ClientCheck('Are you sure to Delete the Selected Rows.');");

To check if any of the Items user select in a DropDown List

function CheckSearchProCustSel() {
var ddlReport;
var sObj;
var sErrMsg = "";
ddlReport = document.getElementById("<%=ddlSProCust.ClientID%>");//ddlSProCust- denotes Dropdown list Id
sObj = ddlReport.options[ddlReport.selectedIndex].value;//Get the Selected index value
//check if the selectedindex
if (sObj == 0) {
sErrMsg = "Select Customer-Project to Serach! \n";
}
if (sErrMsg != "") {
alert(sErrMsg);
return false;
}
else
return true;
}

To Trim the Spaces in a String from the textbox or other controls:

// Removes leading whitespaces
function LTrim(value) {
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim(value) {
return LTrim(RTrim(value));
}

read comments Read User's Comments

Move Items Between 2 ListBoxes Using Javascript

Just wrote this little JavaScript snippet to your asp.net page and make a moment of Items between two ListBox.

 
<script>

function MoveItem(ctrlSource, ctrlTarget) {
var Source = document.getElementById(ctrlSource);
var Target = document.getElementById(ctrlTarget);
if ((Source != null) && (Target != null)) {
while ( Source.options.selectedIndex >= 0 ) {
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = Source.options[Source.options.selectedIndex].text;
newOption.value = Source.options[Source.options.selectedIndex].value;
Target.options[Target.length] = newOption;//Append the item in Target
Source.remove(Source.options.selectedIndex);//Remove the item from Source
}
}
}
</script>

And the HTML Code of the Page:

<table height="150" width="300">
<tr>
<td>
<asp:ListBox id="ListBox1" runat="server" Height="111px" SelectionMode="Multiple">
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
</asp:ListBox>
</td>
<td>
<p>
<input onclick="Javascript:MoveItem('ListBox1', 'ListBox2');" type="button" value="->" />
</p>
<p>
<input onclick="Javascript:MoveItem('ListBox2', 'ListBox1');" type="button" value="<-" />
</p>
</td>
<td>
<asp:ListBox id="ListBox2" runat="server" Height="111px" SelectionMode="Multiple">
<asp:ListItem Value="8">Eight</asp:ListItem>
<asp:ListItem Value="9">Nine</asp:ListItem>
<asp:ListItem Value="10">Ten</asp:ListItem></asp:ListBox>
</td>
</tr>
</table>

read comments Read User's Comments