Click here to Skip to main content
6,305,776 members and growing! (16,871 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Database Viewer and Updater for any Database

By avronp

View and update any database table and metadata. Includes sample databases.
C#, Windows, .NET 2.0, ASP.NET, IIS, Visual Studio, ADO.NET, SQL 2000, SQL 2005, DBA, Dev
Posted:18 Jul 2006
Updated:25 Jul 2006
Views:45,054
Bookmarked:70 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
11 votes for this article.
Popularity: 4.49 Rating: 4.31 out of 5

1

2
3 votes, 27.3%
3
1 vote, 9.1%
4
7 votes, 63.6%
5

MyDbViewer

Introduction

This is a completely dynamic database viewer. It can be used to:

  • View any database
  • Navigate between tables
  • Select data by simply entering the values in the fields
  • Perform updates of table data
  • Display data in grids
  • Display data as single records
  • View metadata

GridView

Purpose of the DbViewer

The purpose of this viewer is to enable direct manipulation of tables without having to use SQL Server or MS Access. It is mainly a tool for developers and not for end users although it could be customized for users.

Uses of DbViewer

  • Quick manipulation of tables
  • Content management

Using the code

Unzip and open in VS2005 as a Web Site. Run the code from the default.htm page.

(default.htm is a dummy page which loads the relevant aspx pages in the _Chapter13_databaseviewer folder.)

The database class DbData

All the database logic of the application has been placed in the database class, DbData.

Code snippets

The metadata information about tables is not obtained by reading tables but by using the GetSchema command. See the functions in the DbData class which shows how to retrieve columns.

private string dbFieldsSQL(string astrTable, string astrField, bool abReadOnly)
{
    DataTable oDataTable, oIndexTable;
    SqlConnection oConn;
    string        strRows;

    try
    {
        oConn       = dbSqlConnection();
        oConn.Open();
        oDataTable  = oConn.GetSchema("Columns");
        oIndexTable = oConn.GetSchema("IndexColumns");
        strRows     = dbFields(oDataTable, oIndexTable, 
                      astrTable, astrField, abReadOnly);
        oConn.Close();
    }
    catch (Exception err)
    {
        strRows = err.Message;
    }
    return strRows;
}

private string dbFields(DataTable oDataTable, DataTable oIndexTable, 
               string astrTable, string astrField, bool abReadOnly)
{
    int      intNo, intMax;
    string   strCol, strRows;
    string[] arrFields = new string[1000];
    
    strRows  = "\n\n";
    strRows += dbStyle();
    
    // SORT BY ORDINAL_POSITION

    intMax = 0;
    strRows += "<TABLE BORDER=0 CELLSPACING=1>\n";
    foreach (DataRow oRow in oDataTable.Rows)
    {
        if (astrTable == oRow["TABLE_NAME"].ToString())
        {
            if (astrField == "" || astrField.ToLower().Trim() 
                == oRow["COLUMN_NAME"].ToString().ToLower().Trim())
            {
                // intNo = (int) oRow["ORDINAL_POSITION"];

                intNo            = Convert.ToInt32(oRow["ORDINAL_POSITION"]);
                intMax           = (intNo > intMax) ? intNo : intMax;
                strCol           = oRow["COLUMN_NAME"].ToString();
                arrFields[intNo] = "<TR><TD CLASS=Smalls " + 
                                   "ALIGN='left'>" + strCol + 
                                   "<BR /><INPUT NAME=INPUT_" + 
                                   strCol + " ###" + strCol + "### ";
                // READONLY if Key/Index Field

                if (abReadOnly)
                {
                    foreach (DataRow oIndexRow in oIndexTable.Rows)
                    {
                        if (astrTable == oIndexRow["table_name"].ToString() && 
                            oRow["COLUMN_NAME"].ToString() == 
                            oIndexRow["column_name"].ToString())
                        {
                            arrFields[intNo] += 
                                " READONLY STYLE='background: #C0C0C0;' ";
                            break;
                        }
                    }
                }
                arrFields[intNo] += "></TD></TR>\n";
            }
        }
    }
    for (intNo=1;intNo<intMax+1;intNo++)
        {strRows += (arrFields[intNo] != "") ? arrFields[intNo] : "";}
    strRows += "</TABLE>\n";

    return strRows;
}

History

  • 25 July 2006 - updated download

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

avronp


Member
Jazz Drummer (1½/2½/3.5++), Writer, Grandfather, Possibly World's Oldest Web Developer, Rebel, Anti-globalization, Prisoner of conscience
Occupation: Web Developer
Location: Israel Israel

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralGreat PinmemberVitali Halershtein6:39 4 Jun '08  
Generalopen database Pinmembergeorgeen11:11 29 May '07  
QuestionAccess databse connectivity Pinmemberhygeenams19:51 18 Jan '07  
QuestionHow to connect to Oracle DB?? PinmemberGsPandian23:52 27 Dec '06  
GeneralWhen I substitute a different Access database I get an error PinmemberGixxer1k11:49 8 Oct '06  
GeneralWhy you use SQLConnection in your example Pinmemberjpazgier7:45 26 Jul '06  
GeneralRe: Why you use SQLConnection in your example Pinmemberavronp11:07 29 Jul '06  
QuestionError when accessing Access Table: MSysAccessObjects Pinmemberchengkc4:41 25 Jul '06  
AnswerRe: Error when accessing Access Table: MSysAccessObjects Pinmemberavronp11:00 29 Jul '06  
GeneralRe: Error when accessing Access Table: MSysAccessObjects Pinmemberchengkc7:16 30 Jul '06  
GeneralMissing Files Pinmember-JeRoK-10:22 18 Jul '06  
GeneralRe: Missing Files Pinmember-JeRoK-12:49 18 Jul '06  
GeneralRe: Missing Files Pinmemberyermalpavan20:46 24 Jul '06  
GeneralRe: Missing Files Pinmemberavronp10:55 29 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jul 2006
Editor: Chris Maunder
Copyright 2006 by avronp
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project