Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2004, 06:00 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Data Base Connection


Hai all,

I want to connect JavaScript Form to database(MS-Access).

How to Connect?
Please Help me.

 
Old November 25th, 2004, 06:18 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Following example shows how to use javascript to connect to database:

http://webservices.web.cern.ch/WebSe...Db/content.asp

Om Prakash
 
Old November 25th, 2004, 07:32 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Here's a few functions I regularly use, you may want to change the error handling to suit your app:
Code:
   var DATABASE_SERVER = <SQL SERVER NAME>;
   var DATABASE_CATALOG = <Database Name>;
   var CONN_STRING = "Provider=SQLOLEDB; Data Source=" + DATABASE_SERVER + "; Initial Catalog=" + DATABASE_CATALOG + "; Integrated Security=SSPI;"

 function getConnection(sConnString)
 {       
    try
    {  
      var oConn = new ActiveXObject("ADODB.Connection");        
      oConn.ConnectionString = sConnString;
      oConn.open();
      return oConn;
    }
    catch(e)
    {
      throw e;
    }      
 }   
 
 //Get a read-only recordset from a query or stored procedure and a connection object
   function getReadOnlyRS(sQuery, iQueryType, oConn)
   {
     try
     {
      if (oConn.state != adStateOpen)
      {
        oConn.open();
      }     
      var oRS = new ActiveXObject("ADODB.Recordset");
      var nothing = oRS.ActiveConnection;
      oRS.CursorLocation = adUseClient;
      oRS.open(sQuery, oConn, adOpenForwardOnly, adLockReadOnly, iQueryType);
      oRS.ActiveConnection = nothing;  
      //oConn.close();
      return oRS;
     }
     catch(e)
     {       
       throw e;
     }      
   }

   //Get a dynamic recordset from a query or stored procedure and a connection object
   function getDynamicRS(sQuery, iQueryType, oConn)
   {
     try
     {
      if (oConn.state != adStateOpen)
      {
        oConn.open();
      }     
      var oRS = new ActiveXObject("ADODB.Recordset");
      var nothing = oRS.ActiveConnection;
      oRS.CursorLocation = adUseClient;
      oRS.open(sQuery, oConn, adOpenDynamic, adLockOptimistic, iQueryType);
      oRS.ActiveConnection = nothing;  
      //oConn.close();
      return oRS;
     }
     catch(e)
     {       
       throw e;
     }      
   }
      
   function executeNonRSQuery(Conn, SQL)
   {
     var lRecCount;     
     if (Conn.state != adStateOpen)
     {
       Conn.open();
     }
     try
     {
       Conn.execute(SQL, lRecCount, adExecuteNoRecords);
     }
     catch(e)
     {
       throw e;
     }          
   }

--

Joe (Microsoft MVP - XML)
 
Old November 27th, 2004, 01:31 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


hai all,
i got this code from Ome Prakash, i tried from this code to connect java script to MS-Access , but i could not sucsseed.
PLZ Help me!

<html>
<%@ Language="JScript"%>
<%
  con.ConnectionString = "DRIVER={Microsoft Access Driver(*.mdb)};"_
           DBQ=" & Request.ServerVariables("APPL_PHYSICAL_PATH") & "\test.mdb"

   con.Open();

   sql = "SELECT * FROM tabel1 ;
   CurrentRecordset = con.Execute(sql)
%>
<body>
<p>This is a view on the database</p>

<table border="1" cellpadding="0" cellspacing="0" width="100%">
  <tr>
   <td width="25%"><b>Id</b></td>
    <td width="25%"><b>E_id</b></td>
    <td width="25%"><b>Name</b></td>
    <td width="25%"><b>Salary</b></td>
    <td width="25%"><b>Grade</b></td>
  </tr>
<%
   while (! CurrentRecordset.EOF)
%>
  <tr>
    <td width="25%"><% = CurrentRecordset("Id")%></td>
    <td width="25%"><% = CurrentRecordset("E_Id")%></td>
    <td width="25%"><% = CurrentRecordset("Name")%></td>
    <td width="25%"><% = CurrentRecordset("Salary")%></td>
    <td width="25%"><% = CurrentRecordset("Grade")%></td>
  </tr>
<%
   CurrentRecordset.MoveNext();
   }
%>
</table>
</body>
</html>

 
Old November 27th, 2004, 06:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Firstly what is your connection string exactly? As you are trying to connect server side you should build the string and Response.Write it out before trying to connect. Is the path correct and does your IIS account have read/write permissions to the folder and database file?

Personally I would never connect with ODBC to an Access database I would use something like this:
Code:
function getAccessConnectionString(Path)
{
  var sPhysicalPath = Path;
  if (sPhysicalPath.indexOf("\\") == -1)
  {
    sPhysicalPath = Server.MapPath(Path);
  }
  var sConnString = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPhysicalPath + ";User Id=admin;Password=;"
  //For debug
  //Response.Write(sConnString);
  return sConnString; 
}
You can pass a relative url such data/myDB.mdb or the full path, "C:\inetpub\wwwroot\myApp\data\myDB.mdb" (remeber to double up backslashes in JavaScript.

That's for standard security, if your database is password protected or has Access groups etc. see http://www.connectionstrings.com for other versions.



--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Data base Format virus220 ASP.NET 2.0 Professional 0 August 10th, 2007 09:37 AM
attach data base angelboy SQL Language 1 May 30th, 2007 11:07 AM
Transfering data from csv file to data base g_vamsi_krish ASP.NET 1.0 and 1.1 Professional 2 May 16th, 2006 11:58 PM
Data base connection string needed pramos.21d ADO.NET 3 March 8th, 2005 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.