CROQUET AND SQL DATABASES - PART 2

At this 2nd part of our serie about how to write and read from a Squeak-Croquet space to a SQL database, we will read the data from the Croquet101 database (table: students) and present it inside a ScrollingText.

The names of the objects used are in the figure. Important to note that StuListText is part of StuList - this last really is not used.

The script glued to the rectangle Click, after the graphic instantialization of the other object is:

  
| page parsedPage thebody clean |
StuListText setCharacters: 'Searching the database.Wait... '.
page:=HTTPSocket httpGet:'http://www.dmu.com/croquet/readstudents.aspx'.
parsedPage := HtmlParser parse: page. 
thebody := parsedPage body.
clean := thebody   contents at:1 .  
clean := clean  asString .
clean := clean copyReplaceAll: '[' with:''. 
clean := clean copyReplaceAll: ']' with:''. 
clean := clean  copyReplaceAll: '#' with: '\'.
clean := clean  copyReplaceAll: ':' with: ''.
StuListText setCharacters: clean withCRs.

The script in JavaScript, to be uploaded to the server having the database, is a little more sophisticated:

<script language="JavaScript" runat="server">
function Page_Load(sender:Object, e:EventArgs):void{
 var msg;
 msg = readDB();
 Response.Write(msg); 
}
function readDB(){ 
var select = "SELECT * FROM students";  
 //The connection
 var oConn:System.Data.OleDb.OleDbConnection = new System.Data.OleDb.OleDbConnection(); 
 oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/croquet101.mdb"); 
 try{   
  oConn.Open(); 
  //The DataSet creation
  var ds:System.Data.DataSet  = new System.Data.DataSet(); 
  var datadapt:System.Data.OleDb.OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter(select, oConn); 
  datadapt.Fill(ds, "stu"); 
  var dsT:System.Data.DataSet  = new System.Data.DataSet();
  dsT = ds; 
  var list = new String("");
  var pad : char = " ";
  for(var i=0; i<dsT.Tables["stu"].Rows.Count; i++){
 list = list + dsT.Tables["stu"].Rows[i]["country"].ToString().Replace("_"," ").PadRight(20, pad) + ":"
            +  dsT.Tables["stu"].Rows[i]["name"].ToString().Replace("_"," ")  + "#";
  //All this needs to be in one line, please!
 }
 //To erase the last separator - we don't need it:
 list=list.slice(0,list.lastIndexOf("#"));
 return list;
}
 catch(e:Exception) {
  //Write the error, if we have one
  return "Error: "+  e.ToString();
 }
 finally{
  //having error or not we close the connection, a good pratice
  oConn.Close();
 }
}
</script>

CODE COMMENTS:

TIP: Having this ASPX script and changing the bolded part you can use .NET without to know too much about it... All is more or less the same, ever...

You can test this project!

You can test this project!

Its address is: http://www.dmu.com/Tealand/readstudents12.006.pr.pr

And re-read the lesson about how to create a 3D window for a project. Use for "extent": 1400 x 1050.

Try to install this application and that of the previous lesson side by side in the space, like in the figure:

WARNING. If you don't have a powerfull computer and a fast communication line it's possible that you will not have a very responsible application. We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps) - Windows XP Professional SP1


PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE

DISCLAIMER: This material can be translated for any language, and reproduced total or partially by anybody using any type of media. But, please, don't put your name like author. And let me know if it was useful (americo@dmu.com).The use of any code, 3D model or technique published is free and doesn't need to have any reference about this source.The author is not responsible for any damage that the material can cause to your professional or sexual life.