久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='0cAA7'></bdo><ul id='0cAA7'></ul>
  • <legend id='0cAA7'><style id='0cAA7'><dir id='0cAA7'><q id='0cAA7'></q></dir></style></legend>
  • <tfoot id='0cAA7'></tfoot>

    <small id='0cAA7'></small><noframes id='0cAA7'>

  • <i id='0cAA7'><tr id='0cAA7'><dt id='0cAA7'><q id='0cAA7'><span id='0cAA7'><b id='0cAA7'><form id='0cAA7'><ins id='0cAA7'></ins><ul id='0cAA7'></ul><sub id='0cAA7'></sub></form><legend id='0cAA7'></legend><bdo id='0cAA7'><pre id='0cAA7'><center id='0cAA7'></center></pre></bdo></b><th id='0cAA7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0cAA7'><tfoot id='0cAA7'></tfoot><dl id='0cAA7'><fieldset id='0cAA7'></fieldset></dl></div>

        從 XSD 生成 SQL Server DB

        Generating SQL Server DB from XSD(從 XSD 生成 SQL Server DB)

            <tbody id='ccHXi'></tbody>

              <tfoot id='ccHXi'></tfoot>

                <legend id='ccHXi'><style id='ccHXi'><dir id='ccHXi'><q id='ccHXi'></q></dir></style></legend>
              • <i id='ccHXi'><tr id='ccHXi'><dt id='ccHXi'><q id='ccHXi'><span id='ccHXi'><b id='ccHXi'><form id='ccHXi'><ins id='ccHXi'></ins><ul id='ccHXi'></ul><sub id='ccHXi'></sub></form><legend id='ccHXi'></legend><bdo id='ccHXi'><pre id='ccHXi'><center id='ccHXi'></center></pre></bdo></b><th id='ccHXi'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ccHXi'><tfoot id='ccHXi'></tfoot><dl id='ccHXi'><fieldset id='ccHXi'></fieldset></dl></div>
                  <bdo id='ccHXi'></bdo><ul id='ccHXi'></ul>
                • <small id='ccHXi'></small><noframes id='ccHXi'>

                  本文介紹了從 XSD 生成 SQL Server DB的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  重復:從 XML 生成 SQL 架構

                  在我正在進行的項目中,我需要支持強類型數據集以將數據存儲為 XML,或將數據存儲在 sql server 中.現在我已經創建了 XSD 架構,我希望能夠使用 XSD 中定義的表和關系創建一個 sql server 數據庫.

                  In a project i am working on, i have a need to support either a strongly-typed dataset for storing the data as XML, or storing the data in sql server. Now i already have the XSD schema created and i would like to be able to create a sql server database using the tables and relationships defined in the XSD.

                  這可能嗎?如果是這樣,解決這個問題的最佳方法是什么?

                  Is this possible? and if so, what is the best way to approach this problem?

                  說明:我正在尋找的是一種在運行時使用 C# 和 SQL Server 通過代碼執行上述操作的方法.這個可以嗎?

                  Clarification: What i'm looking for is a way to do the above via code at runtime with C# and SQL Server. Can this be done?

                  推薦答案

                  我設法提出了以下基于 SQL Server 管理對象的類:

                  I managed to come up with the following class based on the SQL Server Management Objects:

                  using System;
                  using System.Collections.Generic;
                  using System.Data;
                  using System.Data.SqlClient;
                  using System.IO;
                  using System.Text;
                  using Microsoft.SqlServer.Management.Common;
                  using Microsoft.SqlServer.Management.Smo;
                  using Rule=System.Data.Rule;
                  
                  namespace XSD2SQL
                  {
                  public class XSD2SQL
                  {
                      private readonly Server _server;
                      private readonly SqlConnection _connection;
                      private Database _db;
                      private DataSet _source;
                      private string _databaseName;
                  
                      public XSD2SQL(string connectionString, DataSet source)
                      {
                          _connection = new SqlConnection(connectionString);
                          _server = new Server(new ServerConnection(_connection));
                          _source = source;
                      }
                  
                      public void CreateDatabase(string databaseName)
                      {
                          _databaseName = databaseName;
                          _db = _server.Databases[databaseName];
                          if (_db != null) _db.Drop();
                          _db = new Database(_server, _databaseName);
                          _db.Create();
                      }
                  
                      public void PopulateDatabase()
                      {
                          CreateTables(_source.Tables);
                          CreateRelationships();
                      }
                  
                      private void CreateRelationships()
                      {
                          foreach (DataTable table in _source.Tables)
                          {
                              foreach (DataRelation rel in table.ChildRelations)
                                  CreateRelation(rel);
                          }
                      }
                  
                      private void CreateRelation(DataRelation relation)
                      {
                          Table primaryTable = _db.Tables[relation.ParentTable.TableName];
                          Table childTable = _db.Tables[relation.ChildTable.TableName];
                  
                          ForeignKey fkey = new ForeignKey(childTable, relation.RelationName);
                          fkey.ReferencedTable = primaryTable.Name;
                  
                          fkey.DeleteAction = SQLActionTypeToSMO(relation.ChildKeyConstraint.DeleteRule);
                          fkey.UpdateAction = SQLActionTypeToSMO(relation.ChildKeyConstraint.UpdateRule);
                  
                  
                          for (int i = 0; i < relation.ChildColumns.Length; i++)
                          {
                              DataColumn col = relation.ChildColumns[i];
                              ForeignKeyColumn fkc = new ForeignKeyColumn(fkey, col.ColumnName, relation.ParentColumns[i].ColumnName);
                  
                              fkey.Columns.Add(fkc);
                          }
                  
                          fkey.Create();
                  
                      }
                  
                      private void CreateTables(DataTableCollection tables)
                      {
                          foreach (DataTable table in tables)
                          {                
                              DropExistingTable(table.TableName);
                              Table newTable = new Table(_db, table.TableName);
                  
                              PopulateTable(ref newTable, table);                
                              SetPrimaryKeys(ref newTable, table);
                              newTable.Create();
                  
                          }
                      }
                  
                      private void PopulateTable(ref Table outputTable, DataTable inputTable)
                      {
                          foreach (DataColumn column in inputTable.Columns)
                          {
                              CreateColumns(ref outputTable, column, inputTable);
                          }
                      }
                  
                      private void CreateColumns(ref Table outputTable, DataColumn inputColumn, DataTable inputTable)
                      {
                          Column newColumn = new Column(outputTable, inputColumn.ColumnName);
                          newColumn.DataType = CLRTypeToSQLType(inputColumn.DataType);
                          newColumn.Identity = inputColumn.AutoIncrement;
                          newColumn.IdentityIncrement = inputColumn.AutoIncrementStep;
                          newColumn.IdentitySeed = inputColumn.AutoIncrementSeed;
                          newColumn.Nullable = inputColumn.AllowDBNull;
                          newColumn.UserData = inputColumn.DefaultValue;
                  
                          outputTable.Columns.Add(newColumn);
                      }
                  
                      private void SetPrimaryKeys(ref Table outputTable, DataTable inputTable)
                      {
                          Index newIndex = new Index(outputTable, "PK_" + outputTable.Name);
                          newIndex.IndexKeyType = IndexKeyType.DriPrimaryKey;
                          newIndex.IsClustered = false;
                  
                          foreach (DataColumn keyColumn in inputTable.PrimaryKey)
                          {                                
                              newIndex.IndexedColumns.Add(new IndexedColumn(newIndex, keyColumn.ColumnName, true));                
                          }
                          if (newIndex.IndexedColumns.Count > 0)
                              outputTable.Indexes.Add(newIndex);
                      }
                  
                  
                  
                      private DataType CLRTypeToSQLType(Type type)
                      {
                          switch (type.Name)
                          {
                              case "String":
                                  return DataType.NVarCharMax;
                  
                              case "Int32":
                                  return DataType.Int;
                  
                              case "Boolean":
                                  return DataType.Bit;
                  
                              case "DateTime":
                                  return DataType.DateTime;
                  
                              case "Byte[]":
                                  return DataType.VarBinaryMax;
                  
                  
                          }
                  
                          return DataType.NVarCharMax;
                      }
                  
                      private ForeignKeyAction SQLActionTypeToSMO(Rule rule)
                      {
                          string ruleStr = rule.ToString();
                  
                          return (ForeignKeyAction)Enum.Parse(typeof (ForeignKeyAction), ruleStr);
                      }
                  
                      private void DropExistingTable(string tableName)
                      {
                          Table table = _db.Tables[tableName];
                          if (table != null) table.Drop();
                      }
                  
                  }
                  }
                  

                  它尚未經過嚴格測試,需要映射出更多 SQL 到 CLR 類型,但它確實創建了一個新數據庫,包含所有表、列、主鍵和外鍵.

                  It hasn't been rigorously tested yet, and there needs to be more SQL to CLR types mapped out, but it does create a new database, all the tables, columns, primary keys, and foreign keys.

                  要使此代碼正常工作,需要引用一些程序集:

                  For this code to work, a few assemblies need to be referenced:

                  Microsoft.SqlServer.ConnectionInfo
                  Microsoft.SqlServer.Management.Sdk.Sfc
                  Microsoft.SqlServer.Smo
                  Microsoft.SqlServer.SqlEnum
                  

                  希望這對其他人有所幫助.

                  Hope this helps someone else out.

                  這篇關于從 XSD 生成 SQL Server DB的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數調用按鈕 OnClick)
                    <tfoot id='SpTvl'></tfoot>
                      <tbody id='SpTvl'></tbody>
                  1. <small id='SpTvl'></small><noframes id='SpTvl'>

                      <bdo id='SpTvl'></bdo><ul id='SpTvl'></ul>
                      <legend id='SpTvl'><style id='SpTvl'><dir id='SpTvl'><q id='SpTvl'></q></dir></style></legend>
                      <i id='SpTvl'><tr id='SpTvl'><dt id='SpTvl'><q id='SpTvl'><span id='SpTvl'><b id='SpTvl'><form id='SpTvl'><ins id='SpTvl'></ins><ul id='SpTvl'></ul><sub id='SpTvl'></sub></form><legend id='SpTvl'></legend><bdo id='SpTvl'><pre id='SpTvl'><center id='SpTvl'></center></pre></bdo></b><th id='SpTvl'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='SpTvl'><tfoot id='SpTvl'></tfoot><dl id='SpTvl'><fieldset id='SpTvl'></fieldset></dl></div>

                          • 主站蜘蛛池模板: 日韩高清一区 | 日韩电影中文字幕在线观看 | 日本一区二区视频 | 色婷婷久久久久swag精品 | 成人免费网站视频 | 欧美日韩视频在线第一区 | 成人国产综合 | 91欧美| 九九热在线视频 | 久久精品一级 | 日本成人福利视频 | 91色综合| 亚洲精品乱码久久久久久9色 | av三级在线观看 | 日日夜夜天天 | 国内精品视频在线观看 | 青青草一区二区三区 | jlzzjlzz国产精品久久 | 国产精品激情 | 精品国产一区二区三区久久久蜜月 | 国产剧情久久 | 日日噜噜噜夜夜爽爽狠狠视频97 | 九九热精品视频 | 成人精品免费视频 | 日韩一区欧美一区 | 天天干成人网 | 亚洲欧美一区二区三区在线 | 在线午夜 | 国产精品一区二区三区久久久 | 免费在线精品视频 | 亚洲精品成人网 | 久久成人免费视频 | 国产在线一区二区三区 | 亚洲欧美在线观看 | 欧美色性| 国产成人精品一区二区三区网站观看 | www九色| 久久精点视频 | 久久久久国产一级毛片 | 欧美一区二区在线 | 国产欧美精品一区二区色综合朱莉 |