PROCEDURES AND FUNCTIONS IN .NET LANGUAGES { //

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES { // this will be executed Console.WriteLine( i1 Equals i2 ) } if(i1.ComparesTo(i2) == 0) { // this will be executed Console.WriteLine( i1 Compares To i2 ) } There is one more behavior of a SqlType, which is due to its inability to represent a null. Reference types, such as a string, can produce a runtime error because of a null reference. This typically happens because of a coding error. The following code fragment shows a simple way this can happen. string str = null; // this will cause a runtime error // because of a null reference Console.WriteLine(str.Length); An instance of a SqlTypethat is representing a null can produce a similar sort of error. A runtime error will occur when an attempt is made to get the Valueproperty of the SqlTypeor cast it to a CLR type, if that SqlType is representing a null. The following code fragment shows where this will happen. SqlInt64 i64 = SqlInt64.Null; // this will cause a runtime error long l1 = i64.Value; // this will cause a runtime error too l1 = (long) i64; Even though casting an instance of a SqlTypethat is representing a null to a CLR type will cause a runtime error, casting it to another SqlTypewill not, assuming that cast is allowed. For example, the following code fragment will not cause a runtime error. Int32 i32 = SqlInt32.Null; // this will compile and work as expected SqlInt64 i64 = i32; I64 = SqlInt64.Null; // this will compile and work as expected i32 = (SqlInt32)i64; To summarize what we have covered on SqlType, we can say that a SqlType is a value type that can represent a null but otherwise behaves much like the CLR type that corresponds to it. A SqlTypeis used when it is

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

SYSTEM.DATA.SQLTYPES to decide which branch of the flow

SYSTEM.DATA.SQLTYPES to decide which branch of the flow to follow. However, the result of a SqlType comparison is a SqlBoolean, not a CLR Boolean. Some languages, including VB.NET and C#, extend this capability to types that are not CLR Boolean, if that type implements the CLR special named method op_True. SqlBoolean implements op_True, so a SqlType comparison may be the argument of a flow control expression in VB.NET or C#. The following C# code fragment shows this. SqlInt32 i1 = new SqlInt32(3); SqlInt32 i2 = new SqlInt32(3); // this will compile and behave as expected even // though the result of the comparison is SqlBoolean if(i1 == i2) { // this will be executed Console.WriteLine( i1 == i2 ) } Another factor that has to be considered when comparing one SqlType to another is that one or both may be representing a null. Any comparison of a SqlType representing null to another SqlType always results in SqlBoolean.False. This can result in two variables being neither equal nor not equal at the same time. The following code fragment shows an example of this. SqlInt32 i1 = new SqlInt32.Null; SqlInt32 i2 = new SqlInt32.Null; if(i1 == i2) { // this will be never executed Console.WriteLine( i1 == i2 ) } if(i1 != i2) { // this will be never executed Console.WriteLine( i1 != i2 ) } There are other equality operations besides comparison, and these behave differently with respect to a SqlTyperepresenting null. The Equals method, which all types have, and the CompareTo method will show the comparison of two SqlTypes both of which are representing a null as being equal. The following code example shows this. SqlInt32 i1 = new SqlInt32.Null; SqlInt32 i2 = new SqlInt32.Null; if(i1.Equals(i2))

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES The IsNull

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES The IsNull property has to be used to check whether an instance of a SqlTypeis representing a null. The following code fragment illustrates the proper way to check a SqlTypefor null. SqlBoolean b = new SqlBoolean(false); // this will check to see if b is null if(b.IsNull) { Console.WriteLine( b is null ); } Just as a SqlType cannot be compared to null, it cannot be set to null either. Again, this is because it is a value type. In fact there is no way to change the value of a SqlTypeto null if it is not null. You can make a variable of a SqlType null by assigning it a SqlType.Null, but you cannot change an existing value of a SqlType to null. The following code fragment shows assigning to a variable an instance of a SqlType that is representing null. // the variable b is not null SqlBoolean b = new SqlBoolean(false); // this will check to see if b is null if(b.IsNull) { This code will not execute Console.WriteLine( b is null ); } // this assigns a null to b b = SqlBoolean.Null; if(b.IsNull) { This code will execute Console.WriteLine( b is null ); } The comparison of one SqlType to another will always produce a SqlBoolean, not the CLR type Boolean. The assignment of a SqlType comparison to a bool will, therefore, produce a compile-time error. The following code fragment shows this. SqlInt32 i1 = new SqlInt32(3); SqlInt32 i2 = new SqlInt32(3); // this will produce a compile-time error bool b = i2 == i2; Many languages provide a number of flow control expressions, such as if and while. These expressions evaluate a CLR type Boolean and use it

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

SYSTEM.DATA.SQLTYPES // the following lines will compile without

SYSTEM.DATA.SQLTYPES // the following lines will compile without error // but will cause a runtime error Console.WriteLine(d.CompareTo( asdfasdf )); // this will cause a runtime error too Console.WriteLine(d.CompareTo(4.5)); // and so will thisConsole.WriteLine(d.CompareTo(s)); // this will work as expected Console.WriteLine(d.CompareTo((SqlDouble)s); The purpose of CompareTo is to determine the relative value of the instance of a SqlType to the obj. It is meant to be used to order objects. A 0 return value from CompareTomeans objis equal in value to the instance of SqlType. A positive return value means obj is greater in value than the instance of SqlType, and negative means the opposite. Each SqlType is a value type and has all the behaviors of any other value type. The CLR uses value types to represent primitive types like ints and floats. The memory for each is allocated in place, not on the heap, unless it is a member of reference type. The important thing about value types for methods used to implement stored procedures, functions, or triggers is that they do not have a null representation. See Appendix A for a discussion of value types. A SqlType also has the ability to represent a null value, which none of value types in the base class library of the CLR can do. It is important to understand how a SqlType represents a null because this behavior is probably quite different from what you are used to with value types. Every SqlType implements INullable, which has a single property named IsNull. In addition, a SqlType has a public static field named Null. The value of this field is an instance of the SqlTypethat is null. The signature of Null for SqlInt32follows. public static readonly SqlInt32 Null; Note that though a SqlType can represent null, it itself cannot be null, because it is a value type. This means it cannot be compared to null; doing this will always produce a compile-time error. The following code fragment illustrates this. SqlBoolean b = new SqlBoolean(false); // this will produce a compile-time error if(b == null) { Console.WriteLine( b is null ); }

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

SYSTEM.DATA.SQLTYPES Most of the SqlTypes implement many of

SYSTEM.DATA.SQLTYPES Most of the SqlTypes implement many of the specially named operators. Most of the operators are also implemented by CLR numeric types. The numeric operations that are implemented by a SqlType are just as performant as the ones on its corresponding CLR type in most cases, so typically there is no performance to be gained by coercing a SqlType to its corresponding CLR type to do a calculation. The one exception to this is SqlDecimal. The numeric operations that are part of SqlDecimal may not be as fast as those on the CLR decimal type, but they cover a much wider range of numbers. SqlDecimalcan represent any number that the SQL Server decimal can, which is not the case for the CLR decimal type. For example, the largest number that SqlDecimal can represent is 99999999999999999999999999999999999999, but the largest number that the CLR decimal type can represent is only 79228162514264337593543950335. This property of SqlDecimalmay cause a runtime error if it is coerced to the CLR decimaltype and the value of the SqlDecimalis out of the range of values that the CLR decimalcan represent. SqlDecimal sd = SqlDecimal.MaxValue; // this causes a conversion and produces a runtime error decimal d = sd.Value; The semantics of the operation invoked depends on the type. For arithmetic types like SqlInt32, the operands are added by the op_Addition method. For the SqlBinary type, the operands are concatenated. In general, the semantics will be intuitive. For specific details, consult the MSDN documentation. Table 3-4 shows the operations that each SqlType can perform. The first column on the left lists the SqlType. The columns are marked with an X to indicate that the operation listed at the head of the column is available for the SqlType in the same row on the left. The operation includes the symbol used by C# and the name of the operation. Other languages may use different symbols for these operations. For example, this shows that SqlBinarysupports the = =, +, >, >=, !=, < , and <= operations. Note that for SqlBoolean, the Ones Complement operator means Logical Not. Every SqlType implements the IComparable interface. This interface has one method, CompareTo. Its signature follows. int CompareTo(object obj) This signature is a bit deceptive. Even though it implies any object can be passed in, in fact only objects that are of the same type as the SqlType Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES Implicit coercion

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES Implicit coercion can be used to easily set the value of a SqlType. Also note that a SqlTypecannot be implicitly coerced to its corresponding CLR type. Some examples of this are shown in the following code fragment. // sets the Value of si to 12 SqlInt32 si = 12; // this will produce a compile-time error // because there is no implicit coercion from // SqlInt32 to int int i = si; // you must use explicit coercion int i1 = (int)si; SqlDouble and SqlSingle behave a bit differently from their CLR counterparts. SqlDoubleand SqlSinglecannot represent NaN or infinity. An attempt to set a SqlDoubleor SqlSingleto either of these will result in a runtime error. The following code fragment shows this. double d = double.Nan; // this will cause a runtime error SqlDouble sd = d; d = 4.7; // this will work as expected SqlDouble sd2 = d; The CLR defines several special names for methods that are used to implement binary and unary operators, such as op_Addition or op_LogicalNot. Most languages use these special names when a class implements them for operations like addition. Although the CLR doesn t prohibit it, some languages, including VB.NET and C#, do not allow you to use these names for methods in your classes or to call these methods. Instead, they have symbols that are used in their place for example + and ! . The methods for these special names, if a class has them, are implemented as static methods taking up to two instances of the class s type as parameters and returning an instance of the class s type. The following code fragment shows how SqlInt32implements op_Addition. public static SqlInt32 op_Addition(SqlInt32 x, SqlInt32 y); Because of this, you can add one SqlInt32 to another using the + operator. The following code fragment shows this. public SqlInt32 A = new SqlInt32(3); public SqlInt32 B = new SqlInt32(5); // this uses op_Addition to add A and B public SqlInt32 Sum = A + B;

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

SYSTEM.DATA.SQLTYPES So implicit coercion makes code easier to

SYSTEM.DATA.SQLTYPES So implicit coercion makes code easier to read and requires less typing, but implicit coercion should never lose precision, and not all SqlTypes can be implicitly coerced from one to another. Table 3-3 shows the implicit coercions between SqlTypes that are available. The left column and first row lists each SqlType. An X in a column indicates that the SqlType named in the first column can be implicitly coerced to the SqlTypelisted at the top of the column. For example, it shows that a SqlDecimal can be implicitly coerced to a SqlDecimal, a SqlDouble, and a SqlSingle. Table 3-3: Implicit SqlType Coercions SqlBinarySqlBooleanSqlDateSqlDateTimeSqlDecimalSqlDoubleSqlGuidSqlByteSqlInt16 SqlInt32 SqlInt64SqlMoneySqlSingleSqlStringSqlTimeSqlXML Reader SqlBinary X SqlBoolean X SqlDate X SqlDateTime X SqlDecimal X X X SqlDouble X SqlGuid X SqlByte X X X X X X X X SqlInt16 X X X X X X X SqlInt32 X X X X X X SqlInt64 X X X X X SqlMoney X X X X SqlSingle X X SqlString X SqlTime X SqlmlReader X

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES SqlString s2

PROCEDURES AND FUNCTIONS IN .NET LANGUAGES SqlString s2 = {90110409-6000-11D3-8CFE-0150048383C9} ; SqlGuid g = (SqlGuid)s2; // this line prints out 90110409-6000-11D3-8CFE-0150048383C9 Console.WriteLine(g); In addition to explicit coercions, implicit ones are supported. An implicit coercion means the type will automatically be coerced when required. For example, a method that is defined as returning a SqlInt64can return a SqlInt32, because there is an implicit coercion from SqlInt32to SqlInt64. An example of this follows. public class Math { public static SqlInt64 Same3(SqlInt32 i) { // i is implicitly cast to SqlInt64 // because that is what is required // for the return value return i; } } Another example of implicit coercions follows. SqlDecimal sdecimal = new SqlDecimal(1.0); SqlDouble sdouble = 1.0; // the following line does an explicit conversion sdecimal = (SqlDecimal)sdouble; // the following line does an implicit conversion // not that it does not require (SqlDecimal) Sdecimal = sdouble; // the following attempts an implicit conversion but will // not work because there is no implicit conversion // from SqlDouble to SqlDecimal sdecimal = sdouble; // compile-time error In addition, a CLR type that corresponds to a SqlTypecan be implicitly coerced to that SqlType. The CLR type that corresponds to a SqlType is the type of the Value property of the SqlType. For example, the CLR type of the Valueproperty of SqlDoubleis System.Double. This means that no explicit conversion is required to set the value of a SqlDouble with a CLR double. The example that follows shows this. SqlDouble sdouble = new SqlDouble(1.0); // no explicit conversion is required because 3.0 // is a double and the type of the Value property // of SqlDouble is double sdouble = 3.0;

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

SYSTEM.DATA.SQLTYPES Table 3-2: Explicit SqlType Coercions SqlBinarySqlBooleanSqlByteSqlDateSqlDateTimeSqlDecimalSqlDoubleSqlGuidSqlInt16 SqlInt32

SYSTEM.DATA.SQLTYPES Table 3-2: Explicit SqlType Coercions SqlBinarySqlBooleanSqlByteSqlDateSqlDateTimeSqlDecimalSqlDoubleSqlGuidSqlInt16 SqlInt32 SqlInt64SqlMoneySqlSingleSqlStringSqlTimeSqlXML Reader SqlBinary X X SqlBoolean X X X X X X X X X X SqlByte X X X SqlDate X SqlDateTime X X SqlDecimal X X X X X X X X SqlDouble X X X X X X X X X X SqlGuid X X X SqlInt16 X X X X SqlInt32 X X X X X SqlInt64 X X X X X X SqlMoney X X X X X X X SqlSingle X X X X X X X X X SqlString X X X X X X X X X X X X SqlTime X SqlXmlReader X It is interesting to note that SqlString can be explicitly coerced to almost any other SqlType. This mimics the behavior of strings within SQL Server itself. The following code fragment shows how explicit coercions can be used to coerce a SqlStringto some other SqlType. SqlString s = new SqlString( 1.01 ); SqlMoney m = (SqlMoney)s; // this line prints out 1.01 Console.WriteLine(m);

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services