Build Data-Driven Web Services with Updated XML Support for SQL Server 2000
2007-11-11 11:29:29 来源:WEB开发网核心提示:·sql server(WINDOWS平台上强大的数据库平台) 2000企业版安装教程·visual studio 2005 简体中文团队开发·Java连接sql server(WINDOWS平台上强大的数据库平台) 2000·用Java访问sql server(
- ·sql server(WINDOWS平台上强大的数据库平台) 2000企业版安装教程
·visual studio 2005 简体中文团队开发
·Java连接sql server(WINDOWS平台上强大的数据库平台) 2000
·用Java访问sql server(WINDOWS平台上强大的数据库平台) 2000数据库
·一次sql server(WINDOWS平台上强大的数据库平台) 2000 安装奇遇
·不用安装客户端连接sql server(WINDOWS平台上强大的数据库平台) 2000
·Java 实现连接sql server(WINDOWS平台上强大的数据库平台) 2000(JDBC
·sql server(WINDOWS平台上强大的数据库平台) 2000安装和基本框架
·剖析sql server(WINDOWS平台上强大的数据库平台) 2005查询通知之基础篇
·sql server(WINDOWS平台上强大的数据库平台) 2005 Beta 2 Transact-SQL
Download the code for this article: SQLxml(标准化越来越近了)3.exe (239KB) | ---> |
SUMMARY xml(标准化越来越近了) is becoming the ubiquitous data format on the Web, and xml(标准化越来越近了) support in sql server(WINDOWS平台上强大的数据库平台) is evolving to meet the additional demand. Using xml(标准化越来越近了), SOAP, HTTP, and sql server(WINDOWS平台上强大的数据库平台), you can now build powerful Web Services easily. To show just how simple it is with SQLxml(标准化越来越近了) 3.0, this article walks the reader through the process step by step, from setting up a virtual directory enabling data access via HTTP to executing queries and building Web Services. Finally, the author illustrates the creation of two Web Services clients梠ne with C# that works with the Microsoft .NET Framework and one with the SOAP Toolkit 2.0 for anyone still using earlier development tools. |
(标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图一)" width="44" height="63" />t's hard to believe that xml(标准化越来越近了) support in sql server(WINDOWS平台上强大的数据库平台)?2000 has been around for over two years. In the software world, that's a lifetime. sql server(WINDOWS平台上强大的数据库平台) 2000 was the first version to provide native support, and this was limited to the more basic xml(标准化越来越近了) feature set (template queries, mapping schemas, and OPENxml(标准化越来越近了)). Using simple HTTP queries you could retrieve formatted relational data in xml(标准化越来越近了) format. With a little help and some Extensible Stylesheet Language (XSL) magic, you could spit out the data in a formatted, HTML-friendly manner. Later, with the introduction of features like updategrams, you could easily submit an xml(标准化越来越近了)-based SQL template to insert or update rows of information in sql server(WINDOWS平台上强大的数据库平台) with little effort. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Initially, I thought that some would consider xml(标准化越来越近了) support a frivolous addition to an already powerful product. If a developer wasn't displaying SQL data in a Web page or feeding a system that only speaks xml(标准化越来越近了), were these features all that useful? (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Previously, the only viable approach for accessing data, for the middle-tier anyway, was through a traditional data access layer built with ODBC, OLE DB, or ADO. Now with SQLxml(标准化越来越近了) 3.0, sql server(WINDOWS平台上强大的数据库平台) 2000, SOAP, BizTalk? and the .NET Framework, xml(标准化越来越近了) is no longer a frivolous addition梚t's the data language of choice. Using SQLxml(标准化越来越近了) 3.0 for Data Access (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />SQLxml(标准化越来越近了) 3.0 is the third iteration of xml(标准化越来越近了) support for sql server(WINDOWS平台上强大的数据库平台). The biggest difference between the old way of representing data and the way it's represented with xml(标准化越来越近了) is how the rowset is created, where it is created (server-side or client-side), and how it is formatted (raw, nested, element-based, or attribute-based). For more on raw and explicit formats, refer to the information listed in the article summary. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />For those of you already working with some of the .NET server products such as BizTalk, managed classes, and the like, you already know how important it is to use xml(标准化越来越近了) as your data format. If using xml(标准化越来越近了) for data access is new to you, this may take some getting used to. If you choose to use xml(标准化越来越近了) as your data format, you must take into account the subtle differences between relational and hierarchical representation and how you can exploit the benefits of a hierarchy. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />If you are upgrading from a previous version, you can still run SQLxml(标准化越来越近了) 3.0 side by side with your current version. (See the sidebars "Side-by-side Support" and "Evolution of xml(标准化越来越近了) Support" for more information.) Querying sql server(WINDOWS平台上强大的数据库平台) with xml(标准化越来越近了) (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The fastest way to begin accessing sql server(WINDOWS平台上强大的数据库平台) 2000 using xml(标准化越来越近了) is through your browser. This is a great way to check whether you have everything set up correctly, and is also your first means of diagnosing problems should they appear. To access sql server(WINDOWS平台上强大的数据库平台) using a URL via the browser or any HTTP client, you must first set up a virtual directory for sql server(WINDOWS平台上强大的数据库平台) using the Microsoft?Management Console (MMC) snap-in provided with any of the releases. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />If you want to set up a virtual directory to perform template queries, you can still use the MMC snap-in provided with the original installation of sql server(WINDOWS平台上强大的数据库平台). This can be found in the sql server(WINDOWS平台上强大的数据库平台) 2000 program group under Configure SQL xml(标准化越来越近了) Support in IIS. However, to take advantage of SQLxml(标准化越来越近了) 3.0 features, I recommend selecting the MMC snap-in found in the SQLxml(标准化越来越近了) 3.0 program group under Configure IIS Support. Here you can configure all features up to and including those of version 3.0. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />To set up a virtual directory, first you need to set up a directory structure with a main directory (I called mine projects) that has two subdirectories: template and SOAP. The template directory will contain your xml(标准化越来越近了) template files and will be used for all template operations (for example, file-based SQL, XPath, updategrams, and so on). The SOAP directory will contain all files required for accessing sql server(WINDOWS平台上强大的数据库平台) via Web Services. If you want to experiment with mapping schemas (via the schema type) and/or direct database object access (via the dbobject type), then you may add directories for each of those as well. Follow these steps for testing your installation with a simple xml(标准化越来越近了) query.
(标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />If you are already doing .NET development, then you know that building Web Services is quite simple. Through Visual Studio?.NET and the runtime's use of attributes such as WebService and WebMethod, you can quickly produce reliable Web Services. Even more advanced functionality such as passing SOAP headers or hooking SOAP requests passed into a .NET Web Service (trace extensions) becomes less daunting with .NET. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />If you aren't using the .NET runtime in your environment yet (sense my bias?), a little more elbow grease may be required. You can use the SOAP Toolkit 2.0, but that requires more background in how SOAP is used to send and receive data from a Web Service. Overall, however, building a non-.NET client is very similar to working with a Web Service proxy in .NET. If you don't have .NET, or you don't want to build an entire data access/Web Services framework, SQLxml(标准化越来越近了) 3.0 is for you. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />SQLxml(标准化越来越近了) 3.0 provides a Web Service middle tier in the form of an ISAPI library (sqlis3.dll). All you need to do is configure SQLxml(标准化越来越近了) and provide a Web Services client. With SQLxml(标准化越来越近了) you can now send SOAP HTTP requests to a server running SQLxml(标准化越来越近了) 3.0 to execute a stored procedure, xml(标准化越来越近了) template, or UDF directly. The requested operation is executed at the data source and a SOAP response is returned to the client. The Web Services magic, at least on the server, is all taken care of by SQLxml(标准化越来越近了). Just configure the Web Service using the same MMC snap-in as I demonstrated in the previous section for templates. The only code required is on the client. This can be an ASP or ASP.NET application, a Microsoft Windows?application, a console application, or whatever. The client can be built using C#, a standard SOAP client using straight xml(标准化越来越近了), or even the SOAP Toolkit 2.0. In this article I will demonstrate client development by building a simple C# client (using the Visual Studio-generated Web Services proxy) and a Visual Basic?client (using the SOAP Toolkit). Setting Up a SQLxml(标准化越来越近了) Web Service (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />If you are following along with my sample, use these steps to configure the Web Service:
(标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The quickest way to get up and running with Web Services is to write your client using the .NET Framework. As you will see, it isn't the amount of code saved that makes .NET simpler to use. Most of you can get away without knowing the underpinnings of the SOAP protocol since the proxy generated from Visual Studio does all of the work. However, learning some of the basic elements of SOAP would be smart. For my simple example, I use C# to call the newly configured Web Service. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />I created a new client application using Visual Studio .NET. The client can be any type of application. For this example, I am using a C# application for Windows. SQLxml(标准化越来越近了) Web Services can be called like any other Web Service. Add a Web reference from the Add Web Reference dialog type in the same URL you used to test the WSDL file (http://localhost/northwind/SOAPprocedures?wsdl). In Figure 5 the GetAllCustomers template has been called as a Web Service and its xml(标准化越来越近了) results used in a DataSet grid. Figure 6 shows the sample client using ASP.NET. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The WSDL output should appear in the left pane of the Visual Studio IDE. From here, you can add the reference to your project. I added the procedures.wsdl reference, allowing me to declare a variable of this Web reference type. Once declared, I treat this type like any other class type in .NET by instantiating it. After the Web Service object is created, I can invoke its operations by calling any of its exposed methods. IntelliSense?should now display each of these Web methods in the editor. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The following C# code shows how to call a stored procedure wrapped as a Web Service. I've omitted a few details that I will explain shortly. You can see that calling a Web Service at this point is very similar to calling into any other object type: localhost.procedures oWSProcs = new localhost.procedures();int nReturnValue;晻?= oWSProcs.CustOrderHist("ALFKI", out nReturnValue);You'll notice that this call differs from standard calls to Web Services in the return values. When using SQLxml(标准化越来越近了) Web Services, the data returned from the Web method takes the form of an object array, which must then be cast into a workable type like xml(标准化越来越近了)Element or SqlMessage. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />xml(标准化越来越近了)Element objects include the result that is successfully returned by SQLxml(标准化越来越近了) after executing any operations (stored procedure, template, or UDF). In the WSDL file this is defined as having a Sqlxml(标准化越来越近了) complex type. Error messages returned from SQLxml(标准化越来越近了) are of type SqlMessage. If sql server(WINDOWS平台上强大的数据库平台) returns one or more errors, this SqlMessage complex type is returned as part of the object array and is also defined in the WSDL file. (More on this later.) (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The System.xml(标准化越来越近了).xml(标准化越来越近了)Element complex type maps directly into an xml(标准化越来越近了) node class type from the .NET class library. If you have worked with .NET and xml(标准化越来越近了) you should already be familiar with this stock type. SqlMessage is a custom type specific to SQLxml(标准化越来越近了) and contains any error messages generated during transport. To make sense of the returned object array from a SQLxml(标准化越来越近了) Web Service, I created the xml(标准化越来越近了)Element method. In Figure 7 you can see how the object array is handled. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />This method takes any returned object array and either returns an array of xml(标准化越来越近了)Element types or throws an exception, filling in the values from the SqlMessage type. To determine if the object array contains an error or xml(标准化越来越近了) instance data, the type is determined by using GetType and the value is cast appropriately. xml(标准化越来越近了)Elements are simply returned to the caller. Figure 8 shows the calling code in its entirety. (This is slightly different from this article's downloadable code for clarity.) (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />I have not yet mentioned the System.Data.DataSet type. Just because data is being transported via xml(标准化越来越近了), SOAP, and ultimately SQLxml(标准化越来越近了) doesn't mean you cannot use DataSets to your advantage. DataSets are terrific at providing the perfect data container, not to mention being handy for purposes such as displaying data in a grid. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />It's easy to return xml(标准化越来越近了) instance data from a stored procedure (callable from a SQLxml(标准化越来越近了) Web Service) and turn it into an xml(标准化越来越近了) schema-based DataSet, ready to be consumed as you please. To perform this conversion I created a method called GetDataSetFromxml(标准化越来越近了)Fragment which takes any xml(标准化越来越近了) fragment, infers an xml(标准化越来越近了) schema, and hydrates its data. The managed SQLxml(标准化越来越近了) classes can also be used in similar fashion. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />The following code shows how the System.xml(标准化越来越近了).xml(标准化越来越近了)Reader and the DataSet's Readxml(标准化越来越近了) work together to fill a DataSet: public static DataSet GetDataSetFromxml(标准化越来越近了)Fragment(xml(标准化越来越近了)Element oxml(标准化越来越近了)){DataSet ds = new DataSet(); xml(标准化越来越近了)TextReader oReader = new xml(标准化越来越近了)TextReader(oxml(标准化越来越近了).Outerxml(标准化越来越近了), xml(标准化越来越近了)NodeType.Element, new xml(标准化越来越近了)ParserContext(null, null, null, xml(标准化越来越近了)Space.None)); // now lets create a schema off of the instance data ds.Readxml(标准化越来越近了)(oReader, xml(标准化越来越近了)ReadMode.InferSchema); return ds;}(标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Don't forget, value types such as integer and float cannot be passed or returned as a null value when using the proxy classes that are generated by Visual Studio .NET. To do so, you must create your own Web Service proxy class (which is not recommended). Reference types and string types can be null. Calling Templates and UDFs as Web Services (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Along with stored procedures, SQLxml(标准化越来越近了) also allows Web Services to call xml(标准化越来越近了) templates and UDFs. Configuring these types is not very different from working with stored procedures. The configuration process establishes the necessary mapping in a WSDL file as before. Once configured, the mapping is used to execute the corresponding template or UDF just like you do with stored procedures. If you want to configure a template to use with my sample, complete the following steps:
(标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Invoking a UDF is no different. You can build the UDF shown in Figure 9 by following the same steps just outlined and selecting SP as the Edit/New mapping type as you did when configuring a callable stored procedure. All UDFs and stored procedures should appear in the browse dialog. Make sure you update your Web reference from Visual Studio .NET. (IntelliSense will tell you when it is there, or you can look at the generated WSDL.) Using the SOAP Toolkit 2.0 (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Many of you may not yet have the option of using .NET technology in your development environment. If that's the case, you can invoke any SQLxml(标准化越来越近了) feature using plain old Visual Basic?6.0. The only additional component required prior to running the following sample code is the SOAP Toolkit 2.0. I invoke the exact same operations I created here already except I will do it from Visual Basic 6.0. Familiarity with the MSxml(标准化越来越近了) Document Object Model (DOM) would be helpful, but it's not required. The only two interfaces that are required are the Ixml(标准化越来越近了)DOMNodeList and Ixml(标准化越来越近了)DOMNode interfaces from MSxml(标准化越来越近了) 4.0. (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />Figure 10 looks amazingly similar to the C# sample. The major difference here is that I am doing this from a Visual Basic 6.0-based client and I am using the soapclient component from the Soap Toolkit 2.0 for the proxy. Soapclient is used exactly like the generated proxy from Visual Studio .NET. Instead of binding the return values from an object array to a data type, you will always be using an Ixml(标准化越来越近了)DomNodeList from MSxml(标准化越来越近了) 4.0 to iterate through each returned Ixml(标准化越来越近了)DOMNode. Here you are simply working with the MSxml(标准化越来越近了) Node interfaces. The output from running this code is not quite as neat as you saw in the .NET example. It could be much improved with a little XSL. I'll leave the rest up to you. Conclusion (标准化越来越近了) Support for sql server(WINDOWS平台上强大的数据库平台) 2000(图二)" width="10" height="4" />In this article I introduced SQLxml(标准化越来越近了) 3.0 and its most powerful application: Web Services using SOAP. For environments not ready for .NET, or those of you without the inclination to build a custom middle tier, SQLxml(标准化越来越近了) 3.0 provides a simple yet effective way to access sql server(WINDOWS平台上强大的数据库平台) over the wire. Hierarchical data in the form of xml(标准化越来越近了) has become the data format of choice among developers. xml(标准化越来越近了) and SOAP will give you an advantage in the loosely coupled world of Web Services. To download the latest Web release (SQLxml(标准化越来越近了) Version 3.0) or to find more information on the new features offered in the xml(标准化越来越近了) for sql server(WINDOWS平台上强大的数据库平台) Web Releases, see http://msdn.microsoft.com/xml(标准化越来越近了). |
- ››DataGrid中CheckBox绑定bool属性来进行选中判断
- ››data/data/目录下的私有数据
- ››Data Web Services:访问IBM数据库服务器的新方式...
- ››datagridview checkbox控件
- ››DataGridView表尾统计控件3
- ››DataReader与DataSet的区别
- ››data/tmp等文件夹777检测属性不通过的解决方法
- ››DataSet导出到Excel比较完整的解决方案(一)--客...
- ››DataSet导出到Excel比较完整的解决方案(二)--服...
- ››DataSet导出到Excel比较完整的解决方案(二)--服...
- ››DataCore发布中小企业SAN软件解决方案
- ››Data Studio Administrator V1.2 中的数据保留和数...
更多精彩
赞助商链接