For more detail please visit external site i.e.
Friday, February 8, 2013
Tuesday, January 22, 2013
Operator Overloadiong in C#
All unary and
binary operators have pre-defined implementations, that are
automatically available in any expressions. In addition to this
pre-defined implementations, user defined implementations can also be
introduced in C#. The mechanism of giving a special meaning to a
standard C# operator with respect to a user defined data type such as
classes or structures is known as operator overloading. Remember that it
is not possible to overload all operators in C#. The following table
shows the operators and their overloadability in C#.
In
C#, a special function called operator function is used for overloading
purpose. These special function or method must be public and static.
They can take only value arguments. The ref and out parameters are not
allowed as arguments to operator functions. The general form of an
operator function is as follows.
public static return_type operator op (argument list)
Where the op is the operator to be overloaded and operator is the required keyword. For overloading the unary operators, there is only one argument and for overloading a binary operator there are two arguments. Remember that at least one of the arguments must be a user-defined type such as class or struct type.
Overloading Unary Operators
The general form of operator function for unary operators is as follows.
public static return_type operator op (Type t)
{
// Statements
}
Where Type must be a class or struct.
The return type can be any type except void for unary operators like +, ~, ! and dot (.) but the return type must be the type of 'Type' for ++ and - operators and must be a bool type for true and false operators. Also remember that the true and false operators can be overloaded only as pairs. The compilation error occurs if a class declares one of these operators without declaring the other.
The following program overloads the unary - operator inside the class Complex
// Unary operator overloading
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex();
c2.ShowXY(); // displays 0 & 0
c2 = -c1;
c2.ShowXY(); // diapls -10 & -20
}
}
Overloading Binary Operators An overloaded binary operator must take two arguments; at least one of them must be of the type class or struct, in which the operation is defined. But overloaded binary operators can return any value except the type void. The general form of a overloaded binary operator is as follows.
public static return_type operator op (Type1 t1, Type2 t2)
{
//Statements
}
//A concrete example is given below.
// Binary operator overloading
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex() { }
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(20, 30);
c2.ShowXY(); // displays 20 & 30
Complex c3 = new Complex();
c3 = c1 + c2;
c3.ShowXY(); // dislplays 30 & 50
}
}
The binary operators such as = =, ! =, <, >, < =, > = can be overloaded only as pairs. Remember that when a binary arithmetic operator is overloaded, corresponding assignment operators also get overloaded automatically. For example if we overload + operator, it implicitly overloads the + = operator also.
Operator Overloading & Inheritance
Even though the overloaded operators are declared as static, they are inherited to the derived classes. Because operator declaration always requires the class or struct in which the operator is declared, to participate in the signature of the operator, it is jot possible for an operator declared in a derived class to hide an operator declared in a base class. Thus the new modifier is never required and there never permitted in an operator declaration.
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
class MyComplex : Complex
{
private double x;
private double y;
public MyComplex(double i, double j)
{
x = i;
y = j;
}
public MyComplex()
{
}
public new void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
}
class MyClient
{
public static void Main()
{
MyComplex mc1 = new MyComplex(1.5, 2.5);
mc1.ShowXY();
MyComplex mc2 = new MyComplex(3.5, 4.5);
mc2.ShowXY();
MyComplex mc3 = new MyComplex();
//mc3 = mc1 + mc2;
//mc3.ShowXY();
}
}
Overloading Equality Operators
Since all user defined classes, by default, inherit from Syste.object, they inherit the System.object.Equals() method. The default implementation of Equals() method provide a reference based comparison. But it is possible to override this method inside the user-defined class so that they can provide a value-based comparison. The following class is an example for a reference-based comparison of Equals() method.
// Equals() Default Implementation
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10, 20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if (c1.Equals(c2))
Console.WriteLine("OK");
else
Console.WriteLine("NOT OK");
if (c2.Equals(c3))
Console.WriteLine("OK1");
}
}
he above program on execution displays "NOT OK" and "OK1". That means the Equals() method by default do a reference comparison. Remember that the values of objects c1 and c2 are same. But they have different references. But in the case of c2 and c3, they refer to the same object on the memory.
But in C#, it is possible to override the Equals() method inside any user defined class as shown below, so that they can do a value-based comparison.
// Equals() Overriding Default Implementation
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public override bool Equals(object o)
{
if ((Complex)o.x == this.x && (Complex)o.y == this.y)
return true;
else
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10, 20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if (c1.Equals(c2))
Console.WriteLine("OK");
else
Console.WriteLine("NOT OK");
if (c2.Equals(c3))
Console.WriteLine("OK1");
}
}
Now the program displays both "OK" and "OK1" on the command prompt.
Remember that when we override the Equals() method inside a class, we will need to override GetHashCode() method also.
Now let us see how we can override the operator = = and ! = so that they can do the comparisons.
// Overloading = = and ! = operators
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}",x,y);
}
public override bool Equals(object o)
{
if((Complex)o.x == this.x && (Complex)o.y == this.y )
return true;
else
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public static bool operator = = (Complex c1, Complex c2)
{
return c1.Equals(c2);
}
public static bool operator ! = (Complex c1, Complex c2)
{
return ! c1.Equals(c2);
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10,20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if(c1 = = c2)
Console.WriteLine("OK");
else if(c2 ! = c3)
Console.WriteLine("OK1");
else
Console.WriteLine("NOT OK");
}
}
nstead of repeating the codes I just call the override Equals() method inside the operator functions. Remember that if we overload the = = operator inside a class or struct we must also override ! = operator.
Summary
The code uses the feature of Operator Overloading in C#. It shows how different operators are overloaded and can be used in a easy manner.
protected void Page_Load(object sender, EventArgs e)
{
Rectangle objRect1 = new Rectangle();
Rectangle objRect2 = new Rectangle();
Rectangle objRect3 = new Rectangle(10, 15);
objRect1.Height = 15;
objRect1.Width = 10;
objRect2.Height = 25;
objRect2.Width = 10;
Console.WriteLine("Rectangle#1 " + objRect1);
Console.WriteLine("Rectangle#2 " + objRect2);
Console.WriteLine("Rectangle#3 " + objRect3);
if (objRect1 == objRect2)
{
Console.WriteLine("Rectangle1 & Rectangle2 are Equal.");
}
else
{
if (objRect1 > objRect2)
{
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
else
{
Console.WriteLine("Rectangle1 is lesser than Rectangle2");
}
}
if (objRect1 == objRect3)
{
Console.WriteLine("Rectangle1 & Rectangle3 are Equal.");
}
else
{
Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.");
}
}
class Rectangle
{
private int iHeight;
private int iWidth;
public Rectangle()
{
Height = 0;
Width = 0;
}
public Rectangle(int w, int h)
{
Width = w;
Height = h;
}
public int Width
{
get
{
return iWidth;
}
set
{
iWidth = value;
}
}
public int Height
{
get
{
return iHeight;
}
set
{
iHeight = value;
}
}
public int Area
{
get
{
return Height * Width;
}
}
/* OverLoading == */
public static bool operator ==(Rectangle a, Rectangle b)
{
return ((a.Height == b.Height) && (a.Width == b.Width));
}
/* OverLoading != */
public static bool operator !=(Rectangle a, Rectangle b)
{
return !(a == b);
}
/* Overloding > */
public static bool operator >(Rectangle a, Rectangle b)
{
return a.Area > b.Area;
}
/* Overloading < */
public static bool operator <(Rectangle a, Rectangle b)
{
return !(a > b);
}
/* Overloading >= */
public static bool operator >=(Rectangle a, Rectangle b)
{
return (a > b) || (a == b);
}
/* Overloading <= */
public static bool operator <=(Rectangle a, Rectangle b)
{
return (a < b) || (a == b);
}
public override String ToString()
{
return "Height=" + Height + ",Width=" + Width;
}
}
| Operators | Overloadability |
| +, -, *, /, %, &, |, <<, >> | All C# binary operators can be overloaded. |
| +, -, !, ~, ++, --, true, false | All C# unary operators can be overloaded. |
| ==, !=, <, >, <= , >= | All relational operators can be overloaded, but only as pairs. |
| &&, || | They can't be overloaded. |
| [] (Array index operator) | They can't be overloaded. |
| () (Conversion operator) | They can't be overloaded. |
| +=, -=, *=, /=, %= | These compound assignment operators can be overloaded. But in C#, these operators are automatically overloaded when the respective binary operator is overloaded. |
| =, . , ?:, ->, new, is, as, sizeof | These operators can't be overloaded in C#. |
public static return_type operator op (argument list)
Where the op is the operator to be overloaded and operator is the required keyword. For overloading the unary operators, there is only one argument and for overloading a binary operator there are two arguments. Remember that at least one of the arguments must be a user-defined type such as class or struct type.
Overloading Unary Operators
The general form of operator function for unary operators is as follows.
public static return_type operator op (Type t)
{
// Statements
}
Where Type must be a class or struct.
The return type can be any type except void for unary operators like +, ~, ! and dot (.) but the return type must be the type of 'Type' for ++ and - operators and must be a bool type for true and false operators. Also remember that the true and false operators can be overloaded only as pairs. The compilation error occurs if a class declares one of these operators without declaring the other.
The following program overloads the unary - operator inside the class Complex
// Unary operator overloading
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex();
c2.ShowXY(); // displays 0 & 0
c2 = -c1;
c2.ShowXY(); // diapls -10 & -20
}
}
Overloading Binary Operators An overloaded binary operator must take two arguments; at least one of them must be of the type class or struct, in which the operation is defined. But overloaded binary operators can return any value except the type void. The general form of a overloaded binary operator is as follows.
public static return_type operator op (Type1 t1, Type2 t2)
{
//Statements
}
//A concrete example is given below.
// Binary operator overloading
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex() { }
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(20, 30);
c2.ShowXY(); // displays 20 & 30
Complex c3 = new Complex();
c3 = c1 + c2;
c3.ShowXY(); // dislplays 30 & 50
}
}
The binary operators such as = =, ! =, <, >, < =, > = can be overloaded only as pairs. Remember that when a binary arithmetic operator is overloaded, corresponding assignment operators also get overloaded automatically. For example if we overload + operator, it implicitly overloads the + = operator also.
Operator Overloading & Inheritance
Even though the overloaded operators are declared as static, they are inherited to the derived classes. Because operator declaration always requires the class or struct in which the operator is declared, to participate in the signature of the operator, it is jot possible for an operator declared in a derived class to hide an operator declared in a base class. Thus the new modifier is never required and there never permitted in an operator declaration.
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public static Complex operator +(Complex c1, Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
}
class MyComplex : Complex
{
private double x;
private double y;
public MyComplex(double i, double j)
{
x = i;
y = j;
}
public MyComplex()
{
}
public new void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
}
class MyClient
{
public static void Main()
{
MyComplex mc1 = new MyComplex(1.5, 2.5);
mc1.ShowXY();
MyComplex mc2 = new MyComplex(3.5, 4.5);
mc2.ShowXY();
MyComplex mc3 = new MyComplex();
//mc3 = mc1 + mc2;
//mc3.ShowXY();
}
}
Overloading Equality Operators
Since all user defined classes, by default, inherit from Syste.object, they inherit the System.object.Equals() method. The default implementation of Equals() method provide a reference based comparison. But it is possible to override this method inside the user-defined class so that they can provide a value-based comparison. The following class is an example for a reference-based comparison of Equals() method.
// Equals() Default Implementation
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10, 20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if (c1.Equals(c2))
Console.WriteLine("OK");
else
Console.WriteLine("NOT OK");
if (c2.Equals(c3))
Console.WriteLine("OK1");
}
}
he above program on execution displays "NOT OK" and "OK1". That means the Equals() method by default do a reference comparison. Remember that the values of objects c1 and c2 are same. But they have different references. But in the case of c2 and c3, they refer to the same object on the memory.
But in C#, it is possible to override the Equals() method inside any user defined class as shown below, so that they can do a value-based comparison.
// Equals() Overriding Default Implementation
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}", x, y);
}
public override bool Equals(object o)
{
if ((Complex)o.x == this.x && (Complex)o.y == this.y)
return true;
else
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10, 20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10, 20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if (c1.Equals(c2))
Console.WriteLine("OK");
else
Console.WriteLine("NOT OK");
if (c2.Equals(c3))
Console.WriteLine("OK1");
}
}
Now the program displays both "OK" and "OK1" on the command prompt.
Remember that when we override the Equals() method inside a class, we will need to override GetHashCode() method also.
Now let us see how we can override the operator = = and ! = so that they can do the comparisons.
// Overloading = = and ! = operators
// Author: murlid05@gmail.com
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine("{0} {1}",x,y);
}
public override bool Equals(object o)
{
if((Complex)o.x == this.x && (Complex)o.y == this.y )
return true;
else
return false;
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public static bool operator = = (Complex c1, Complex c2)
{
return c1.Equals(c2);
}
public static bool operator ! = (Complex c1, Complex c2)
{
return ! c1.Equals(c2);
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(10,20);
c2.ShowXY(); // displays 10 & 20
Complex c3 = c2;
c3.ShowXY(); // dislplays 10 & 20
if(c1 = = c2)
Console.WriteLine("OK");
else if(c2 ! = c3)
Console.WriteLine("OK1");
else
Console.WriteLine("NOT OK");
}
}
nstead of repeating the codes I just call the override Equals() method inside the operator functions. Remember that if we overload the = = operator inside a class or struct we must also override ! = operator.
Summary
- The
user defined operator declarations can't modify the syntax, precedence
or associatively of an operator. For example, a + operator is always a
binary operator having a predefined precedence and an associatively of
left to right.
- User defined operator implementations are given preference over predefined implementations.
- Operator overload methods can't return void.
- The
operator overload methods can be overloaded just like any other methods
in C#. The overloaded methods should differ in their type of arguments
and/or number of arguments and/or order of arguments. Remember that in
this case also the return type is not considered as part of the method
signature.
- emember that the capability to overload operators is not a requirement of the common language specification. Hence, not all .NET aware languages supports operator overloading.
The code uses the feature of Operator Overloading in C#. It shows how different operators are overloaded and can be used in a easy manner.
protected void Page_Load(object sender, EventArgs e)
{
Rectangle objRect1 = new Rectangle();
Rectangle objRect2 = new Rectangle();
Rectangle objRect3 = new Rectangle(10, 15);
objRect1.Height = 15;
objRect1.Width = 10;
objRect2.Height = 25;
objRect2.Width = 10;
Console.WriteLine("Rectangle#1 " + objRect1);
Console.WriteLine("Rectangle#2 " + objRect2);
Console.WriteLine("Rectangle#3 " + objRect3);
if (objRect1 == objRect2)
{
Console.WriteLine("Rectangle1 & Rectangle2 are Equal.");
}
else
{
if (objRect1 > objRect2)
{
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
else
{
Console.WriteLine("Rectangle1 is lesser than Rectangle2");
}
}
if (objRect1 == objRect3)
{
Console.WriteLine("Rectangle1 & Rectangle3 are Equal.");
}
else
{
Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.");
}
}
class Rectangle
{
private int iHeight;
private int iWidth;
public Rectangle()
{
Height = 0;
Width = 0;
}
public Rectangle(int w, int h)
{
Width = w;
Height = h;
}
public int Width
{
get
{
return iWidth;
}
set
{
iWidth = value;
}
}
public int Height
{
get
{
return iHeight;
}
set
{
iHeight = value;
}
}
public int Area
{
get
{
return Height * Width;
}
}
/* OverLoading == */
public static bool operator ==(Rectangle a, Rectangle b)
{
return ((a.Height == b.Height) && (a.Width == b.Width));
}
/* OverLoading != */
public static bool operator !=(Rectangle a, Rectangle b)
{
return !(a == b);
}
/* Overloding > */
public static bool operator >(Rectangle a, Rectangle b)
{
return a.Area > b.Area;
}
/* Overloading < */
public static bool operator <(Rectangle a, Rectangle b)
{
return !(a > b);
}
/* Overloading >= */
public static bool operator >=(Rectangle a, Rectangle b)
{
return (a > b) || (a == b);
}
/* Overloading <= */
public static bool operator <=(Rectangle a, Rectangle b)
{
return (a < b) || (a == b);
}
public override String ToString()
{
return "Height=" + Height + ",Width=" + Width;
}
}
Monday, January 21, 2013
Get All the TABLES with Number of Rows and Columns
SELECT OBJECT_NAME(D.object_id) As [TABLE NAME], SUM(row_count) [ROWS COUNT] , Count(C.Column_Id) [COLUMN COUNT] FROM sys.dm_db_partition_stats As D Inner JOIN sys.objects As o On o.object_id = D.object_id And o.type = 'u' Inner Join sys.columns As C On C.object_id = D.object_id WHERE [index_id] IN (0,1) GROUP BY D.object_id
Sunday, December 9, 2012
Check if Checkbox is checked using jQuery
to find out if checkbox is checked or not using jQuery. I was knowing one way to find out but there are couple of other ways as well to find out if checkbox is checked using jQuery. In this post, you will find all different possible ways.
1. First Way:
Below single line of code will provide the status of checkbox using jQuery. It checks whether the checked is checked or not using jQuery and will return 1 or 0.
I
have noticed that on many website it is written that 'checked'
attribute will return true or false, but this is not correct. If the
checked box is checked then it return status as "checked", otherwise
"undefined".
2. Second Way
3. Third Way
The above method uses "is" selector and it returns true and false based on checkbox status.
4. Fourth Way
The below code is to find out all the checkbox checked through out the page.
Feel free to contact me for any help related to jQuery, I will gladly help you.
1. First Way:
Below single line of code will provide the status of checkbox using jQuery. It checks whether the checked is checked or not using jQuery and will return 1 or 0.
1 | var isChecked = $('#chkSelect').attr('checked')?true:false; |
2. Second Way
1 | var isChecked = $('#chkSelect:checked').val()?true:false; |
3. Third Way
1 | var isChecked = $('#chkSelect').is(':checked'); |
4. Fourth Way
The below code is to find out all the checkbox checked through out the page.
1 | $("input[type='checkbox']:checked").each( |
2 | function() { |
3 | // Your code goes here... |
4 | } |
5 | ); |
Find Current Location of Data and Log File of All the Database and get size of databases
Some time for carring data from one machine to another machine we need to copy database file
for that we need to copy database ldf and mdf file becaues this is eassiest way yo carring database. for this we must be know the these db files are where is located on server so we can copy this.
for that we need to copy database ldf and mdf file becaues this is eassiest way yo carring database. for this we must be know the these db files are where is located on server so we can copy this.
First way to write this query on our Query windowSELECT name, physical_name AS current_file_location
FROM sys.master_filesSecond way to find the location with Appropriate database file size SELECT DB_NAME(mf.database_id) AS databaseName
,mf.physical_name
,num_of_reads
,num_of_bytes_read
,io_stall_read_ms
,num_of_writes
,num_of_bytes_written
,io_stall_write_ms
,io_stall
,size_on_disk_bytes
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs
JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id
AND mf.file_id = divfs.file_id
ORDER BY 3 DESC Third way to find database details with size in shortest form that you wanted to findSELECT
DB_NAME(mf.database_id) AS databaseName,
name as File_LogicalName,
case
when type_desc = 'LOG' then 'Log File'
when type_desc = 'ROWS' then 'Data File'
Else type_desc
end as File_type_desc
,mf.physical_name
,num_of_reads
,num_of_bytes_read
,io_stall_read_ms
,num_of_writes
,num_of_bytes_written
,io_stall_write_ms
,io_stall
,size_on_disk_bytes
,size_on_disk_bytes/ 1024 as size_on_disk_KB
,size_on_disk_bytes/ 1024 / 1024 as size_on_disk_MB
,size_on_disk_bytes/ 1024 / 1024 / 1024 as size_on_disk_GB
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs
JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id
AND mf.file_id = divfs.file_id
ORDER BY num_of_Reads DESC
Thursday, November 29, 2012
JavaScript function to get random number between a range
Getting random number in JavaScript
Code: alert(Math.random())Getting random number is very easy you can use JavaScript function random() of Math object to get the random number between 0 and 1. For example, above JavaScript statement returns a random number between 0 and 1.
JavaScript function to get random number between 1 and N
//function to get random number from 1 to nfunction randomToN(maxVal, floatVal) {
var randVal = Math.random() * maxVal;
return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}
As, you can see in the above JavaScript function, there are two parameters. One for the maximum value(N) up to which random number have to be generated. The second parameter is optional which specifies number of digits after decimal point.If not provided, this function returns integer.
JavaScript function to get random number between a range
function GenerateRandom(min, max, floatVal) {var randVal = min + (Math.random() * (max - min));
return typeof floatVal == "undifined" ? Math.round(randVal) : randVal.toFixed(floatVal);
}
The above JavaScript funciton accepts
three parameters.The first and second parameter is mandatory while the
third is optional. The first and second parameter specifies the range
between which the random number has to be generated. The thir parameter
is optional which specifies number of floating point digits, if not
provided, the above JavaScript function returns integer random number.
Thursday, November 8, 2012
Xml data type is not supported in distributed queries. Remote object
I found one issue while coping records from latest version of sql to old version of
sql where xml datatype is not supported using linked server while fetching records
i found following error occurred
"Xml data type is not supported in distributed queries. Remote object
'Server1.dbMurli.dbo.TB_Murli' has xml column(s)."
Root Cause :
- XML column can not be accessed directly from the remote server....
- Following is the table structure in Remote server and Local server
CREATE TABLE TB_Murli
(
ID INT,
Column1 VARCHAR(10),
[Address] XML
)
Solution :
So, we can solve this issue as given below...
INSERT TB_Murli
SELECT * FROM
(
SELECT * FROM OPENQUERY(Server1,'SELECT ID, Column1,CAST([Address] AS NVARCHAR(MAX))
[Addres] FROM dbMurli.dbo.TB_Murli')
)AS XUsign the "OPENQUERY"
we can solve the issue...
In Short :-
XML is not supported in distributed queries. You could write a
passthrough query with OPENQUERY, and cast the XML column to
nvarchar(MAX). For instance:SELECT cast(xmlcol as xml) FROM OPENQUERY(REMOTESVR, 'SELECT cast(xmlcol AS nvarchar(MAX)) FROM db.dbo.tbl')
Subscribe to:
Comments (Atom)