2003-01-19 Daniel Morgan <danmorg@sc.rr.com>
[mono.git] / doc / mysql
1 * MySQL Data Provider
2
3 <ul>
4         <li>MySQL Data Provider for 
5         <a href="http://www.mysql.com/">MySQL</a> databases.  It is written in C# and uses
6         the MySQL C Client Library</li>
7
8         <li>Exists in namespace Mono.Data.MySql and assembly Mono.Data.MySql</li>
9         
10         <li>Works on Windows and Linux via the MySQL client shared library
11             (libmySQL.dll on Windows and libmysqlclient.so on Linux).</li>
12             
13     <li>Started by Daniel Morgan using 
14     <a href="http://www.cybercom.net/~zbrad/DotNet/MySql/">C# Bindings to MySQL</a> from <a href="mailto:zbrad@cybercom.net">Brad Merill</a>
15     
16     <li><a href="http://www.mysql.com/articles/dotnet/">Exploring MySQL in the Microsoft .NET Environment</a> is an article
17     by Mr. Venu who is a MySQL AB developer.</li>
18     
19     <li>There is a good alternative to the provider in Mono:
20                 <ul>
21                         <li><a href="http://sourceforge.net/projects/mysqlnet/">MySQLNet</a> from ByteFX 
22                         is a MySQL Managed provider written in 100% C#,
23                         does not require a client library, and works on Microsoft .NET and Mono.  You 
24                         need at least Mono 0.18 and MySQLNet 0.65 for it to work on Mono.</li>
25
26                 </ul>
27                 </li>
28         <li>Testing for Mono's Mono.Data.MySql and ByteFX's ByteFX.Data.MySQLClient is below...</li>
29 </ul>
30     
31 ** Current Status
32
33 <ul>
34         <li>can connect
35         
36         <li>can execute non-queries via ExecuteNonQuery()
37         
38         <li>can execute aggregates via ExecuteScalar() and retrieve the
39         single row/single column result
40         
41         <li>can execute queries and retrieve results using a data reader.
42         
43         <li>a schema DataTable has been partially 
44         implemented which is returned from GetSchemaTable() in MySqlDataReader.
45         
46         <li>a DataTable in a DataSet can be filled via a MySqlDataAdapter 
47                 
48         <li>The shared client libraries 
49         between windows version and linux are different: windows has libmySQL.dll 
50         while linux has libmysqlclient.so.  This is handled by the 
51         file etc/mono/config which is mapped by the mono runtime in knowing
52         which native shared library to load.  In cvs, this file is mono/config.in and
53         can be modified with a text editor.  
54 </ul>
55
56 ** Action plan
57
58 The current plan to work on the MySQL data provider:
59         
60         <ul>
61                 <li>Parameters support via MySqlParameter and MySqlParameterCollection
62                 
63                 <li>Support LOBs (Large Object)
64                 
65                 <li>Change the MySQL provider to not be dependent on a client library and
66                     be written in 100% C#
67                     
68                 <li>Connection pooling
69                 
70                 <li>Get the provider to work like other data providers
71         </ul>
72
73 ** Testing for Mono's MySQL provider (Mono.Data.MySql)
74
75 <ul>
76         <li>Have access to a MySQL database or download it from
77                 <ul>
78                         <li><a href="http://www.mysql.com/downloads/index.html">MySQL AB</a></li>
79                 </ul>
80         </li>
81         
82         <li>Take a look at MySqlTest.cs in mcs/class/Mono.Data.MySql/Test</li>
83         
84         <li>Has a ConnectionString format: 
85 <pre>
86  "Server=hostname;" +
87  "Database=database;" +
88  "User ID=username;" +
89  "Password=password"
90          (or)
91  "host=hostname;" +
92  "dbname=database;" +
93  "user=username;" +
94  "passwd=password"
95 </pre>
96
97         <li>C# Example:
98 <pre>
99  using System;
100  using System.Data;
101  using Mono.Data.MySql;
102  
103  public class Test 
104  {
105     public static void Main(string[] args)
106     {
107        string connectionString = 
108           "Server=localhost;" +
109           "Database=test;" +
110           "User ID=mysql;" +
111           "Password=;";
112        IDbConnection dbcon;
113        dbcon = new MySqlConnection(connectionString);
114        IDbCommand dbcmd = dbcon.CreateCommand();
115        // requires a table to be created named employee
116        // with columns firstname and lastname
117        // such as,
118        //        CREATE TABLE employee (
119        //           firstname varchar(32),
120        //           lastname varchar(32));
121        string sql = 
122             "SELECT firstname, lastname " + 
123             "FROM employee";
124        dbcmd.ConnectionString = sql;
125        IDataReader reader = dbcmd.ExecuteReader();
126        while(reader.Read()) {
127             string FirstName = reader["firstname"];
128             string LastName = reader["lastname"];
129             Console.WriteLine("Name: " + 
130                  FirstName + " " + LastName);
131        }
132        // clean up
133        reader.Close();
134        reader = null;
135        dbcmd.Dispose();
136        dbcmd = null;
137        dbcon.Close();
138        dbcon = null;
139     }
140  }
141 </pre>
142         </li>
143         <li>Building C# Example:
144         <ul>
145                 <li>Save the example to a file, such as, TestExample.cs</li>
146                 <li>Build on Linux:
147 <pre>
148         mcs TestExample.cs \
149             -r System.Data.dll \
150             -r Mono.Data.MySql.dll
151 </pre>
152                 </li>
153                 <li>Build on Windows via Cygwin:
154 <pre>
155         mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe \
156              TestExample.cs \
157              -lib:C:/cygwin/home/MyHome/mono/install/lib \
158              -r System.Data.dll \
159              -r Mono.Data.MySql.dll
160 </pre>
161                 </li>
162         </ul>
163         </li>
164         <li>Running the Example:
165 <pre>
166 mono TestExample.exe
167 </pre>
168         </li>
169
170 </ul>
171
172 ** Testing for MySQLNet provider (ByteFX.Data.MySQLClient)
173
174 <ul>
175         <li>Have access to a MySQL database or download it from
176                 <ul>
177                         <li><a href="http://www.mysql.com/downloads/index.html">MySQL AB</a></li>
178                 </ul>
179         </li>
180         
181         <li>MySQLNet can be gotten from <a href="http://sourceforge.net/projects/mysqlnet/">here</a> and the 
182         binary assembly ByteFX.Data.dll needs to be     installed 
183         in the same place as the mono class libraries.</li>
184         
185         <li>MySQLNet requires <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/default.asp">SharpZipLib</a> which is 
186         a Zip Library written in 100% C#.  This is used for compression/decompression of data
187         sent/received over the network.  The SharpZipLib binary assembly SharpZipLib.dll should 
188         be installed in the same place as the mono class libraries.</li>
189         
190         <li>Has a ConnectionString format: 
191 <pre>
192 "Server=hostname;" +
193 "Database=database;" +
194 "User ID=username;" +
195 "Password=password"
196 </pre>
197         </li>
198         <li>C# Example:
199 <pre>
200  using System;
201  using System.Data;
202  using ByteFX.Data.MySQLClient;
203  
204  public class Test 
205  {
206     public static void Main(string[] args)
207     {
208        string connectionString = 
209           "Server=localhost;" +
210           "Database=test;" +
211           "User ID=mysql;" +
212           "Password=;";
213        IDbConnection dbcon;
214        dbcon = new MySQLConnection(connectionString);
215        IDbCommand dbcmd = dbcon.CreateCommand();
216        // requires a table to be created named employee
217        // with columns firstname and lastname
218        // such as,
219        //        CREATE TABLE employee (
220        //           firstname varchar(32),
221        //           lastname varchar(32));
222        string sql = 
223            "SELECT firstname, lastname " +
224            "FROM employee";
225        dbcmd.ConnectionString = sql;
226        IDataReader reader = dbcmd.ExecuteReader();
227        while(reader.Read()) {
228             string FirstName = reader["firstname"];
229             string LastName = reader["lastname"];
230             Console.WriteLine("Name: " + 
231                   FirstName + " " + LastName);
232        }
233        // clean up
234        reader.Close();
235        reader = null;
236        dbcmd.Dispose();
237        dbcmd = null;
238        dbcon.Close();
239        dbcon = null;
240     }
241  }
242 </pre>
243         </li>
244         <li>Building C# Example:
245         <ul>
246                 <li>Save the example to a file, such as, TestExample.cs</li>
247                 <li>Build on Linux:
248 <pre>
249         mcs TestExample.cs -r System.Data.dll \
250             -r ByteFX.Data.dll
251 </pre>
252                 </li>
253                 <li>Build on Windows via Cygwin:
254 <pre>
255         mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe \
256              TestExample.cs \
257              -lib:C:/cygwin/home/MyHome/mono/install/lib \
258              -r System.Data.dll -r ByteFX.Data.dll
259 </pre>
260                 </li>
261         </ul>
262         </li>
263         <li>Running the Example:
264 <pre>
265 mono TestExample.exe
266 </pre>
267         </li>
268
269 </ul>
270