SQL Server - Bài
Lập trình SQL Server với .NetVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftWhat is .NET?An application development platform from MicrosoftRapidly develop secure and robust softwareWeb and WindowsFull support for object-oriented programming MicrosoftMicrosoftAdvantages of .Net FrameworkCLR – Common Language RuntimeGarbage collector, type safety, JIT, …Language integrationC#, VB.net, …Built-in support for open standardsSOAP, …Simple development, simple deploymentSupporting entity data model (ADO.net & entity framework) MicrosoftMicrosoftOutline√.Net Framework ADO.net CLR IntegrationEnabling CLR IntegrationCLT Integration: Pros & Cons MicrosoftMicrosoft.Net FrameworkWebFormsWebServicesADO.NET and XMLBase Framework ClassesCommon Language RuntimeWinForms… MicrosoftMicrosoftDataReaderDataSetDataAdapterADO.NET ArchitectureCommandTransactionConnectionCommandBuilderDisconnectedLayerConnectedLayerDataLayer MicrosoftMicrosoftADO.NET Managed ProviderSystem.data.dll+{System.data}….IDbConnectionIDbCommandIDataReaderIDbTransactionIDbDataAdapter….System.data.dll+{System.data.SqlClient}….SqlConnectionSqlCommandSqlDataReaderSqlTransactionSqlDataAdapterSqlCommandBuilderWarning: There is no IDbCommandBuilder MicrosoftMicrosoftDataReaderFast Forward-Only/Read-Only streams of dataReturned by IDbCommand.ExecuteReader( .)Data is not cached on the client-sideMust be closed explicitlyNot serializableCannot be inherited (sealed class) MicrosoftMicrosoftDataSetIn-memory cache of dataRelational view of dataData source agnosticKeeps track of changesContains an XSD schemaMarshalByValue objectDataSetSchemaRelationTableColumnConstraintRow MicrosoftMicrosoftTypedDataSetWithout TypedDataSetWithout TypedDataSetWith TypedDataSetWith TypedDataSetCode is more readableIntroduces compile-time checkingIntellisense under Visual Studiodataset.Tables[0].Rows[0][1] = 1023;dataset.Tables[“Orders”].Rows[0][“CustomerID"] = 1023;dataset.Orders[0].CustomerID = 1023; [...]... Server, a new Provider Very similar to SqlClient Provider Follows Managed Provider Model Supports Microsoft IDbConnection IDbCommand IDbDataReader Etc InProc Managed Provider (2) Dude, where’s my Connection? In Out-of-proc ADO.NET: using (SqlConnection conn = new SqlConnection(" ")) using (SqlCommand cmd = conn.CreateCommand()) { try { cmd.CommandText = " "; conn.Open(); using (SqlDataReader... to VS about how to deploy Microsoft [SqlProcedure] [SqlFunction] [SqlUserDefinedType] [SqlUserDefinedAggregate] … Also used at runtime for behaviors of objects Managed Stored Procedures To expose a Stored Procedure: The containing class must be public The exposed method must be public The exposed method must be static public class SqlClr { public static void MyProc() { // Put your... Name>. CREATE FUNCTION Addition ( @x int, @y int ) RETURNS int AS EXTERNAL NAME ExampleYukon.SqlClr.Addition Microsoft Managed Functions (2) Code is similar to Stored Procedures [SqlFunction] public static int Addition(int x, int y) { return x + y; } [SqlFunction] Parameters Helps SQL Server know what the function does Microsoft IsDeterministic IsPrecise DataAccess SystemDataAccess... Custom Average Example [Serializable] [SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 80 00)] public class CustomAverage : IBinarySerialize { SqlDouble _total = 0; ulong _totalCount = 0; public void Init() { } public void Accumulate(SqlDouble Value) { ++_totalCount; if (_total.IsNull) _total = 0; _total += Value; } public void Merge(StdDev grp) {/* */ } public SqlDouble Terminate() { return _total/_totalCount;... Microsoft SqlDataAdapter XML File DB Outline √ √ Net Framework ADO.net CLR Integration Enabling CLR Integration CLT Integration: Pros & Cons Microsoft CLR Integration Brand new since SQL Server 2005 (Standard and Express) Write database queries using NET Supporting Any NET language (C#, VB, C++) Creating and debugging using VS 2005 IDE Microsoft What can we do with CLR code? Common T -SQL. .. EXTERNAL NAME AssemlyExample.SqlClr.InputProcedure CREATE PROCEDURE OutputProcedure @number int OUTPUT AS EXTERNAL NAME AssemlyExample.SqlClr.OutputProcedure CREATE PROCEDURE InOutProcedure @number int OUTPUT AS EXTERNAL NAME AssemlyExample.SqlClr.InOutProcedure NOTE: You don’t specify ReturnParameters CREATE PROCEDURE ReturnProcedure AS EXTERNAL NAME AssemlyExample.SqlClr.ReturnProcedure Microsoft... AssemlyExample.SqlClr.AddContact DDL Triggers CREATE TRIGGER ON AS EXTERNAL NAME .. CREATE TRIGGER AddUser ON DATABASE FOR CreateUser AS EXTERNAL NAME AssemlyExample.SqlClr.AddUser Microsoft TriggerContext ColumnsUpdates to see what columns changed public static void AddAuthor() { SqlTriggerContext... Type that does the aggregation Use DML command to register CREATE AGGREGATE ( @param ) RETURNS EXTERNAL NAME . CREATE AGGREGATE CustomAverage ( @value float ) RETURNS float EXTERNAL NAME AssemlyExample.CustomAverage Microsoft Custom Aggregates - CLR Class called during the aggregation Not just passed a set of values, but one at a time... is disposed } finally { conn.Close() } } // using statements ensure Disposed is called Microsoft InProc Managed Provider (3) InProc you can assume the connection using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = " "; SqlContext.Pipe.ExecuteAndSend(cmd); } Microsoft ... and the data Scale up/out possibilities of different tiers Abstraction of the database technology Security requirements Set-based versus procedural code Possibilities for using shared code libraries in multiple tiers Microsoft Enabling CLR Integration Enabled on an instance SQL Script Execute sp_configure ‘clr enabled’, ‘1’ Sp_reconfigure Surface Area Configuration (features) Microsoft Assembly . ProviderSystem.data.dll+{System.data}….IDbConnectionIDbCommandIDataReaderIDbTransactionIDbDataAdapter….System.data.dll+{System.data.SqlClient}….SqlConnectionSqlCommandSqlDataReaderSqlTransactionSqlDataAdapterSqlCommandBuilderWarning: There is no. Lập trình SQL Server với .NetVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftWhat