none
Whether to use C# data types or SQL data types RRS feed

  • Question

  • Can anyone please tell me when I should use a C# data type compared to when I use a SQL data type.

    Is it determined by the source of the data , the processing of the data in the business logic or the destination of the data, or other ?

     

    eg if my information is sourced form a data entry form / user input and then goes through various stages of processing before being stored in a sql database, should the data retrieved from the user be C# data types which may then need to be cast to sql data types in order ot store them or can I / should I use sql data types from the beginning?

    Thank you

    lpbcorp

     

    Thursday, May 8, 2008 3:15 PM

Answers

  •  lpbcorp wrote:

    Can anyone please tell me when I should use a C# data type compared to when I use a SQL data type.

    Is it determined by the source of the data , the processing of the data in the business logic or the destination of the data, or other ?

     

    eg if my information is sourced form a data entry form / user input and then goes through various stages of processing before being stored in a sql database, should the data retrieved from the user be C# data types which may then need to be cast to sql data types in order ot store them or can I / should I use sql data types from the beginning?

    Thank you

    lpbcorp

     

     

    There is a mapping of Slq Dtata Types into .NET Data Types and vice versa.

     

    You read Sql Server Fields with a clear identification of Sql Data Types in the parameter string. In ExecuteReader ( ) command you get a set of rows and you have to cast them all as .NET types while reading them, for instance:

     

    cmdm.Parameters.Add ( "@months", SqlDbType.Int, 4, "@months" ).Value = month;

    SqlDataReader rdr = cmdm.ExecuteReader ( CommandBehavior.Default );

    if (rdr.HasRows != false)

    {

    foreach (System.Data.Common.DbDataRecord row in rdr)

    {

    if (row[ 0 ] != System.DBNull.Value)

    firstDate = (DateTime)row.GetValue ( 0 );

    else

    firstDate = DateTime.MinValue;

     

    This is a piece of code from the opposite (reverse) situation: writing to Sql Server:

     

    if (arrL0[ 6 ] == null)

    cmdm.Parameters.Add ( "@divYield", SqlDbType.Float, 8, "divYield" ).Value = (System.DBNull)arrL0[ 6 ];

else

cmdm.Parameters.Add ( "@divYield", SqlDbType.Float, 8, "divYield" ).Value = (Double)arrL0[ 6 ];

 

 

Thursday, May 8, 2008 4:48 PM

All replies

  •  lpbcorp wrote:

    Can anyone please tell me when I should use a C# data type compared to when I use a SQL data type.

    Is it determined by the source of the data , the processing of the data in the business logic or the destination of the data, or other ?

     

    eg if my information is sourced form a data entry form / user input and then goes through various stages of processing before being stored in a sql database, should the data retrieved from the user be C# data types which may then need to be cast to sql data types in order ot store them or can I / should I use sql data types from the beginning?

    Thank you

    lpbcorp

     

     

    There is a mapping of Slq Dtata Types into .NET Data Types and vice versa.

     

    You read Sql Server Fields with a clear identification of Sql Data Types in the parameter string. In ExecuteReader ( ) command you get a set of rows and you have to cast them all as .NET types while reading them, for instance:

     

    cmdm.Parameters.Add ( "@months", SqlDbType.Int, 4, "@months" ).Value = month;

    SqlDataReader rdr = cmdm.ExecuteReader ( CommandBehavior.Default );

    if (rdr.HasRows != false)

    {

    foreach (System.Data.Common.DbDataRecord row in rdr)

    {

    if (row[ 0 ] != System.DBNull.Value)

    firstDate = (DateTime)row.GetValue ( 0 );

    else

    firstDate = DateTime.MinValue;

     

    This is a piece of code from the opposite (reverse) situation: writing to Sql Server:

     

    if (arrL0[ 6 ] == null)

    cmdm.Parameters.Add ( "@divYield", SqlDbType.Float, 8, "divYield" ).Value = (System.DBNull)arrL0[ 6 ];

else

cmdm.Parameters.Add ( "@divYield", SqlDbType.Float, 8, "divYield" ).Value = (Double)arrL0[ 6 ];

 

 

Thursday, May 8, 2008 4:48 PM
  • Thank you

    lpbcorp

    Saturday, May 10, 2008 3:37 AM