javascript asp教程More About Recordsets_ASP基础

Below we will attempt to access data from a database without knowing the column names. Clearly the best way to utilize data in your database is to keep track of your schema. Schema is the layout of data in your database. The concept is well beyond the scope of this web site, but it is worth mentioning. Most good resources on SQL will also be good resources on database management. Better database schema leads to better ASP code.

Get Started:

Below is the script for Lesson 18.

<%@LANGUAGE="JavaScript"%>
<!-- METADATA TYPE="typelib"
FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<HTML>
<BODY>
<%
var myConnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
myConnect += Server.MapPath("\\")
myConnect += "\\GlobalScripts\\htmlColor.mdb;";

var ConnectObj = Server.CreateObject("ADODB.Connection");
var RS = Server.CreateObject("ADODB.Recordset");
var sql="SELECT * FROM colorChart;";

ConnectObj.Open (myConnect);
RS.Open(sql,ConnectObj,adOpenForwardOnly,adLockReadOnly,adCmdText);

var recordCount = RS.Fields.Count;
var x = 0;
var getFieldNames = false;

Response.Write("<TABLE BORDER=\"1\" CELLSPACING=\"0\">\r");
while (!RS.EOF)
	{
	if (x >= recordCount)
		{
		x = 0
		}
	Response.Write("<TR>");
	if (!getFieldNames)
		{
		while (x <= recordCount-1)
			{
			Response.Write("<TH>" + RS.Fields(x).Name + "</TH>");
			x++;
			}
		getFieldNames = true;
		x = 0;
		Response.Write("</TR>\r<TR>")
		}
	while (x <= recordCount-1)
		{
		Response.Write("<TD>" + RS.Fields(x).Value + "</TD>");
		x++;
		}
	Response.Write("</TR>\r");
	RS.MoveNext();
	}
Response.Write("</TABLE>\r");
RS.Close();
ConnectObj.Close();
RS = null;
ConnectObj = null;
%>
</BODY>
</HTML>

Click Here to run the script in a new window.

I don't think this needs much explaining. The RS.Fields.Count tells us how many columns wide the Recordset is. For each row, we loop through columns using either RS.Fields(x).Name for the colum name or RS.Fields(x).Value for the datum in said column.

Another Way:

A potentially more elegant way to accomplish this same goal is to use the ADO Method GetRows. It returns a multi-dimensional array containing the Recordset data. WAIT! Aren't JavaScript Arrays lexical (and flat)? Yes. We can emulate multi-dimensional arrays, but in reality they are flat. So it's a no-go on the GetRows... unless we do something really creative.

<%@LANGUAGE="JavaScript"%>
<!-- METADATA TYPE="typelib"
FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<HTML>
<BODY>
<%
var myConnect = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
myConnect += Server.MapPath("\\")
myConnect += "\\GlobalScripts\\htmlColor.mdb;";

var ConnectObj = Server.CreateObject("ADODB.Connection");
var RS = Server.CreateObject("ADODB.Recordset");
var sql="SELECT * FROM colorChart;";
ConnectObj.Open (myConnect);
RS.Open(sql,ConnectObj,adOpenForwardOnly,adLockReadOnly,adCmdText);

var myArray = RS.GetRows().toArray();
Response.Write("Let's see the results of myArray as JavaScript");
Response.Write(" sees them (which is flat).<BR>\r");
Response.Write(myArray + "<BR><BR>\r")

RS.MoveFirst();
var myVBArray = new VBArray(RS.GetRows())
Response.Write("We can use the <I>new VBArray</I> constructor and the ")
Response.Write("<I>getItem( )</I> method. For example: myVBArray.getItem(1,1) ")
Response.Write("returns " + myVBArray.getItem(1,1) + "<BR><BR>\r")

Response.Write("Now lets make something useful.<BR>\r")
Response.Write("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>")
Response.Write("\r<TR>")
for (var x=0; x<=myArray.length-1; x++)
	{
	Response.Write("<TD>" + myArray[x] + "</TD>")
	if ((x+1)%RS.Fields.Count==0)
		{
		Response.Write("</TR>\r<TR>")
		}
	}
Response.Write("</TR>\r")
Response.Write("</TABLE>")
RS.Close();
RS = null;
ConnectObj.Close();
ConnectObj = null;
%>
</BODY>
</HTML>

Click Here to run the script in a new window.

Notice when we use getRows( ) we don't get the column names (but that would be really easy to fix). The problem with myArray is that it's not very useful in its raw state. So we use a modulo operator and thanks to a little thing called RS.Fields.Count we can tell how many times we write data to the table before staring a new table row.

If you like the new VBArray constructor you should know that you have the following methods: dimensions() getItem() lbound() toArray() and ubound().

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索javascript
, About
, asp教程More
Recordsets
javascript recordset、asp recordset、asp adodb.recordset、asp recordset open、asp recordset 记录数,以便于您获取更多的相关知识。

时间: 2024-11-10 10:19:59

javascript asp教程More About Recordsets_ASP基础的相关文章

javascript asp教程错误处理_ASP基础

The ASPError Object has zero (0) Methods, nine (9) Properties, zero (0) Events, and zero (0) Collections. AspCode AspDescription Category Column Description File Line Number Source The way you access the ASPError Properties is with a Server Method. Y

javascript asp教程 日期相关_ASP基础

JavaScript is loosely typed. Database files are not. If you put text into a Boolean database column or a Boolean value into a date/time column, then you will get an error. For the most part this is not a problem, except for date/time. It does not cor

javascript asp教程Recordset记录_ASP基础

Recordset is another created/instanciated Object. It is a collection of data taken from a database. Recordset has 26 properties, 25 methods, 11 events, and two (2) collections. The vast majority of Recordset is beyond the scope of this web site. Quic

javascript asp教程创建数据库连接_ASP基础

While this section is devoted to ASP database utilization, it very important to remember that this web site is not intended to be a thorough ASP resource. Remember, the focus of this site is strictly limited to how to use JavaScript as your primary s

javascript asp教程第九课--cookies_ASP基础

Response Cookies in General: We'll start with the Response Cookies collection. I don't think it could be any easier. You simply put the name of the cookie in the argument. The corresponding value is a string. The only time it gets complicated is when

javascript asp教程服务器对象_ASP基础

Overview: The Server Object has seven (7) Methods, one (1) Property, zero (0) Events, and zero (0) Collections. List of Methods: Server Methods CreateObject( ) Server.CreateObject("ADODB.Recordset")Create an instance of an ObjectExecute( ) Serve

javascript asp教程添加和修改_ASP基础

The Connection Execute(): If you want to retrieve data from a database then you have no choice but to use a Recordset. However, for the purposes of adding, updating, and deleting data you don't necessarily have to have a Recordset. It's up to you. Fo

javascript asp教程第十一课--Application 对象_ASP基础

Overview: The Application Object represents a collection of ASP pages. The Application object has zero (0) properties, two (2) collections, two (2) methods, and two (2) events. Get Started: Below are a couple scripts for lesson11. <%@LANGUAGE="Jav

javascript asp教程第八课--request对象_ASP基础

Request Object: Request has five (5) Collections, one (1) Property, and one (1) Method. You'll use the Collections far more than the property or the method. Request Collections: Below is a table of the Request Collections and descriptions of how they