Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello...


I'm using Visual Studio 2010 express edition.
I am looking MySql Database Connection with MVC3.net
Actually this way I want to use database first approach.
Where I can get database and tables from MySQL.

I tried to make a DSN but when I add ADO.NET Entity Frame Work in Models it doesn't appear in Data Provider list also. There fore I am not able to get the database and tables from MYSQL in my MVC3 application.


I'm looking for this so long. and read the articles for .net MVC but couldn't get the right way..


Please guide me...


Regards
Manish Kumnar Namdev
Posted

1 solution

using MySql.Data.MySqlClient;
using System.Data;
using System;
using System.Data.Common;
class DbHelper
{
    public MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=root;database=dothrmproxy");
// ConnectionString
    public MySqlCommand cmd;
    public MySqlDataAdapter Mdr;   

    public int Execute(string query)
    {
        int i = 0;
        try
        {
            con.Open();
            cmd = new MySqlCommand(query, con);
            i = cmd.ExecuteNonQuery();
        }
        catch
        {
            i = -1;
        }
        finally
        {
            con.Close();
        }
        return i;
    }
    public int Execute(string commandtext,object [,]parameter,CommandType ct)
    {
        int i = 0;
        try
        {
            con.Open();
            cmd = new MySqlCommand(commandtext, con);
            cmd.CommandType = ct;
            MySqlParameter oParameter;
            for (int j = 0; j < parameter.Length / 2; j++)
            {
                oParameter = cmd.CreateParameter();
                oParameter.ParameterName = parameter[j, 0].ToString();
                oParameter.Value = parameter[j, 1];
                cmd.Parameters.Add(oParameter);
            }
            i = cmd.ExecuteNonQuery();
        }
        catch
        {
            i = -1;
        }
        finally
        {
            con.Close();
        }
        return i;
    }
    public DataTable GetTable(string query)
    {
        DataTable result = new DataTable();
        try
        {
            Mdr = new MySqlDataAdapter(query, con);
            if (con.State == ConnectionState.Closed)
                con.Open();
            Mdr.Fill(result);
            return result;
        }
        catch (Exception err)
        { return result; }
        finally
        {
            con.Close();
        }
    }
    public DataSet GetSet(string query)
    {
        DataSet result = new DataSet();
        try
        {
            Mdr = new MySqlDataAdapter(query, con);
            if (con.State == ConnectionState.Closed)
                con.Open();
            Mdr.Fill(result);
            return result;
        }
        catch (Exception err)
        { return result; }
        finally
        {
            con.Close();
        }
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900