Click here to Skip to main content
15,906,626 members
Articles / Programming Languages / C#
Tip/Trick

SqlXml Updategram Runner

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
31 Dec 2013CPOL 7.3K   35   1  
Visual Studio extension that can run the Updategram

Introduction

As a BizTalk developer, I need to test the Updategram frequently, so I think it will be good to create a Visual Studio Extension that can run the Updategram file against SQL server connection.

Background

The extension works with SQLXML to execute the Updategram file.

Using the Code

This tip utilizes [VSIX] rather than normal Add-in approach because I want to learn the new VSIX project template and also I want to list my extension in Visual Studio gallery.

The idea behind the extension is very simple; which takes the active document file location in Visual Studio and passes it to the SqlXmlCommand object.

C#
private void MenuItemRunCallback(object sender, EventArgs e)
  {
      bool proceed = false;
      String inputFilepath = "";
      if (dte.ActiveDocument != null)
      {
          if (Path.GetExtension(dte.ActiveDocument.FullName).EndsWith("xml"))
          {
              proceed = true;
              inputFilepath = dte.ActiveDocument.FullName;
          }
      }
      if (!proceed)
      {
          MessageBox.Show("to run updategram please select a valid XML file  ",
                          "invalid file ",
                          MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
      }

      try
      {
          FileStream FileStrm = new FileStream(inputFilepath, FileMode.Open);
          SqlConnectionStringBuilder objSB1 = new
                              SqlConnectionStringBuilder(dcd.ConnectionString);
          strDatabase = objSB1.InitialCatalog;
          strServer = objSB1.DataSource;
          connectionString = "Provider=SQLNCLI11;"
                  + "Server=" + strServer + ";"
                  + "Database=" + strDatabase + ";"
                  + "Integrated Security=SSPI;"
                  + "DataTypeCompatibility=80;";
          SqlXmlCommand cmd = new SqlXmlCommand(connectionString);
          owP.OutputString("Connect to " + connectionString + "\r\n\r\n");
          cmd.CommandStream = FileStrm;
          owP.OutputString("Open file  " + inputFilepath + "\r\n\r\n");
          cmd.CommandType = SqlXmlCommandType.UpdateGram;

          Stream outStrm = cmd.ExecuteStream();
          StreamReader reader = new StreamReader(outStrm);
          owP.OutputString("================Outputs====================\r\n\r\n");
          owP.OutputString(reader.ReadToEnd() + "\r\n\r\n");
          FileStrm.Close();
      }
      catch (Exception ex)
      {

          owP.OutputString("================Exception====================\r\n");
          owP.OutputString(ex.Message + "\r\n\r\n");
      }
      finally
      {
          owP.Activate();
      }
  }

How To Use It?

The user can open a valid updategram XML, then select Run As Updategram from SqlXml Updategram Runner from Tools menu.

Image 1

Sample Updategram File

The below file will update the Orders and Order Details from Northwind database.

Note that the file should have urn:schemas-microsoft-com:xml-updategram namespace and it uses updg:at-identity to maintain the identity fields.

XML
<ns0:req xmlns:ns0="http://Northwind/Orders" xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
  <updg:sync>
    <updg:after>
      <ns0:Orders
          updg:at-identity="io"     
          CustomerID	="VINET"
          EmployeeID	="5"
          OrderDate	="2002-09-24"
          RequiredDate	="2002-09-24"
          ShippedDate	="2002-09-24"
          ShipVia	="3"
          Freight	="32.38"
          ShipName	="Test Ship"
          ShipAddress	=" Ship Address"
          ShipCity	="Aqaba"
          ShipPostalCode	="51100"
          ShipCountry	="Jordan"
       />
      <ns0:_x005b_Order_x0020_Details_x005d_
        OrderID="io"	ProductID="11"	
        UnitPrice="15"	Quantity="12"	Discount="0"/>
      <ns0:_x005b_Order_x0020_Details_x005d_
        OrderID="io"	ProductID="42"	
        UnitPrice="114"	Quantity="10"	Discount="0"/>
    </updg:after>
  </updg:sync>
</ns0:req> 

Where to Download

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Jordan Jordan
web developer
email : mohammed.barqawi AT gmail.com

Comments and Discussions

 
-- There are no messages in this forum --