2

我是初学者,我想从 Zoho Crm 获取数据,然后通过调用 C# 中的存储过程将其插入 SQL Server 数据库

我的方式 :

  1. 使用 Zoho API whit URL(效果很好)

  2. 用于从上面的 URL 加载XMLXmlDocument数据(效果很好)

我的代码:

XmlDocument doc = new XmlDocument();
doc.Load("https://crm.zoho.com/crm/private/xml/Accounts/getRecords?newFormat=1&authtoken=mytoken&scope=crmapi&lastModifiedTime=2011-05-27%2000:00:00&fromIndex=1482&toIndex=1682");

现在如何使用存储过程插入数据库?

数据示例:

注意:每行中FL元素的数量可能不同

<response uri="/crm/private/xml/Accounts/getRecords">
  <result>
    <Accounts>
      <row no="1">
        <FL val="ACCOUNTID">380198000000068646</FL>
        <FL val="SMOWNERID">380198000000052013</FL>
        <FL val="Account Owner">Mai Tran</FL>
        <FL val="Account Name">Trung Tâm Nghiên Cứu & Phát Triển Chế Biến Dầu Khí</FL>
        <FL val="Website">www.pvpro.com.vn</FL>
        <FL val="Industry">Energy, Oil & Gas</FL>
        <FL val="SMCREATORID">380198000000041003</FL>
        <FL val="Created By">Bach Tran Phuong</FL>
        <FL val="MODIFIEDBY">380198000000041003</FL>
        <FL val="Modified By">Tran Phuong</FL>
        <FL val="Created Time">2011-05-04 09:49:11</FL>
        <FL val="Modified Time">2012-02-24 14:41:48</FL>
        <FL val="Billing Street">4 Nguyen Thong St</FL>
        <FL val="District">Q.3</FL>
        <FL val="City">Hồ Chí Minh</FL>
      </row>
      <row no="2">
        <FL val="ACCOUNTID">380198000000068644</FL>
        <FL val="SMOWNERID">380198000000041003</FL>
        <FL val="Account Owner">Bach Tran Phuong</FL>
        <FL val="Account Name">Tổng Cty Tư Vấn Thiết Kế&lt;/FL>
        <FL val="Phone">6297 1778</FL>
        <FL val="SMCREATORID">380198000000041003</FL>
        <FL val="Created By">Bach Tran Phuong</FL>
        <FL val="MODIFIEDBY">380198000000041003</FL>
        <FL val="Modified By">Tran Phuong</FL>
        <FL val="Created Time">2011-05-04 09:49:11</FL>
        <FL val="Modified Time">2012-02-24 14:40:42</FL>
        <FL val="Billing Street">Parkson Tân Sân Nhất</FL>
        <FL val="District">Tân Bình</FL>
        <FL val="City">Hồ Chí Minh</FL>
        <FL val="Alternative Names">PV Engineering</FL>
      </row>
      <row no="3">
        <FL val="ACCOUNTID">380198000000068642</FL>
        <FL val="SMOWNERID">380198000000052015</FL>
        <FL val="Account Owner">Trang Do Thi</FL>
        <FL val="Account Name">CTY TNHH BẢO HIỂM NHÂN THỌ VIỆT NAM</FL>
        <FL val="Phone">9 101 660</FL>
        <FL val="Account Type">Active</FL>
        <FL val="Industry">Financial Services</FL>
        <FL val="SMCREATORID">380198000000041003</FL>
        <FL val="Created By">Bach Tran Phuong</FL>
        <FL val="MODIFIEDBY">380198000000041003</FL>
        <FL val="Modified By">Tran Phuong</FL>
        <FL val="Created Time">2011-05-04 09:49:11</FL>
        <FL val="Modified Time">2012-04-27 11:02:25</FL>
        <FL val="Billing Street"> Tôn Đức Thắng P.Bến Nghé</FL>
        <FL val="Phone 2">9 101 659</FL>
        <FL val="District">Q.1</FL>
        <FL val="City">Hồ Chí Minh</FL>
        <FL val="Key Contact">Duy Nhật</FL>
        <FL val="Partner Type">Loyal Partner</FL>
        <FL val="Collection">1859353554</FL>
        <FL val="Alternative Names">Việt Nam</FL>
      </row>
      <row no="4">
        <FL val="ACCOUNTID">380198000000068638</FL>
        <FL val="SMOWNERID">380198000000052015</FL>
        <FL val="Account Owner">Trang Do Thi</FL>
        <FL val="Account Name">Cty TNHH Gia Dụng Phương Gia</FL>
        <FL val="SMCREATORID">380198000000041003</FL>
        <FL val="Created By">Bach Tran Phuong</FL>
        <FL val="MODIFIEDBY">380198000000041003</FL>
        <FL val="Modified By">Bach Tran Phuong</FL>
        <FL val="Created Time">2011-05-04 09:49:11</FL>
        <FL val="Modified Time">2012-02-24 13:52:25</FL>
        <FL val="Billing Street">Cao Thang</FL>
        <FL val="District">Q.3</FL>
        <FL val="City">Hồ Chí Minh</FL>
        <FL val="Alternative Names">Home Appliances ltd</FL>
      </row>
    </Accounts>
  </result>
</response>
4

1 回答 1

3

第 1 步:创建您的存储过程:

CREATE PROCEDURE dbo.StoreXmlData (@XmlData XML)
AS BEGIN
    INSERT INTO dbo.YourXmlTable(XmlColumnName)
    VALUES(@XmlData)
END

这假设您使用的是 SQL Server 2005或更高版本,并且您将 XML 存储在一个真正属于 datatype 的列中XML。根据需要调整表和列名称以匹配您的情况。

第 2 步:从 C# 中,使用标准 ADO.NET 类调用您的存储过程:

string sqlStmt = "dbo.StoreXmlData";  // name of your stored procedure

// here, you need to use *YOUR* connection string instead of my demo one...    
using(SqlConnection conn = new SqlConnection("server=.;database=test;integrated security=SSPI;"))
using (SqlCommand cmd = new SqlCommand(sqlStmt, conn))
{
    // make sure to tell ADO.NET you're about to call a stored procedure!
    cmd.CommandType = CommandType.StoredProcedure;

    // define the parameter and set its value
    cmd.Parameters.Add("@YourData", SqlDbType.Xml);
    cmd.Parameters["@YourData"].Value = doc.DocumentElement.InnerXml;

    // standard ADO.NET - open connection, execute query, close connection
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

这就是全部!

更新:好的,所以您实际上希望将 XML 分解为单独的列 - 在这种情况下,您需要在存储过程中执行以下操作:

CREATE PROCEDURE dbo.StoreXmlData (@XmlData XML)
AS 
BEGIN
    INSERT INTO dbo.YourDataTable(RowNo, AccountID, OwnerID, AccountOwner)
        SELECT
            RowNo = Acc.value('@no', 'int'),
            AccountID = Acc.value('(FL[@val="ACCOUNTID"])[1]', 'nvarchar(50)'),
            OwnerID = Acc.value('(FL[@val="SMOWNERID"])[1]', 'nvarchar(50)'),
            AccountOwner = Acc.value('(FL[@val="Account Owner"])[1]', 'nvarchar(50)')
            -- and so on to grab all the items you need
        FROM
            @XmlData.nodes('/response/result/Accounts/row') AS Resp(Acc)
END
于 2012-05-04T04:52:23.807 回答