SqlTypes 및 DataSet

ADO.NET 2.0은 System.Data.SqlTypes 네임스페이스를 통해 DataSet에 대해 강화된 형식 지원을 도입했습니다. System.Data.SqlTypes의 형식은 SQL Server 데이터베이스의 데이터 형식과 의미 체계 및 정밀도가 동일한 데이터 형식을 제공하도록 디자인되었습니다. System.Data.SqlTypes의 각 데이터 형식은 SQL Server의 데이터 형식과 동일하며 기본 데이터 표현도 같습니다.

System.Data.SqlTypesDataSet를 직접 사용하면 SQL Server 데이터 형식으로 작업할 때 몇 가지 이점이 있습니다. System.Data.SqlTypes는 SQL Server 기본 데이터 형식과 동일한 의미 체계를 지원합니다. System.Data.SqlTypes 정의에 DataColumn 중 하나를 지정하면 decimal 또는 숫자 데이터 형식을 CLR(공용 언어 런타임) 데이터 형식 중 하나로 변환할 때 발생할 수 있는 전체 자릿수 손실을 방지할 수 있습니다.

예시

다음 예지에서는 DataTable 개체를 만들고 CLR 형식 대신 System.Data.SqlTypes를 사용하여 DataColumn 데이터 형식을 명시적으로 정의합니다. 이 코드는 SQL Server의 AdventureWorks 데이터베이스에 있는 Sales.SalesOrderDetail 테이블의 데이터로 DataTable을 채웁니다. 콘솔 창에 표시된 출력은 각 열의 데이터 형식과 SQL Server에서 검색한 값을 나타냅니다.

static void GetSqlTypesAW(string connectionString)
{
    // Create a DataTable and specify a SqlType
    // for each column.
    DataTable table = new();
    table.Columns.Add("SalesOrderID", typeof(SqlInt32));
    table.Columns.Add("UnitPrice", typeof(SqlMoney));
    table.Columns.Add("LineTotal", typeof(SqlDecimal));
    table.Columns.Add("ModifiedDate", typeof(SqlDateTime));

    // Open a connection to SQL Server and fill the DataTable
    // with data from the Sales.SalesOrderDetail table
    // in the AdventureWorks sample database.
    using (SqlConnection connection = new(connectionString))
    {
        const string queryString =
            "SELECT TOP 5 SalesOrderID, UnitPrice, LineTotal, ModifiedDate "
            + "FROM Sales.SalesOrderDetail WHERE LineTotal < @LineTotal";

        // Create the SqlCommand.
        SqlCommand command = new(queryString, connection);

        // Create the SqlParameter and assign a value.
        SqlParameter parameter =
            new("@LineTotal", SqlDbType.Decimal)
            {
                Value = 1.5
            };
        command.Parameters.Add(parameter);

        // Open the connection and load the data.
        connection.Open();
        SqlDataReader reader =
            command.ExecuteReader(CommandBehavior.CloseConnection);
        table.Load(reader);

        // Close the SqlDataReader.
        reader.Close();
    }

    // Display the SqlType of each column.
    Console.WriteLine("Data Types:");
    foreach (DataColumn column in table.Columns)
    {
        Console.WriteLine(" {0} -- {1}",
            column.ColumnName, column.DataType.UnderlyingSystemType);
    }

    // Display the value for each row.
    Console.WriteLine("Values:");
    foreach (DataRow row in table.Rows)
    {
        Console.Write(" {0}, ", row["SalesOrderID"]);
        Console.Write(" {0}, ", row["UnitPrice"]);
        Console.Write(" {0}, ", row["LineTotal"]);
        Console.Write(" {0} ", row["ModifiedDate"]);
        Console.WriteLine();
    }
}
Private Sub GetSqlTypesAW(ByVal connectionString As String)

    ' Create a DataTable and specify the
    ' SqlType for each column.
    Dim table As New DataTable()
    Dim icolumnolumn As DataColumn = _
      table.Columns.Add("SalesOrderID", GetType(SqlInt32))
    Dim priceColumn As DataColumn = _
      table.Columns.Add("UnitPrice", GetType(SqlMoney))
    Dim totalColumn As DataColumn = _
      table.Columns.Add("LineTotal", GetType(SqlDecimal))
    Dim columnModifiedDate As DataColumn = _
      table.Columns.Add("ModifiedDate", GetType(SqlDateTime))

    ' Open a connection to SQL Server and fill the DataTable
    ' with data from the Sales.SalesOrderDetail table
    ' in the AdventureWorks sample database.
    Using connection As New SqlConnection(connectionString)

        Dim queryString As String = _
           "SELECT TOP 5 SalesOrderID, UnitPrice, LineTotal, ModifiedDate " _
           & "FROM Sales.SalesOrderDetail WHERE LineTotal < @LineTotal"

        ' Create the SqlCommand.
        Dim command As SqlCommand = New SqlCommand(queryString, connection)

        ' Create the SqlParameter and assign a value.
        Dim parameter As SqlParameter = _
           New SqlParameter("@LineTotal", SqlDbType.Decimal)
        parameter.Value = 1.5
        command.Parameters.Add(parameter)

        ' Open the connection and load the data.
        connection.Open()
        Dim reader As SqlDataReader = _
           command.ExecuteReader(CommandBehavior.CloseConnection)
        table.Load(reader)

        ' Close the SqlDataReader
        reader.Close()
    End Using

    ' Display the SqlType of each column.
    Dim column As DataColumn
    Console.WriteLine("Data Types:")
    For Each column In table.Columns
        Console.WriteLine(" {0} -- {1}", _
        column.ColumnName, column.DataType.UnderlyingSystemType)
    Next column

    ' Display the value for each row.
    Dim row As DataRow
    Console.WriteLine("Values:")
    For Each row In table.Rows
        Console.Write(" {0}, ", row("SalesOrderID"))
        Console.Write(" {0}, ", row("UnitPrice"))
        Console.Write(" {0}, ", row("LineTotal"))
        Console.Write(" {0} ", row("ModifiedDate"))
        Console.WriteLine()
    Next row
End Sub

참고 항목