0 Replies - 2833 Views - Last Post: 27 October 2008 - 06:14 PM Rate Topic: -----

#1 Svitol  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 26-October 08

Simple Database Class

Posted 27 October 2008 - 06:14 PM

Hello,

I'm very new at .Net programming, actually i started in July but due to work need had to proceed very fast and found a way for making whatever i needed, one of the first thing i found out was that understanding how to access and use mysql database or just xml can be a real pain for a beginner, and people around keep saying "start from simplier thing", well if you need that for working on ur project better to find something written in simple way that works...

The Following code will need the MySql.Data.dll that you can download from MySql website, the following code is very simple to use with methods that return simple result, i kindly ask to experienced programmers to say whatever you think about it, i know is not so "elegant" but it works :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace CS_Template
{
	class MyDbEngine
	{
		public static string XmlRead(string xmlfilename, string table, string item)
		{
			//Usage: MyDbEngine.XmlRead("filename.xml or subdir/filename.xml", "Table Name", "Item Name");

			DataSet ds = new DataSet();
			ds.ReadXml(Application.StartupPath + "/" + xmlfilename);
			string value = ds.Tables[table].Rows[0][item].ToString();
			return value;
		}

		public static void XmlWrite(string xmlfilename, string table, string item, string value)
		{
			//Usage: MyDbEngine.XmlWrite("filename.xml or subdir/filename.xml", "Table Name", "Item Name", "new value for the item");

			DataSet ds = new DataSet();
			ds.ReadXml(Application.StartupPath + "/" + xmlfilename);
			ds.Tables[table].Rows[0][item] = value;
			ds.WriteXml(Application.StartupPath + "/" + xmlfilename);
		}

		public static string MySqlReadString(string query)
		{
			string result = "";

			try
			{
				MySqlConnection connection = new MySqlConnection(MyGlobalVar.mysqlstring);
				MySqlCommand command = connection.CreateCommand();
				MySqlDataReader Reader;
				command.CommandText = query;
				connection.Open();
				Reader = command.ExecuteReader();
				while (Reader.Read())
				{
					result = Reader.GetValue(0).ToString();
				}
				connection.Dispose();
				connection = null;
				return result;
			}

			catch (MySql.Data.MySqlClient.MySqlException ex)
			{
				throw(ex);
			}
		}

		public static DataSet MySqlReadGrid(string query, string datatable)
		{
			try
			{
				MySqlConnection connection = new MySqlConnection(MyGlobalVar.mysqlstring);
				MySqlDataAdapter da = new MySqlDataAdapter(query, connection);

				DataSet ds = new DataSet();

				da.Fill(ds, datatable);

				connection.Dispose();
				connection = null;

				return ds;
			}

			catch (MySql.Data.MySqlClient.MySqlException ex)
			{
				throw (ex);
			}

		}

		public static void MySqlRun(string query)
		{
			try
			{
				MySqlConnection connection = new MySqlConnection(MyGlobalVar.mysqlstring);
				MySqlCommand cmd = new MySqlCommand(query, connection);

				cmd.Connection.Open();
				cmd.ExecuteNonQuery();

				cmd.Connection.Dispose();
				cmd.Connection = null;
			}

			catch (MySql.Data.MySqlClient.MySqlException ex)
			{
				throw (ex);
			}
		}
	}
}



XmlRead is for reading whatever XML file you put inside the program folder or in sub-folders, you read just one string where you want to read it
XmlRead("test.xml", "table", "item");

XmlWrite is for writing a single data into the specified XML file
XmlWrite("test.xml", "table", "item", "value");

MySqlReadString return as result just one single string from the query you specify, example:
MySqlReadString("Select x from y where b = a");

MySqlReadGrid will return a "DataSet" that you can add as source for any datagridview, very confortable for newbie like me...
MySqlReadGrid("Select * from x", "Whatever");
PS: remember that "whatever" will be what you will specify as databinding for datagridview.

MySqlRun is just a "execute non query" method, for updading, insert and whatever you need to do in ur MySql db...
MySqlRun("Update table set x = y where a = b");



I hope this will help me with some suggestion and help others who really need something that works in a simple way :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1