Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / Windows Forms

Working C# code for MySql Stored Procedures IN, OUT, and INOUT parameters

Rate me:
Please Sign up or sign in to vote.
4.29/5 (10 votes)
15 May 2009CPOL9 min read 179.3K   3.5K   42  
An article on how to get parameters into and out of MySql stored procedures.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace StoredProcs
{
    public partial class Form1 : Form
    {
        clsKenTest ken;
        string ans;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ken = new clsKenTest("server=localhost;userid=demo;database=test;pooling=true");
        }
        private string go(string sql)
        {
            try
            {
                return ken.ExecSql(sql);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return ex.Message;
            }
        }

        private void CreateTable_Click(object sender, EventArgs e)
        {
            ans = go(ken.table);
            txtTable.Text = ans;
        }

        private void DropProc_Click(object sender, EventArgs e)
        {
            ans = go(ken.drop);
            txtDropProc.Text = ans;
        }

        private void CreateProc_Click(object sender, EventArgs e)
        {
            ans = go(ken.proc);
            txtCreateProc.Text = ans;
        }

        private void spInsert_Click(object sender, EventArgs e)
        {
            ans = ken.DoInsert(ken.nsert);
            txtInsert.Text = ans;
        }

        private void proc2_Click(object sender, EventArgs e)
        {
            ans = ken.runProc();
            if (ans.Equals("OK"))
            {
                txtProc2.Text = ken.lastinsertid.ToString();
                txtConcat.Text = ken.newval;
            }
            else
            {
                txtProc2.Text = ans;
                txtConcat.Text = "Error";
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Senior software developer / consultant with 30 years experience.

Comments and Discussions