Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / System / data / design / TypedDatasetGenerator.cs / 9 / TypedDatasetGenerator.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All Rights Reserved. // Information Contained Herein is Proprietary and Confidential. // //----------------------------------------------------------------------------- namespace System.Data.Design { using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Design; using System.Diagnostics; using System.IO; using System.CodeDom; using System.CodeDom.Compiler; using System.Text; using System.Reflection; ////// Public class to expose the DataSource Generator functionality. /// public sealed class TypedDataSetGenerator { private static Assembly systemAssembly = Assembly.GetAssembly(typeof(System.Uri)); private static Assembly dataAssembly = Assembly.GetAssembly(typeof(System.Data.SqlClient.SqlDataAdapter)); private static Assembly xmlAssembly = Assembly.GetAssembly(typeof(System.Xml.Schema.XmlSchemaType)); private static Assembly[] fixedReferences = new Assembly[] { systemAssembly, dataAssembly, xmlAssembly}; private static Assembly[] referencedAssemblies = null; private static Assembly entityAssembly; private static string LINQOverTDSAssemblyName = "System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; private static string[] imports = new String[] { }; // we disable the warning about this type not being CLS-Compliant; the toolset is being updated and the type is compliant //#pragma warning disable 3003 public static ICollectionReferencedAssemblies { //#pragma warning restore 3003 get { return referencedAssemblies; } } [Flags] public enum GenerateOption { None = 0, HierarchicalUpdate = 1, LinqOverTypedDatasets = 2, } private TypedDataSetGenerator() { // avoid the class being Instantiated. } public static string GetProviderName(string inputFileContent) { return GetProviderName(inputFileContent, null); } public static string GetProviderName(string inputFileContent, string tableName) { // Validate parameters. if (inputFileContent == null || inputFileContent.Length == 0) { throw new ArgumentException(SR.GetString(SR.CG_DataSetGeneratorFail_InputFileEmpty)); } // Make sure the XMLContent is valid (can generate a DataSet). StringReader stringReader = new StringReader(inputFileContent); DesignDataSource designDS = new DesignDataSource(); try { designDS.ReadXmlSchema(stringReader); } catch (Exception e) { string errorMessage = SR.GetString(SR.CG_DataSetGeneratorFail_UnableToConvertToDataSet, CreateExceptionMessage(e)); throw new Exception(errorMessage, e); } if (tableName == null || tableName.Length == 0) { if (designDS.DefaultConnection != null) { return designDS.DefaultConnection.Provider; } } else { DesignTable designTable = designDS.DesignTables[tableName]; if (designTable != null) { return designTable.Connection.Provider; } } return null; } public static string Generate(DataSet dataSet, CodeNamespace codeNamespace, CodeDomProvider codeProvider) { if(codeProvider == null) { throw new ArgumentException("codeProvider"); } if (dataSet == null) { throw new ArgumentException(SR.GetString(SR.CG_DataSetGeneratorFail_DatasetNull)); } StringWriter writer = new StringWriter(System.Globalization.CultureInfo.CurrentCulture); dataSet.WriteXmlSchema(writer); StringBuilder schema = writer.GetStringBuilder(); return Generate(schema.ToString(), null, codeNamespace, codeProvider); } public static void Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, DbProviderFactory specifiedFactory) { if (specifiedFactory != null) { ProviderManager.ActiveFactoryContext = specifiedFactory; } try { Generate(inputFileContent, compileUnit, mainNamespace, codeProvider); } finally { ProviderManager.ActiveFactoryContext = null; } } public static void Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, Hashtable customDBProviders) { Generate(inputFileContent, compileUnit, mainNamespace, codeProvider, customDBProviders, GenerateOption.None); } public static void Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, Hashtable customDBProviders, GenerateOption option) { Generate(inputFileContent, compileUnit, mainNamespace, codeProvider, customDBProviders, option, null); } public static void Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, Hashtable customDBProviders, GenerateOption option, string dataSetNamespace) { if (customDBProviders != null) { ProviderManager.CustomDBProviders = customDBProviders; } try { Generate(inputFileContent, compileUnit, mainNamespace, codeProvider, option, dataSetNamespace); } finally { ProviderManager.CustomDBProviders = null; } } public static string Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider) { return Generate(inputFileContent, compileUnit, mainNamespace, codeProvider, GenerateOption.None); } public static string Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, GenerateOption option) { return Generate(inputFileContent, compileUnit, mainNamespace, codeProvider, option, null); } public static string Generate(string inputFileContent, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, GenerateOption option, string dataSetNamespace) { // Validate parameters. if (inputFileContent == null || inputFileContent.Length == 0) { throw new ArgumentException(SR.GetString(SR.CG_DataSetGeneratorFail_InputFileEmpty)); } if (mainNamespace == null) { throw new ArgumentException(SR.GetString(SR.CG_DataSetGeneratorFail_CodeNamespaceNull)); } if (codeProvider == null) { throw new ArgumentException("codeProvider"); } // Make sure the XMLContent is valid (can generate a DataSet). StringReader stringReader = new StringReader(inputFileContent); DesignDataSource designDS = new DesignDataSource(); try { designDS.ReadXmlSchema(stringReader); } catch (Exception e) { string errorMessage = SR.GetString(SR.CG_DataSetGeneratorFail_UnableToConvertToDataSet, CreateExceptionMessage(e)); throw new Exception(errorMessage, e); } return GenerateInternal(designDS, compileUnit, mainNamespace, codeProvider, option, dataSetNamespace); } internal static string GenerateInternal(DesignDataSource designDS, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider, GenerateOption generateOption, string dataSetNamespace) { if(StringUtil.Empty(designDS.Name)) { designDS.Name = "DataSet1"; } try { TypedDataSourceCodeGenerator generator = new TypedDataSourceCodeGenerator(); generator.CodeProvider = codeProvider; generator.GenerateSingleNamespace = false; if (mainNamespace == null) { mainNamespace = new CodeNamespace(); } if (compileUnit == null) { compileUnit = new CodeCompileUnit(); compileUnit.Namespaces.Add(mainNamespace); } generator.GenerateDataSource(designDS, compileUnit, mainNamespace, dataSetNamespace, generateOption); foreach (string import in imports) { mainNamespace.Imports.Add(new CodeNamespaceImport(import)); } } catch (Exception e) { string errorMessage = SR.GetString(SR.CG_DataSetGeneratorFail_FailToGenerateCode, CreateExceptionMessage(e)); throw new Exception(errorMessage, e); } System.Collections.ArrayList refAssemblies = new System.Collections.ArrayList(fixedReferences); refAssemblies.AddRange(TypedDataSourceCodeGenerator.GetProviderAssemblies(designDS)); // Add a reference to the new LINQ over Typed Datasets DLL if necessary if ((generateOption & GenerateOption.LinqOverTypedDatasets) == GenerateOption.LinqOverTypedDatasets) { Assembly entityAssembly = EntityAssembly; if (entityAssembly != null) { refAssemblies.Add(entityAssembly); } else { Debug.Fail("Could not load LinqOverTDS assembly!"); } } referencedAssemblies = (Assembly[])refAssemblies.ToArray(typeof(Assembly)); // Add the referenced assemblies to the CodeCompileUnit foreach (Assembly a in referencedAssemblies) { compileUnit.ReferencedAssemblies.Add(a.GetName().Name + ".dll"); } return designDS.GeneratorDataSetName; } private static Assembly EntityAssembly { get { if (entityAssembly == null) { try { entityAssembly = Assembly.Load(LINQOverTDSAssemblyName); } catch { } Debug.Assert(entityAssembly != null, "Could not load LinqOverTDS assembly!"); } return entityAssembly; } } private static string CreateExceptionMessage(Exception e) { string message = (e.Message != null ? e.Message : string.Empty); Exception innerException = e.InnerException; while (innerException != null) { string innerMessage = innerException.Message; if (innerMessage != null && innerMessage.Length > 0) { message = message + " " + innerMessage; } innerException = innerException.InnerException; } return message; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SessionStateContainer.cs
- RegexNode.cs
- CodeExpressionCollection.cs
- DataGridViewCellEventArgs.cs
- SqlDataSourceCache.cs
- GrammarBuilderBase.cs
- WebReferencesBuildProvider.cs
- Vector3DAnimationUsingKeyFrames.cs
- MaskInputRejectedEventArgs.cs
- HtmlInputSubmit.cs
- WebHostedComPlusServiceHost.cs
- PerspectiveCamera.cs
- PrtTicket_Public.cs
- HandlerFactoryWrapper.cs
- MouseGesture.cs
- Crypto.cs
- controlskin.cs
- PropertyItem.cs
- DataPager.cs
- FixedSOMContainer.cs
- InputLanguageProfileNotifySink.cs
- ResourceType.cs
- PointAnimationBase.cs
- PropertyRef.cs
- OracleBFile.cs
- ValidationSummary.cs
- DecoderBestFitFallback.cs
- HtmlElementErrorEventArgs.cs
- WmlObjectListAdapter.cs
- HasCopySemanticsAttribute.cs
- HttpProcessUtility.cs
- Tool.cs
- DataGridViewColumnCollectionDialog.cs
- LabelEditEvent.cs
- xmlfixedPageInfo.cs
- PropertyValidationContext.cs
- Int32RectConverter.cs
- Missing.cs
- VirtualPathUtility.cs
- AttachedPropertyMethodSelector.cs
- EventLogQuery.cs
- CodeTypeReferenceCollection.cs
- InfiniteIntConverter.cs
- ClientFormsIdentity.cs
- parserscommon.cs
- ClipboardProcessor.cs
- MemberRelationshipService.cs
- IDispatchConstantAttribute.cs
- TemplateParser.cs
- StateBag.cs
- PathTooLongException.cs
- CodeSubDirectory.cs
- WindowsFormsEditorServiceHelper.cs
- RequestQueryProcessor.cs
- TCPListener.cs
- CollectionViewGroup.cs
- ClonableStack.cs
- ListCollectionView.cs
- StylusPointPropertyInfo.cs
- BitmapVisualManager.cs
- FileStream.cs
- MatrixAnimationUsingKeyFrames.cs
- IpcClientManager.cs
- TextSerializer.cs
- NotSupportedException.cs
- AnonymousIdentificationSection.cs
- MutexSecurity.cs
- Switch.cs
- ArrayElementGridEntry.cs
- ImportContext.cs
- ContentFileHelper.cs
- DataGridViewCellStyleConverter.cs
- ElementUtil.cs
- Brush.cs
- DynamicRenderer.cs
- EventLogLink.cs
- TraceInternal.cs
- DispatcherEventArgs.cs
- FilteredAttributeCollection.cs
- oledbconnectionstring.cs
- ByteKeyFrameCollection.cs
- VarInfo.cs
- ServiceRouteHandler.cs
- MailMessageEventArgs.cs
- DefaultAsyncDataDispatcher.cs
- Array.cs
- IIS7UserPrincipal.cs
- WebPartsSection.cs
- SoapTypeAttribute.cs
- OptimisticConcurrencyException.cs
- QilXmlReader.cs
- DBConnection.cs
- FrameworkContentElement.cs
- CustomLineCap.cs
- DataGridCommandEventArgs.cs
- QilName.cs
- SimpleType.cs
- AdRotator.cs
- ProviderException.cs
- TextEditorTables.cs