SQL Server DB to XSD - can it be done thru code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2lwcHl1Y29ubg==?=

    SQL Server DB to XSD - can it be done thru code

    Hi

    In C# ide if you have a SqlServer attached and Create a New XSD Item in a
    solution you can then drag a db from the SQLServer and ti creates a XSD
    modelled after the db. Can this be done thru code???

    If I have a EXE that is passed a db can I generate a XSD on the fly???

    Thanks
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: SQL Server DB to XSD - can it be done thru code

    sippyucon,

    You ^might^ be able to get it through a call to GetSchema on a
    SqlConnection. This will return a DataTable, which you might be able to use
    to create the XSD schema for the table in question.

    My guess, though, is that this won't give you what you want, and that
    the functionality you are seeing is IDE-specific, and not accessible through
    the framework (which means you will have to do this on your own).


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "sippyuconn " <sippyuconn@new sgroup.nospamwr ote in message
    news:363438AA-A889-4BD6-AFD8-1985C1CAD17A@mi crosoft.com...
    Hi
    >
    In C# ide if you have a SqlServer attached and Create a New XSD Item in a
    solution you can then drag a db from the SQLServer and ti creates a XSD
    modelled after the db. Can this be done thru code???
    >
    If I have a EXE that is passed a db can I generate a XSD on the fly???
    >
    Thanks

    Comment

    • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

      #3
      RE: SQL Server DB to XSD - can it be done thru code

      You can generate a schema from a DataSet, so either construct one in code or
      Fill one with a DataAdapter and call the GetXmlSchema function which returns
      it as a string.

      e.g:

      DataSet ds = new DataSet();

      ds.Tables.Add(" table 1");
      ds.Tables[0].Columns.Add("c ol1", typeof(int));
      ds.Tables[0].Columns.Add("c ol2", typeof(DateTime ));
      ds.Tables[0].Columns.Add("c ol3", typeof(string)) ;
      ds.Tables[0].Columns.Add("c ol4", typeof(Guid));

      File.WriteAllTe xt("C:\\sch.xsd ",ds.GetXmlSche ma());



      this makes:

      <?xml version="1.0" encoding="utf-16"?>
      <xs:schema id="NewDataSet " xmlns=""
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata">
      <xs:element name="NewDataSe t" msdata:IsDataSe t="true"
      msdata:UseCurre ntLocale="true" >
      <xs:complexType >
      <xs:choice minOccurs="0" maxOccurs="unbo unded">
      <xs:element name="table_x00 20_1">
      <xs:complexType >
      <xs:sequence>
      <xs:element name="col1" type="xs:int" minOccurs="0" />
      <xs:element name="col2" type="xs:dateTi me" minOccurs="0" />
      <xs:element name="col3" type="xs:string " minOccurs="0" />
      <xs:element name="col4" msdata:DataType ="System.Gui d,
      mscorlib, Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9"
      type="xs:string " minOccurs="0" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:choice>
      </xs:complexType>
      </xs:element>
      </xs:schema>
      --
      Ciaran O''Donnell
      try{ Life(); } catch (TooDifficultException) { throw Toys(); }



      "sippyuconn " wrote:
      Hi
      >
      In C# ide if you have a SqlServer attached and Create a New XSD Item in a
      solution you can then drag a db from the SQLServer and ti creates a XSD
      modelled after the db. Can this be done thru code???
      >
      If I have a EXE that is passed a db can I generate a XSD on the fly???
      >
      Thanks

      Comment

      • =?Utf-8?B?SGVyYmVl?=

        #4
        RE: SQL Server DB to XSD - can it be done thru code

        that creates a xml including the schema:

        SELECT * from [tablename] FOR XML AUTO, XMLSCHEMA, ELEMENTS, ROOT
        ('tablename')


        "sippyuconn " wrote:
        Hi
        >
        In C# ide if you have a SqlServer attached and Create a New XSD Item in a
        solution you can then drag a db from the SQLServer and ti creates a XSD
        modelled after the db. Can this be done thru code???
        >
        If I have a EXE that is passed a db can I generate a XSD on the fly???
        >
        Thanks

        Comment

        Working...