Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
ADO.NET 2.0 ha introdotto il supporto avanzato dei tipi per l'DataSet
tramite lo spazio dei nomi System.Data.SqlTypes. I tipi in System.Data.SqlTypes sono progettati per fornire tipi di dati con la stessa semantica e precisione dei tipi di dati in un database di SQL Server. Ogni tipo di dati in System.Data.SqlTypes ha un tipo di dati equivalente in SQL Server, con la stessa rappresentazione dei dati sottostante.
L'uso di System.Data.SqlTypes direttamente in un DataSet offre diversi vantaggi quando si usano i tipi di dati di SQL Server. System.Data.SqlTypes supporta la stessa semantica dei tipi di dati nativi di SQL Server. Se si specifica una delle System.Data.SqlTypes nella definizione di un DataColumn si elimina la perdita di precisione che può verificarsi durante la conversione di tipi di dati decimali o numerici in uno dei tipi di dati CLR (Common Language Runtime).
Esempio
Nell'esempio seguente viene creato un oggetto DataTable, definendo in modo esplicito i tipi di dati DataColumn usando System.Data.SqlTypes anziché i tipi CLR. Il codice riempie il DataTable con i dati della tabella Sales.SalesOrderDetail nel database AdventureWorks in SQL Server. L'output visualizzato nella finestra della console mostra il tipo di dati di ogni colonna e i valori recuperati da 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