Click here to Skip to main content
15,896,448 members
Articles / Programming Languages / ASP

Online DB Administration

Rate me:
Please Sign up or sign in to vote.
3.88/5 (16 votes)
17 Mar 2003 237.4K   2.5K   58  
Online administration of your database. Create or modify tables, keys, and indexes. Edit or insert data.
<%@language=javascript%>
<!--#include file=common.asp-->
<!--#include file=b.grid.asp-->
<%
	function Content(oConn) {
		if(Request.Form("createtable").Count==1) {
			var sql = "";
			var sqlPK = "";
			try {
				var sTableName = Request.Form("tablename").Item;

				if(IsJet(oConn))
					sql = "create table [" + sTableName + "](\n";
				else
					sql = "create table \"" + sTableName + "\"(\n";

				var nCol = 0;
				do {
					var nDataType = GetFormValue("typename"+nCol,"");
					if(nDataType=="") break;
					
					var sColName = Request.Form("fieldname"+nCol).Item;
				
					if(nCol>0) sql += ",\n";
					if(IsJet(oConn))
						sql += ' [' + sColName + ']';
					else
						sql += ' "' + sColName + '"';
					sql += " " + nDataType;
					if(Request.Form("DefinedSize"+nCol).Item!="") {
						sql += "(" + Request.Form("DefinedSize"+nCol).Item + ")";
					} else if(Request.Form("Precision"+nCol).Item!="" || Request.Form("NumericScale"+nCol).Item!="") {
						sql += "(";
						if(Request.Form("Precision"+nCol).Item!="")
							sql += Request.Form("Precision"+nCol).Item;
						else
							sql += "0";
						sql += ",";
						if(Request.Form("NumericScale"+nCol).Item!="")
							sql += Request.Form("NumericScale"+nCol).Item;
						else
							sql += "0";
						sql += ")";
					}

					if(Request.Form("FieldNullable"+nCol).Count==1)
						sql += " null";
					else
						sql += " not null";

					if(Request.Form("AutoIncrement"+nCol).Count==1)
						sql += " identity";

					if(Request.Form("PrimaryKey"+nCol).Count==1) {
						if(sqlPK!="") sqlPK += ",";
						if(IsJet(oConn))
							sqlPK += '[' + sColName + ']';
						else
							sqlPK += '"' + sColName + '"';
					}

					nCol++;
				} while(true);
				if(sqlPK!="") {
					if(IsJet(oConn))
						sql += ',\n constraint [PK_' + sTableName + '] primary key(' + sqlPK + ')';
					else
						sql += ',\n constraint "PK_' + sTableName + '" primary key(' + sqlPK + ')';
				}
				sql += "\n)";

				oConn.Execute(sql);
				EndPage('columns.asp?table='+sTableName);
				return;
			}
			catch(e) {
				Out('\n<s'+'cript type="text/javascript">\nSetErrorMessage("' + e.description + '");\n</scr'+'ipt>\n');
			}
		}

		Out('<table class="list" cellspacing="0" cellpadding="0" width="100%">');
		Out("<tr><th colspan=8>Table Name:</th></tr>");
		Out("<tr><td colspan=8><input type=edit name=tablename value=\"" + GetFormValue("tablename","") + "\"/></td></tr>");

		Out('<tr><th>Fields</th><th>Data Type</th><th>Defined Size</th><th>Precision</th><th>Numeric Scale</th><th>Primary Key</th><th>Nullable</th><th>Auto Increment</th></tr>');
		var nCol = 0;
		var bTitle = false;
		do {
			var nDataType = GetFormValue("typename"+nCol,"");

			Out("<tr class=gridrow><td>");
		
			Out("<input type=edit name=\"fieldname"+nCol+"\" value=\""+GetFormValue("fieldname"+nCol,"")+"\"/></td><td>");
		
			Out("<select onchange=\"this.form.submit()\" name=\"typename"+nCol+"\">");
			Out("<option/>");
			var rsProviderTypes = oConn.OpenSchema(adSchemaProviderTypes);
			while(!rsProviderTypes.EOF) {
				Out("<option ");
				if(nDataType==rsProviderTypes("TYPE_NAME").Value)
					Out("selected ");

				Out("value='"+rsProviderTypes("TYPE_NAME").Value+"'>" + rsProviderTypes("TYPE_NAME").Value + "</option>");
				rsProviderTypes.MoveNext();
			}
			Out("</select></td>");
			rsProviderTypes.Close();
			rsProviderTypes = null;
			
			// DefinedSize
			Out("<td><input style=\"width:100%\" type=edit name=\"DefinedSize"+nCol+"\" value=\"" + GetFormValue("DefinedSize"+nCol,"") + "\"/></td>");
			// Precision
			Out("<td><input style=\"width:100%\" type=edit name=\"Precision"+nCol+"\" value=\"" + GetFormValue("Precision"+nCol,"") + "\"/></td>");
			// NumericScale
			Out("<td><input style=\"width:100%\" type=edit name=\"NumericScale"+nCol+"\" value=\"" + GetFormValue("NumericScale"+nCol,"") + "\"/></td>");
			// Primary Key
			Out("<td><input type=checkbox "+(GetFormValue("PrimaryKey"+nCol,false)?"checked ":"")+"name=\"PrimaryKey"+nCol+"\"/></td>");
			// Nullable
			Out("<td><input type=checkbox "+(GetFormValue("FieldNullable"+nCol,false)?"checked ":"")+"name=\"FieldNullable"+nCol+"\"/></td>");
			// Autoincrement
			Out("<td><input type=checkbox "+(GetFormValue("AutoIncrement"+nCol,false)?"checked ":"")+"name=\"AutoIncrement"+nCol+"\"/></td>");

			Out("</tr>");
			nCol++;
			if(nDataType=="") break;
		} while(true);

		Out("<tr><td colspan=8><input type=submit value=Create name=createtable></td></tr>");
		Out("</table>");

		Out('<br>');
		Out('<table class="list" cellspacing="0" cellpadding="0">\n');
		Out('<tr>\n');
  		Out('<td>&nbsp;<a href=".">Back</a>&nbsp;</td>\n');
  		Out('</table>\n');
	}

	PageSetup();
	NewPageHeader();
	Content(Object.Conn);
	NewPageFooter();
	PageCleanup();
%>

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 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


Written By
Web Developer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions