如何从sql server 2008调用webservice?

时间:2013-08-22 12:11:17

标签: asp.net sql web-services sql-server-2008 c#-4.0

我正在使用asp.net(4.0)数据库的Web应用程序我有SQL SERVER 2008.我创建了一个Web服务,它的功能是将数据插入数据库。我在本地IIS上部署了我的Web服务,在浏览器中工作正常意味着它在数据库中插入数据但我的任务是创建一个将参数传递给Web服务的过程,然后Web服务将插入该数据。我为此尝试过很多东西,但可以完成这项任务。知道如何从SQL SERVER调用Web服务。 我尝试了许多内容,例如下面链接和此URL上提到的类似技术

http://www.sqlservergeeks.com/forums/microsoft-data-platform/sql-server-bi/9/sql-programming-calling-the-web-service-thru-sql

我也尝试过终点但无法完成。甚至没有从SQL SERVER调用HelloWork方法。

Web服务名称WebService1.asmx 方法public string SaveRecord(string a,string b,string c,string d)

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }



    [WebMethod]
        public string SaveRecord(string userid, string fname, string lname, string email)
        {

            //here is my code to save records is database 
 return "";

        }

1 个答案:

答案 0 :(得分:0)

可以通过与SQLCLR集成从sql server调用Web服务。下面是CLR触发器的代码片段,它从DB接收有关已删除记录的数据,并通过Web服务调用将其发送到远程。

[SqlTrigger(Name = "OnDelete", Target = "Contact1", Event = "FOR DELETE")]
public static void OnDelete()
{
    string recid, accountno, address3;

    recid = accountno = address3 = string.Empty;

    var sqlContext = SqlContext.TriggerContext;
    using (var connection = new SqlConnection("context connection=true"))
    {
        var command = new SqlCommand("SELECT recid, accountno, address3 FROM DELETED; ", connection);
        connection.Open();

        using (var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                recid = reader[0].ToString();
                accountno = reader[1].ToString();
                address3 = reader[2].ToString();
            }
        }
    }

    if (!string.IsNullOrEmpty(address3))
    {
        var token = new CrmAuthenticationToken();
        token.AuthenticationType = 0;
        token.OrganizationName = "organization";

        using (var crmService = new CrmService())
        {
            crmService.Url = "http://domain:5555/MSCRMServices/2007/CrmService.asmx";
            crmService.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
            crmService.CrmAuthenticationTokenValue = token;
            crmService.UnsafeAuthenticatedConnectionSharing = true;
            crmService.PreAuthenticate = true;

            crmService.Delete(EntityName.contact.ToString(), new Guid(address3));
        }
    }
}