connect to an access database using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kartheek
    New Member
    • Sep 2006
    • 2

    connect to an access database using javascript

    hi friends,

    can any one out here help me by giving me the code to connect to an MSACCESS database using javascript.
  • phpmaet
    New Member
    • Sep 2006
    • 27

    #2
    Is there a way to do it?

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      it would be a task to do such a thing seeming how the database isn't going to be on every users computer, javascript is client side, you would need a serverside script to do this for you, ASP and PHP can both efficiently do this.

      Comment

      • kartheek
        New Member
        • Sep 2006
        • 2

        #4
        to connect to an MSACCESS database using ASP(javascript)

        hi friends,

        can any one out here help me by giving me the code to connect to an MSACCESS database using ASP(javascript)

        Comment

        • dannyh
          New Member
          • Sep 2006
          • 1

          #5
          I am also interesting in using JS to access an MS-Access file. I know this is CLIENT SIDE. The access file will be locally hosted so JS seems the way to go.

          I am very new to JS, but looking use it to aid in IE pages that will be dynamically created from a DVD that is distributed (not from the web).

          The only snip-it that I am starting to review is as follows:

          <% @LANGUAGE="JScr ipt" %>


          var strConnect = "DSN=YourDSNHer e";
          oConn=Server.Cr eateObject("ADO DB.Connection") ;
          oConn.Open(strC onnect);

          var strSQL = "SELECT SomeField FROM myTable;";
          var rsAd=oConn.Exec ute(strSQL);



          Response.Write( "First row is, "+ rsAd.Fields("So meField") )

          oConn.Close();

          Comment

          • sanjujohn
            New Member
            • Feb 2007
            • 1

            #6
            Originally posted by kartheek
            hi friends,

            can any one out here help me by giving me the code to connect to an MSACCESS database using javascript.
            If you are using asp.net, there is no way to connect to database using standard javascript. You have to install server side javascript and you can do data connection with any data souce and delete,modify,v iew the records of the table.
            That is the only way.Bye

            Sanju John

            Comment

            • dorinbogdan
              Recognized Expert Contributor
              • Feb 2007
              • 839

              #7
              You can connect to database from client-side script, if MDAC is installed on client's computer.
              Just Update strConn with the correct Data Source, and SQL query as desired.
              Code:
              <html>
              	<head>
              		<title>Entitled Document</title>
              		<script language="JavaScript">
              		function getCount(){
              			var cn = new ActiveXObject("ADODB.Connection");
              			var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\clientDB.mdb;Persist Security Info=False";
              			cn.Open(strConn);
              			var rs = new ActiveXObject("ADODB.Recordset");
              			var SQL = "select count(*) from Customers";
              			rs.Open(SQL, cn);
              			alert(rs(0));
              			rs.Close();
              			cn.Close(); 
              			
              		}
              		</script>
              	</head>
              	<body>
              		<input type="button" value="Get count" onclick="getCount()">
              	</body>
              </html>
              Did it help?
              Last edited by Dormilich; Apr 21 '09, 07:46 PM. Reason: fixed code tags

              Comment

              • dains
                New Member
                • May 2007
                • 4

                #8
                Hi, thanks for posting this solution! I'm working on a local application which uses a local DB as well, so this is a great snip.

                I do have a question about extending this. My DB is not Access and uses an ActiveX DLL for communication, but I've never worked with ActiveX in this manner. Would you happen to know how to modify this script to work with an ActiveX COM object?

                I'm guessing it would be something like this to start, but obviously I'm clueless, so please correct as necessary.
                6.
                var cn = new ActiveXObject(" ActiveXDB.com") ;

                but then I'm clueless as to how to open a connection, send data to it or get the data back?

                Sorry if this is a newbie question, but I've been searching for a day now and most answers I find are just "No, Javascript has no business talking to a database". If this method works out, it would be an awesome decrease in system requirements for my application, so I very much appreciate any help available.

                Thanks!
                D

                Comment

                • dorinbogdan
                  Recognized Expert Contributor
                  • Feb 2007
                  • 839

                  #9
                  Yes it should work.
                  I suppose that know the methods of your DLL, then just follow the previous ADODB sample and update it as needed.

                  Please note that only IExplorer allows ActiveX objects, and FireFox, Opera... do not allow.

                  Comment

                  • dains
                    New Member
                    • May 2007
                    • 4

                    #10
                    Blarg, getting "Automation server can't create object" when running with an ActiveX friendly platform (HTA for testing purposes). Can anyone see what I'm doing wrong here that causes the error?

                    SQLiteDb.DLL is registered on the machine, I even took the classID out of the registry to make sure it was right.Thanks for any help!

                    Code:
                    <html>
                    <head>
                    <object>
                    id = "SQLiteDb"
                    CLASSID = "CLSID:1B562564-1BB6-4970-B0FB-31B24AA9C1D0"
                    </object>
                    
                    <script language="JavaScript">
                    
                    var cn = new ActiveXObject("SQLiteDb.Connection");
                    var strConn = "Database=db/test.db";
                    </script>
                    
                    </head>
                    <body>
                    
                    <script language="javascript">
                    function getdata(){
                    cn.Open(strConn);
                    var rs = new ActiveXObject("SQLiteDb.Recordset");
                    var SQL = "SELECT CoName from Companies WHERE CoID=1";
                    rs.Open(SQL, cn);
                    alert(rs(0));
                    rs.Close();
                    cn.Close();
                    }
                    </script>
                    <input type="button" value="Get data"
                    onclick="getdata()">
                    </body>
                    Last edited by Dormilich; Apr 21 '09, 07:47 PM. Reason: added [code] tags, please use [code] tags when posting code!

                    Comment

                    • dorinbogdan
                      Recognized Expert Contributor
                      • Feb 2007
                      • 839

                      #11
                      Run this code and see if connection is created:
                      [code=html]<html>
                      <head>

                      <script language="JavaS cript">

                      var cn = new ActiveXObject(" SQLiteDb.Connec tion");
                      if (typeof(cn)=="u ndefined" || cn==null)
                      alert("Unable to create the connection object!");
                      else
                      alert("Connecti on object was successfully created!");

                      var strConn = "Database=d b/test.db";
                      function getdata(){
                      cn.Open(strConn );
                      var rs = new ActiveXObject(" SQLiteDb.Record set");
                      var SQL = "SELECT CoName from Companies WHERE CoID=1";
                      rs.Open(SQL, cn);
                      alert(rs(0));
                      rs.Close();
                      cn.Close();
                      }
                      </script>

                      </head>
                      <body>

                      <input type="button" value="Get data"
                      onclick="getdat a()">
                      </body>
                      </html>[/code]

                      Comment

                      • dains
                        New Member
                        • May 2007
                        • 4

                        #12
                        Originally posted by dorinbogdan
                        Run this code and see if connection is created:
                        [code=html]<html>
                        <head>

                        <script language="JavaS cript">

                        var cn = new ActiveXObject(" SQLiteDb.Connec tion");
                        if (typeof(cn)=="u ndefined" || cn==null)
                        alert("Unable to create the connection object!");
                        else
                        alert("Connecti on object was successfully created!");

                        var strConn = "Database=d b/test.db";
                        function getdata(){
                        cn.Open(strConn );
                        var rs = new ActiveXObject(" SQLiteDb.Record set");
                        var SQL = "SELECT CoName from Companies WHERE CoID=1";
                        rs.Open(SQL, cn);
                        alert(rs(0));
                        rs.Close();
                        cn.Close();
                        }
                        </script>

                        </head>
                        <body>

                        <input type="button" value="Get data"
                        onclick="getdat a()">
                        </body>
                        </html>[/code]
                        Hi, thanks for the help! And the survey says... Nope! Still has the "Automation Server" error. Interesting that I didn't need to put in the Object CLSID? I thought it was necessary for the registry to look up this object?

                        Anyways, this has got to be about creating and using the ActiveX object. I've posted on the manufacturer forums and gotten no helpful responses, but this code was in their help:
                        Code:
                        Dim cn As SQLiteDb.Connection
                        Set cn = New SQLiteDb.Connection
                        
                        cn.ConnectionString = "Data Source=C:\MyData\MyDatabase.db;"
                        cn.Open
                        So if this is supposed to work in VBscript, I can't see why the Javascript doesn't work, unless it has something to do with the "As SQLiteDb.Connec tion" line?

                        I have a different setup to the same DB with an ODBC driver and using an ADODB connection object, and it works fine. Unfortunately, that won't work for the distribution because it still requires installation of the driver and ADO to be installed on the client machine.

                        So, is there an ActiveX forum that I should move over to for this? Or are we ok to keep working here?
                        Thanks again for the help!
                        Last edited by dains; May 31 '07, 03:20 PM. Reason: added code tags

                        Comment

                        • dorinbogdan
                          Recognized Expert Contributor
                          • Feb 2007
                          • 839

                          #13
                          I guess that your ActiveX does not expose a COM interface for any application.
                          Perhaps it's a VB6 DLL that works in VB only (maybe .Net too).

                          At this point just the manufacturer should offer more support.

                          God bless you,
                          Dorin.

                          Comment

                          • dains
                            New Member
                            • May 2007
                            • 4

                            #14
                            Thanks for all of your help Dorin. At least I'm up and running with the ODBC method in Javascript, so I'm not at a total work stoppage :)

                            I agree about the manufacturer. They advertise that this is usable by any COM-aware language, including VBscript like the example I posted, but they surely ought to give full information on using it as a web object. I don't know how to make this work only because every other ActiveX object I've ever used has come with a code snippet from the manufacturer, and all I did was paste that in and get to work with it.

                            So, guess I'll keep looking for a solution instead of buying theirs. Too bad, theirs supported ADO-style requests, and that would let me use a lot of standard methods to get the job done quicker.

                            Thanks again for your help, you've been great!

                            Comment

                            • rcbrown
                              New Member
                              • Aug 2007
                              • 1

                              #15
                              Dorin,

                              Thank you for your post; it helped me alot.

                              Bobby

                              Originally posted by dorinbogdan
                              You can connect to database from client-side script, if MDAC is installed on client's computer.
                              Just Update strConn with the correct Data Source, and SQL query as desired.
                              Code:
                              <html>
                              	<head>
                              		<title>Entitled Document</title>
                              		<script language="JavaScript">
                              		function getCount(){
                              			var cn = new ActiveXObject("ADODB.Connection");
                              			var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\clientDB.mdb;Persist Security Info=False";
                              			cn.Open(strConn);
                              			var rs = new ActiveXObject("ADODB.Recordset");
                              			var SQL = "select count(*) from Customers";
                              			rs.Open(SQL, cn);
                              			alert(rs(0));
                              			rs.Close();
                              			cn.Close(); 
                              			
                              		}
                              		</script>
                              	</head>
                              	<body>
                              		<input type="button" value="Get count" onclick="getCount()">
                              	</body>
                              </html>
                              Did it help?

                              Comment

                              Working...