85ef0f1ed452b0d583afcf387328db2728ab131f
[mono.git] / doc / ado-net
1 * ADO.NET
2
3         The coordinator for the ADO.NET implementation is <a
4         href="mailto:rodrigo@ximian.com">Rodrigo Moya</a>, with
5         the collaboration of <a href="mailto:danmorg@sc.rr.com">Daniel
6         Morgan</a>.
7
8 * Action plan
9
10         The current plan to implement ADO.NET is as follows:
11
12         <b>Step 1:</b> SqlClient:
13
14         <ul>
15                 * Implementation of System.Data.SqlClient based on
16                   the <a href="http://www.postgresql.org/idocs/">PostgreSQL C API</a>
17
18                 * Once the <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatasqlclient.asp?frame=true">System.Data.SqlClient</a>
19                   code is functional and is usable by other people, we willl move it to
20                   System.Data.PostgreSQL, and will convert the existing
21                   System.Data.SqlClient to be just a wrapper around
22                   System.Data.PostgreSQL.  
23
24         </ul>
25
26         <b>Step 2:</b> <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataOleDb.asp?frame=true">System.Data.OleDb</a> Provider:
27                 
28         <ul>
29                 * On Unix systems: System.Data.OleDb will use the
30                 <a href="http://www.gnome-db.org/">LibGDA</a>
31                  engine. 
32
33                   LibGDA is a data access engine like ADO/OLE-DB, but for Unix.  It is
34                   used by Gnome-Db and libgnomedb.  There is work under way to get libgda
35                   working under Windows by the Gnome-Db developers.
36
37                 * On Windows systems: System.Data.OleDb will use OLE-DB as
38                   its engine.  It may have the option of using libgda too.
39         </ul>
40
41         <b>Step 3:</b> <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatasqlclient.asp?frame=true">System.Data.SqlClient</a> Provider:
42
43         <ul>
44                 * <p>System.Data.SqlClient will then become a managed
45                   provider for Microsoft SQL Server, both on Windows and
46                   Linux, to be compatible with applications written
47                   for the Microsoft .NET Development Framework.
48                   
49                   <p>Once Step 1 has been completed and the PostgreSQL
50                   provider support has been moved to its own place at 
51                   System.Data.PostgreSQL, we will use 
52                   <a href="http://www.freetds.org/">FreeTDS</a> as the basis
53                   for providing access to Microsoft SQL Server databases.
54                   
55                   FreeTDS is a C API for Unix and Windows that implements 
56                   the TDS (Tabular Data Stream) protocol used in accessing 
57                   Microsoft SQL Server and Sybase databases.
58         </ul>
59
60         <b>Step 4:</b> <a href="http://msdn.microsoft.com/downloads/sample.asp?url=/MSDN-FILES/027/001/668/msdncompositedoc.xml&frame=true">System.Data.Odbc</a> Provider:
61         
62         <ul>
63                 * We will create a .NET Managaed Provider for ODBC
64                   in System.Data.Odbc for those using ODBC.
65                   On Unix and Windows, <a href="http://www.unixodbc.org/">unixODBC</a> will be used.
66                   
67                   unixODBC works on Unix and Windows.  Providers exist for
68                   Oracle, Microsoft SQL Server and Sybase via FreeTDS, 
69                   MySQL, PostgreSQL, IBM DB2, Interbase, and others.
70                   
71         </ul>
72
73         <b>Step 5:</b> Other System.Data providers:
74
75         <ul>
76                 * The idea in Microsoft .NET System.Data seems to be to have
77                   a managed provider for each supported DBMS. So, apart
78                   from System.Data.OleDb (generic) and System.Data.SqlClient,
79                   we'll need to have System.Data.MySQL,
80                   System.Data.Oracle, and System.Data.PostgreSQL. Others,
81                   of course, are welcomed.
82         </ul>
83         
84 * Current Status
85
86         <p>We are still working on Step 1, but we are planning the other steps.
87         
88         <p>We are able to do simple CREATE TABLE, DROP TABLE, UPDATE, INSERT, and
89         DELETE SQL commands using the ExecuteNonQuery method in SqlCommand.  
90         
91         <p>We are also able to do simple aggregate functions, 
92         ie, count(), sum(), min(), and max() 
93         in a simple SELECT SQL query using the ExecuteScalar() now.  
94         
95         <p>We are also able to retrieve data with a simple SELECT SQL query 
96         using ExecuteReader() which returns a SqlDataReader.  We are able to
97         use GetSchemaTable() to get the meta data about the table columns.  
98         We are able     to Read() to get each row from the result set.  We are able to get 
99         String data (char, bpchar (character), text, varchar), Int16 (int2 or smallint),
100         Int32 (int4 or integer), Int64 (int8 or bigint), DateTime (time, date, timestamp),
101         Boolean (boolean), Single (float), and Double (double).
102         Other More data types will follow later on.
103                 
104         <p>Parameters and stored procedures have not been tested and most likely
105         do not work yet.
106         
107         <p>A lot of functionality in System.Data is missing, but the 
108         infrastructure is starting to come together.
109         
110         <p>Need help on the DataSet/DataAdaptor/DataTable/DataRelation/XML 
111         functionality so we     can integrate with 
112         the ASP.NET controls and Windows.Forms.  
113         
114         <P>Need to add XML support in System.Data.
115         
116         <p>To compile that test program, you need:
117
118         <b>On Linux</b>
119
120         <ul>
121                 * update your mono and mcs sources.
122
123                 * get the files from mcs/class/lib/*.dll and mcs/mcs/mcs* 
124                   built on windows, and put them on your
125                   linux machine at, for example,  ~/mono/install/bin and 
126                   ~/mono/install/lib
127
128                 * compile the test program along with the System.Data,
129                   System.Data.Common, System.Data.SqlClient, and
130                   System.Data.SqlTypes.  The test program to use is PostgreTest.cs
131         </ul>
132
133 * Testing
134
135         <p>In order to test System.Data.SqlClient, you will need to have
136         access to a remote PostgreSQL DBMS, or you will have to install 
137         one locally.  PostgreSQL is the DBMS used for the initial 
138         implementation of System.Data.SqlClient.
139                 
140         <p>Why?  Because it is open source, has a client 
141         library that is easy to use, PostgreSQL on Windows is easy to install on
142         Unix and Windows (using the Cygwin install program), not difficult to setup after
143         installation, and it runs under: Linux, 
144         Windows (via cygwin and ipc-daemon), Unix, and
145         others.  This allowed us to create the
146         System.Data functionality in Mono much quicker.
147                 
148         <p>If you plan on using a remote PostgreSQL DBMS Server,
149         than you will need to have the PostgreSQL client software on your
150         local computer that includes libpq.so (pq.dll on Windows).
151                 
152         <p>Installation instructions for PostgreSQL DBMS:
153
154         <b>On Linux</b>
155
156         <ul>
157                 * TODO
158
159                 * It easier to install PostgreSQL on Linux than Windows.  
160         </ul>
161
162         <b>On Windows</b>
163
164         <ul>
165                 * Use the <a href="http://www.cygwin.com/">Cygwin</a> installer to install the PostgreSQL DBMS.  It is
166                   found in the database category.
167                   
168                 * <p>Read the file postgres-x.x.README at /usr/doc/Cygwin and read 
169                   the requirements to install PostgreSQL.  Those requirements
170                   are included with cygwin except cygipc.  A default installtion
171                   of cygwin does not install everything you will need, so on the 
172                   safe side, just include everything when installing cygwin.
173                 
174                 * <p>The -x.x in postgres-x.x is the version of your PostgreSQL DBMS.
175                 
176                 * <p>Once Cygwin has installed the PostgreSQL DBMS on your computer,
177                   read the file FAQ_MSWIN which is available 
178                   in /usr/doc/postgres-x.x 
179                                   
180                 * <p>Important notes from this file are:
181                   
182                   <ul>
183                                 <p><b>2.</b> - Install the latest <a href="http://www.neuro.gatech.edu/users/cwilson/cygutils/OBSOLETE/V1.1/cygipc/index.html">CygIPC</a> package.
184                                                   
185                                 <p>The cygipc package contains the ipc-daemon you will need 
186                                 to run before you can
187                                 run the PostgreSQL DBMS Server daemon (postmaster) or run
188                                 initdb which initializes the PostgreSQL database.
189                           
190                                 <p><b>3.</b>  The Cygwin bin directory has to be placed in 
191                                 the path before the Windows program directories, 
192                                 for example, C:\cygwin\bin 
193                           
194                                 My own note, in the Windows control panel, I set
195                                 the environment variables PATH to my cygwin /usr/local/bin,
196                                 /usr/bin, and /bin.  I also set my LD_LIBRARY_PATH to 
197                                 /usr/local/lib and /usr/lib.  For example:
198                           
199                                 <p>
200 <pre>
201 PATH=c:\cygwin\usr\local\bin;c:\cygwin\usr\bin;c:\cygwin\bin;
202 LD_LIBRARY_PATH=c:\cygwin\usr\local\lib;c:\cygwin\usr\lib;
203 </pre>
204                                                           
205                                 <p><b>4.</b> Start the ipc-daemon that came with the cygipc package.  There
206                                 are two ways to do this: run it from the command line as:
207                           
208                                 <p>
209 <pre>
210 ipc-daemon &
211 </pre>                    
212                                 <p>or you can set it up as a Windows service.  See the 
213                                 file cygrunsrv.README at /usr/doc/Cygwin on how to do this
214                                 for ipc-daemon and postmaster.  Note the
215                                 troubleshooting section at the end of 
216                                 the cygrunsrv.README file.
217                           
218                                 <p>To install ipc-daemon as a service, 
219                                 you just have to run
220                           
221                                 <p>
222 <pre>
223 ipc-daemon --install-as-service' (--remove-as-service) 
224 </pre>
225                           
226                                 <p>and then run
227                           
228 <pre>
229 net start ipc-daemon
230 </pre>
231                         </ul>
232                           
233                         <p>Read the installation.html file 
234                         at /usr/doc/postgresql-x.x/html/installation.html
235                 
236                         <p>You will see in this file that you will need to 
237                         run the following commands:
238                   
239                         <p>
240 <pre>
241 mkdir /usr/local/pgsql/data\r
242 initdb -D /usr/local/pgsql/data\r
243 postmaster -D /usr/local/pgsql/data >logfile 2>&1 &\r
244 createdb test\r
245 psql test               
246 </pre>
247                   
248                         <p>When you need to connect to the database, 
249                         you will need ipc-daemon and postmaster running.  Start ipc-daemon
250                         before any of the command above.  
251                   
252                         <p>psql is a command-line PostgreSQL client tool to 
253                         enter and run SQL commands and queries.
254                   
255                         <p>If there is no database user named postgres, create a user named
256                         postgres with the following SQL command in the client tool psql:
257                   
258                         <p>
259 <pre>
260 psql test
261 create user postgres with password 'fun2db';
262 </pre>
263                         <p>The only reason I say this is so you can easily use the System.Data tests
264                         without having to change the database, userid, etc.
265         </ul>
266         
267         <p>In the path mcs/class/System.Data/Test
268         there is a PostgreSQL test program named
269         PostgreTest.cs.  Thanks goes to Gonzalo for creating the original
270         PostgreSQL test.
271         
272         <p>To use it to test System.Data, you
273         modify the file to your PostgreSQL database
274         connection requirements:
275
276         <p>
277 <pre>   
278 dbname is the database, ie, test
279 host is the hostname of the PostgreSQL DBMS Server to connect to
280 user is the username, ie, someuser
281 password is the password, ie, mypass1234
282 </pre>
283         
284         <p>The connection string is in OLE-DB connection string format.  Internally,
285         SqlConnection converts this to the PostgreSQL connection string format.
286         
287         <p>
288 <pre>
289     OLE-DB: "host=localhost;dbname=test;user=joe;password=smoe"
290 PostgreSQL: "host=localhost dbname=test user=joe password=smoe"
291 </pre>
292
293         <p>
294         Note that OLE-DB includes the semicolons while PostgreSQL's connection
295         string does not.
296
297         <p>
298     To compile the PostgresTest.cs program, do:
299     
300     <p>
301 <pre>
302 mcs PostgresTest.cs -r System.Data
303 </pre>
304     
305     <p>
306     To run using mint, do:
307     
308     <p>
309 <pre>
310 mint PostgresTest.exe
311 </pre>
312     
313     <p>
314     To run using mono, do:
315 <pre>
316 mono PostgresTest.exe
317 </pre>
318     
319     <p>You should get something like:
320
321 <p>
322 <pre>   
323  Administrator@DANPC ~/mono/mcs/class/System.Data/Test\r
324  $ mcs PostgresTest.cs -r System.Data\r
325 \r
326  Administrator@DANPC ~/mono/mcs/class/System.Data/Test\r
327  $ mint PostgresTest.exe\r
328          Postgres provider specific tests...\r
329 \r
330                  Drop table:\r
331  Error (don't worry about this one)\r
332                  Create table with all supported types:\r
333  OK\r
334                  Insert values for all known types:\r
335  OK\r
336                  Select values from the database:\r
337                  Get Schema.\r
338  dt.Columns.Count: 6\r
339  * Column Name: int2_value\r
340           MaxLength: 2\r
341           Type: System.Int16\r
342  * Column Name: int4_value\r
343           MaxLength: 4\r
344           Type: System.Int32\r
345  * Column Name: bigint_value\r
346           MaxLength: 8\r
347           Type: System.Int64\r
348  * Column Name: char_value\r
349           MaxLength: -1\r
350           Type: System.String\r
351  * Column Name: varchar_value\r
352           MaxLength: -1\r
353           Type: System.String\r
354  * Column Name: text_value\r
355           MaxLength: -1\r
356           Type: System.String\r
357  Row 0:\r
358      Col 0: int2_value - -22\r
359      Col 1: int4_value - 1048000\r
360      Col 2: bigint_value - 123456789012345\r
361      Col 3: char_value - This is a char\r
362      Col 4: varchar_value - This is a varchar\r
363      Col 5: text_value - This is a text\r
364  Rows: 1\r
365  Clean up...\r
366                  Drop table...\r
367  OK\r
368 </pre>\r
369 \r