Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using MySQL from C# and Mono.NET

, 3 Jul 2005
An article on connecting to and using MySQL from Mono.
mysqlconsole_mono.zip
MySQLConsole
ByteFX.Data.dll
ICSharpCode.SharpZipLib.dll
Mono.Posix.dll
mysqlconsole_vs.zip
App.ico
bin
ByteFX.Data.dll
ICSharpCode.SharpZipLib.dll
Mono.Posix.dll
MySQLConsole.csproj.user
using System;
using System.Configuration;
using ByteFX.Data;
using System.IO;
using System.Reflection;

public class MySQLConsole
{
	private static string connectionString = string.Empty;
	private static string databaseName = string.Empty;
	private static ByteFX.Data.MySqlClient.MySqlConnection mysqlConn;

	public static void Main(string[] args)
	{
		try
		{
			connectionString = ConfigurationSettings.AppSettings["mysqlConnectionString"].ToString();
			Console.WriteLine("Please enter a new database name:");
			databaseName = Console.ReadLine();

			if(databaseName.Length > 0)
			{
				mysqlConn = new ByteFX.Data.MySqlClient.MySqlConnection();
				mysqlConn.ConnectionString = connectionString;
				mysqlConn.Open();

				//Create the sql script...
				string createdbSql = string.Format(
					"CREATE DATABASE {0}", databaseName);

				using(ByteFX.Data.MySqlClient.MySqlCommand cmd
					= new ByteFX.Data.MySqlClient.MySqlCommand(createdbSql, mysqlConn))
				{
					cmd.ExecuteNonQuery();

					Console.WriteLine(string.Format(
						"Database {0} successfully created", databaseName));
				}

			}

		}
		catch(Exception ex)
		{
			Console.WriteLine(ex.Message.ToString());
		}
		finally
		{
			if(mysqlConn != null)
			{
				mysqlConn.Close();
				mysqlConn.Dispose();
			}
		}

	}
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Custec
Software Developer (Senior)
United Kingdom United Kingdom
Sixteen years experience in software programming and web development, using C, Visual Basic and C Sharp on Windows and Linux.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 4 Jul 2005
Article Copyright 2005 by Custec
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid