分享方式:


SqlTypes 和資料集

下載 ADO.NET

ADO.NET 2.0 透過 System.Data.SqlTypes 命名空間引進了對 DataSet 的增強類型支援。 System.Data.SqlTypes 中的類型其設計目的是提供與 SQL Server 資料庫中的資料類型具有相同語意及精確度的資料類型。 System.Data.SqlTypes 中的每個資料類型,在 SQL Server 中都具有對應的資料類型,並具有相同的基礎資料表示。

直接在 DataSet 中使用 System.Data.SqlTypes,會在使用 SQL Server 資料類型時帶來數個優點。 System.Data.SqlTypes 支援與 SQL Server 原生資料類型相同的語意。 在 DataColumn 的定義中指定其中一個 System.Data.SqlTypes,在將十進位或數值資料類型轉換為通用語言執行平台 (CLR) 資料類型之一時就不會發生精確度遺失。

下列範例建立 DataTable 物件,其會使用 System.Data.SqlTypes 而非 CLR 類型,來明確定義 DataColumn 資料類型。 該程式碼會將 SQL Server 中 AdventureWorks 資料庫內 Sales.SalesOrderDetail 資料表的資料,填入 DataTable。 主控台視窗中顯示的輸出會顯示每個資料行的資料類型,以及擷取自 SQL Server 的值。

using Microsoft.Data.SqlClient;
using System.Data.SqlTypes;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        GetSqlTypesAW(connectionString);
        Console.ReadLine();
    }
    static private void GetSqlTypesAW(string connectionString)
    {
        // Create a DataTable and specify a SqlType
        // for each column.
        DataTable table = new DataTable();
        DataColumn icolumnolumn =
            table.Columns.Add("SalesOrderID", typeof(SqlInt32));
        DataColumn priceColumn =
            table.Columns.Add("UnitPrice", typeof(SqlMoney));
        DataColumn totalColumn =
            table.Columns.Add("LineTotal", typeof(SqlDecimal));
        DataColumn columnModifiedDate =
            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 SqlConnection(connectionString))
        {
            string queryString =
                "SELECT TOP 5 SalesOrderID, UnitPrice, LineTotal, ModifiedDate "
                + "FROM Sales.SalesOrderDetail WHERE LineTotal < @LineTotal";

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

            // Create the SqlParameter and assign a value.
            SqlParameter parameter =
                new SqlParameter("@LineTotal", SqlDbType.Decimal);
            parameter.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();
        }
    }

    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file, using the 
        // System.Configuration.ConfigurationSettings.AppSettings property 
        return "Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI;";
    }
}