久久久久久久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 架構

                  在我正在進行的項目中,我需要支持強類型數(shù)據(jù)集以將數(shù)據(jù)存儲為 XML,或?qū)?shù)據(jù)存儲在 sql server 中.現(xiàn)在我已經(jīng)創(chuàng)建了 XSD 架構,我希望能夠使用 XSD 中定義的表和關系創(chuàng)建一個 sql server 數(shù)據(jù)庫.

                  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 通過代碼執(zhí)行上述操作的方法.這個可以嗎?

                  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();
                      }
                  
                  }
                  }
                  

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

                  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模板網(wǎng)!

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

                  相關文檔推薦

                  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(從函數(shù)調(diào)用按鈕 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>

                          • 主站蜘蛛池模板: 久久av一区 | 欧美激情va永久在线播放 | 宅男伊人 | 天天射天天干 | 国产激情一区二区三区 | 国产精品一区二区三区在线 | 国产一区 | 99免费精品视频 | 欧美久久精品一级c片 | 欧美中文字幕在线观看 | www.久久久久久久久久久久 | 无码一区二区三区视频 | 国产乱码精品一区二区三区五月婷 | 黄网站涩免费蜜桃网站 | 久草新在线 | 日韩欧美一区在线 | 欧美成人久久 | 久久国产电影 | 欧美成人一区二区 | 最新超碰 | 电影91久久久| 国产在线观看不卡一区二区三区 | 亚洲视频免费播放 | 亚洲精品久久久久久国产精华液 | 亚洲美女av网站 | 欧美三级三级三级爽爽爽 | 欧美黄在线观看 | 中文字幕三区 | 亚洲午夜网 | 久热伊人 | www.成人在线视频 | 欧美精品在欧美一区二区少妇 | av一区二区在线观看 | 国产日韩在线观看一区 | 久久精品屋| 欧美 日韩 国产 成人 在线 91 | 99综合在线| 亚洲免费精品 | 国产精品1区2区3区 中文字幕一区二区三区四区 | 亚洲一区二区精品视频在线观看 | 成年人网站免费 |