Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.7K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using MySql.Data.MySqlClient;

namespace SubSonic
{
    public class MySqlInnoDBDataProvider : MySqlDataProvider
    {
        public override string GetForeignKeyTableName(string fkColumnName)
        {
            string tableName = string.Empty;

            if(SupportsInformationSchema(GetDatabaseVersion(Name)))
            {
                string sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE COLUMN_NAME = ?ColumnName AND CONSTRAINT_NAME = 'PRIMARY' AND TABLE_SCHEMA = ?DatabaseName";

                using (AutomaticConnectionScope automaticConnectionScope = new AutomaticConnectionScope(this))
                {
                    MySqlCommand cmd = new MySqlCommand(sql);
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = (MySqlConnection)automaticConnectionScope.Connection;

                    cmd.Parameters.AddWithValue("?ColumnName", fkColumnName);
                    cmd.Parameters.AddWithValue("?DatabaseName", cmd.Connection.Database);

                    object result = cmd.ExecuteScalar();
                    if (result != null)
                    {
                        tableName = result.ToString();
                    }
                }
            }

            return tableName;
        }

        public override string GetForeignKeyTableName(string fkColumnName, string tableName)
        {
            string returnTableName = string.Empty;

            if (SupportsInformationSchema(GetDatabaseVersion(Name)))
            {
                string sql = "SELECT REFERENCED_TABLE_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE COLUMN_NAME = ?ColumnName AND TABLE_NAME = ?TableName AND TABLE_SCHEMA = ?DatabaseName";

                using (AutomaticConnectionScope automaticConnectionScope = new AutomaticConnectionScope(this))
                {
                    MySqlCommand cmd = new MySqlCommand(sql);
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = (MySqlConnection)automaticConnectionScope.Connection;

                    cmd.Parameters.AddWithValue("?ColumnName", fkColumnName);
                    cmd.Parameters.AddWithValue("?TableName", tableName);
                    cmd.Parameters.AddWithValue("?DatabaseName", cmd.Connection.Database);

                    object result = cmd.ExecuteScalar();
                    if (result != null)
                    {
                        returnTableName = result.ToString();
                    }
                }
            }

            return returnTableName;
        }
    }
}

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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions