|
TN0016: How to access SAP DB from a Microsoft .NET environment This technical note describes how to access a SAP DB database from a .NET program. Installed Software I started with a clean installation of Windows 2000 Workstation. Then I used the built-in „Windows Update“ to install all available service packs and patches. As for the .NET environment, I installed the 1.0 version (service pack 2) as well as the 1.1 version of the .NET framework. On top of that I installed "ODBC .NET Data Provider" which according to the online documentation is part of the 1.1 framework, but needs to be installed separately for the 1.0 framework. In order to make this ODBC bridge happy, I also had to install MDAC 2.6 service pack 2. For the database, I installed SAP DB 7.4.03.27 which I downloaded straight from the SAP DB web site. I installed the "all" package which includes the server, the clients, and some command line utilities. I ran the "create_demo_db" script and thusly created the TST database. I also installed DBM GUI and the SQL Studio. I used the system configuration tools to create an ODBC data source called "TST" which connects via the SAP DB ODBC driver to the TST database on server "localhost". .NET Framework 1.0 As a .NET client, I used the application "ADO.NET Ad Hoc Query Tool" which comes on the acompanying CD to David Sceppa's book "Microsoft ADO.NET. Core Reference“ from Microsoft Press. When I first started this tool, I had to select " .NET Framework 1.1 With the release of .NET 1.1, the ODBC data provider got moved into the System.Data.Odbc Namespace and became part of the core framework. The following C# application shows how to access the SAP DB demo database: // hello.cs (.NET framework 1.1) using System; using System.Data; using System.Data.Odbc; // FW 1.0: Microsoft.Data.Odbc; class hello { static void Main(string[] args) { string strConn = "DSN=TST;UID=TEST;PWD=TEST"; IDbConnection conn = new OdbcConnection(strConn); conn.Open(); IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT owner,tablename,type,tabletype FROM tables"; IDataReader reader = cmd.ExecuteReader(); try { while (reader.Read()) { int n=reader.FieldCount; for(int i=0; i<n; i++) Console.Write(reader.GetString(i)+" "); Console.WriteLine(""); } } finally { reader.Close(); conn.Close(); } } } This code gets compiled from the command line with the following commands: C:\home\ck>set path=c:\programme\microsoft.net\sdk\v1.1\bin\;c:\winnt\microsoft. net\framework\v1.1.4322\;c:\programme\microsoft visual studio .net 2003\vc7\bin\ ;c:\programme\microsoft visual studio .net 2003\common7\ide\;c:\winnt\system32\; c:\winnt;c:\winnt\system32\whem C:\home\ck>set LIB=c:\programme\microsoft visual studio .net 2003\vc7\lib\;c:\pr ogramme\microsoft.net\sdk\v1.1\lib\; C:\home\ck>set INCLUDE=c:\programme\microsoft visual studio .net 2003\vc7\includ e\;c:\programme\microsoft.net\sdk\v1.1\include\; C:\home\ck>csc hello.cs Microsoft (R) Visual C# .NET-Compilerversion 7.10.3052.4 für Microsoft (R) .NET Framework, Version 1.1.4322 Copyright (C) Microsoft Corporation 2001-2002. Alle Rechte vorbehalten. C:\home\ck> Document History First Version: August 31, 2003 Questions? If you have any questions, please send e-mail to Carsten Kuckuk at . |