<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wcode.net &#187; MySQL</title>
	<atom:link href="http://wcode.net/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://wcode.net</link>
	<description>Article and Personal Blog</description>
	<lastBuildDate>Tue, 24 Nov 2009 10:51:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# with MySQL Database</title>
		<link>http://wcode.net/2009/10/csharp-with-mysql-database/</link>
		<comments>http://wcode.net/2009/10/csharp-with-mysql-database/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 16:23:44 +0000</pubDate>
		<dc:creator>Willy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://wcode.net/?p=71</guid>
		<description><![CDATA[If you want direct connection from C# to MySQL you can use MySQL.Data library from this resource. I&#8217;ve write down my own DataAccess class to make my work in other class easier.
Settings is setting class in .NET you can click on project->properties->settings to generate this class. You can find this file in Debug folder that [...]]]></description>
			<content:encoded><![CDATA[<p>If you want direct connection from C# to MySQL you can use MySQL.Data library from <a href="http://dev.mysql.com/downloads/connector/net/1.0.html">this</a> resource. I&#8217;ve write down my own DataAccess class to make my work in other class easier.</p>
<p><em>Settings</em> is setting class in .NET you can click on project->properties->settings to generate this class. You can find this file in Debug folder that have .config extension.</p>
<p><span id="more-71"></span></p>
<p>Here is my Data Access class manage command from C# to MySQL database.</p>
<pre class="brush:csharp">
    public class DataAccess
    {
        static internal string GetConnectionString()
        {
            return String.Format("server={0};user id={1};Password={2};database={3}",
                Settings.Default.DbServer, Settings.Default.DbUserId, Settings.Default.DbPassword,
                Settings.Default.DbName);
        }

        static DataAccess()
        { }

        public static DataTable ExecuteSelectCommand(MySqlCommand command)
        {
            DataTable table = null;
            try
            {
                command.Connection.Open();
                MySqlDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Data.ToString());
            }
            finally
            {
                command.Connection.Close();
            }
            return table;
        }

        public static MySqlCommand CreateCommand()
        {
            string dataProviderName = Settings.Default.DbProvoderName;
            string connectionString = GetConnectionString();
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = connectionString;
            MySqlCommand comm = conn.CreateCommand();
            return comm;
        }

        public static int ExecuteNonQuery(MySqlCommand command)
        {
            int affectedRows = -1;
            try
            {
                command.Connection.Open();
                affectedRows = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
            return affectedRows;
        }
    }
</pre>
<p>Now here&#8217;s the sample to call that DataAccess class</p>
<pre class="brush:csharp">
    public class Golongan
    {
        private int idGolongan;
        private string namaGolongan;

        public int IdGolongan
        {
            get { return idGolongan; }
            set { idGolongan = value; }
        }

        public string NamaGolongan
        {
            get { return namaGolongan; }
            set { namaGolongan = value; }
        }

        public Golongan()
        { }

        public Golongan(int idGolongan, string namaGolongan)
        {
            this.idGolongan = idGolongan;
            this.namaGolongan = namaGolongan;
        }

        public static DataTable SelectAllGolongan()
        {
            MySqlCommand command = DataAccess.CreateCommand();
            command.CommandText = "select * from golongan";
            DataTable table = DataAccess.ExecuteSelectCommand(command);
            return table;
        }

        public int InsertGolongan()
        {
            MySqlCommand commmand = DataAccess.CreateCommand();
            commmand.CommandText = "insert into golongan (golongan) " +
                                    "values(    '" + this.namaGolongan + "')";

            return DataAccess.ExecuteNonQuery(commmand);
        }

        public int UpdateGolongan()
        {
            MySqlCommand commmand = DataAccess.CreateCommand();
            commmand.CommandText =
                String.Format("update golongan set golongan = '{0}' where idgolongan = {1}",
                this.namaGolongan, this.idGolongan);

            return DataAccess.ExecuteNonQuery(commmand);
        }

        public int DeleteGolongan()
        {
            MySqlCommand commmand = DataAccess.CreateCommand();
            commmand.CommandText =
                String.Format("delete from golongan where idgolongan = {0}",
                this.idGolongan);

            return DataAccess.ExecuteNonQuery(commmand);
        }
}
</pre>
<p>Hope it&#8217;s help<br />
Willy Achmat Fauzi</p>
]]></content:encoded>
			<wfw:commentRss>http://wcode.net/2009/10/csharp-with-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
