Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Free Your C# Apps from .NET Platforms : Page 3

Visual MainWin offers unprecedented platform flexibility, allowing you to develop applications in C# and deploy and run them on J2EE. Learn how to take advantage of this freedom by building a C# Web service that you can run on platforms besides .NET and IIS.


advertisement
Accessing Data with ADO.NET
Your Web service can also access data using ADO.NET or JDBC directly. In the case of the former, you have full access to the features of ADO.NET to allow ease of development and debugging. The problem is that with the current release of Visual MainWin only SQL Server and Oracle are supported.

To use ADO.NET with SQL Server, use the Server explorer to find the table or other view that contains data that you want to access. Drag this view onto the Web service design area, and Visual Studio.NET will create a sqlConnection object and a sqlDataAdapter object. The former contains the specifics for connecting to the desired database and the latter contains the specifics for the records in which you are interested. For example, locate the Northwind database and drag the 'Customers' table onto the workspace.

Your interaction with the data is through an object type called a DataSet. To get one of these, select the sqlDataAdapter object and, in the top menu, select Data—>Generate Dataset. Call the dataset dsCustomers and make sure that you check 'Add this component to the designer.'



Once you have this dataset successfully created, you can pull data from the database and return it to the user in XML using this code:

[WebMethod] public XmlDocument getAuthors(){ sqlDataAdapter1.Fill(dsCustomers1.Customers); System.IO.StringWriter sw = new System.IO.StringWriter(); dsCustomers1.Namespace=""; dsCustomers1.WriteXml(sw); System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.LoadXml(sw.ToString()); return xDoc;}

This code compiles and runs on the J2EE application server, cross compiling the ADO.NET data access into the appropriate JDBC code.

What About Other Databases?
One of the problems with a cross compiler is that the compiler itself has to understand every technology that it cross-compiles. This can cause it to lose the 'pluggability' you find in aspects of the technology such as JDBC or ADO.NET. For instance, to support different databases, a cross compiler is necessary for each instance of a JDBC driver. This allows you to cut code using the ADO.NET or ODBC version of the driver. The compiler then cross-compiles this to the equivalent JDBC-generated bytecode. As you can see, the number of databases that a cross compiler can support is be limited so the vendors have chosen to support SQL Server and Oracle natively within Visual Studio.NET. This is why you can only use the ADO.NET shortcuts if you are using these databases.

All this need not concern you, as there is a very neat feature if you want to access any other database (and by extension any other java-based functionality)—you simply import the JAR file containing the JDBC drivers for your target database, and reference the drivers in C#.

For example, say you want to access a MySQL database on your application server. Add a java reference to the JDBC driver's JAR file in your project by selecting the 'references' node in the solution explorer and then selecting 'Add Java Reference.' Browse to the JAR containing the JDBC drivers and add them. Visual Studio will take a few minutes to process the JAR.

Once you have done this with the drivers for MySQL, you can then create code to connect to a MySQL database. The code below is java, developed using all the features of the Microsoft IDE!

java.sql.Statement stmt = null; java.sql.ResultSet rs = null; String query="Select * from useraccounts"; java.lang.Class.forName("com.mysql.jdbc.Driver").newInstance(); string strSource = jdbc:mysql://localhost/mydb?user=me&password=this"; java.sql.Connection conn = DriverManager.getConnection(strSource); stmt = conn.createStatement(); rs = stmt.executeQuery(query); java.sql.ResultSetMetaData rsmd = rs.getMetaData();

You can add this code to a WebMethod and compile and run it inline with your C# code.

Freedom for Middleware Developers
Microsoft has appealed to the low- to mid-level developers by giving them the best tools, thus enabling them to do their jobs without worrying about underlying complexity. Opinion is sharply divided as to the right technology for the more advanced tiers of an application, notably for the data access and business logic tiers. Applications like Visual MainWin render this division irrelevant for middleware and user interface developers. Using the simplicity of Web services development provided by Visual Studio.NET, you can finally create applications and Web services that run on platforms other than IIS and .NET.



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date