다음을 통해 공유


SQL 유형 및 데이터 집합

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

System.Data.SqlTypes을(를) DataSet에서 사용할 경우, SQL Server 데이터 형식 작업에 여러 가지 이점이 있습니다. System.Data.SqlTypes 는 SQL Server 네이티브 데이터 형식과 동일한 의미 체계를 지원합니다. 정의 System.Data.SqlTypes 에서 하나를 DataColumn 지정하면 소수 또는 숫자 데이터 형식을 CLR(공용 언어 런타임) 데이터 형식 중 하나로 변환할 때 발생할 수 있는 정밀도 손실이 제거됩니다.

예시

다음 예제에서는 CLR 유형 대신 DataTable를 사용하여 DataColumn 데이터 유형을 명시적으로 정의하고, System.Data.SqlTypes 개체를 만듭니다. 이 코드는 SQL Server의 DataTable AdventureWorks 데이터베이스에 있는 Sales.SalesOrderDetail 테이블의 데이터로 채웁니다. 콘솔 창에 표시되는 출력에는 각 열의 데이터 형식과 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($" {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

참고하십시오