Click here to Skip to main content
15,039,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i am trying to retrieving data from sql database in html with javascript.
please any body help to me..........
i know not possible in only javascript. but will have some way!
now i written like this:

C#
var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close;



thanks in advance.
Posted
Updated 18-Jul-12 3:43am
v2
Comments
StianSandberg 18-Jul-12 8:16am
   
You can't access your database using pure javascript client side. You have to use json or similar.
What kind of technology are you using? ASP, ASP.NET, PHP?
Unareshraju 18-Jul-12 8:36am
   
Thank u Somuch Allu,
Actually i am using phonegap template for that using HTML and Javascript only.
here iam trying with only javascript and html. please any suggestion .

Does the samples at the following link of help: Javascript: ADODB & SQL Queries[^]

A sample from the link:
RETRIEVE DATA FROM SQL DATABASE
Example simple query: query="SELECT x,y FROM blah-blah";
JavaScript
function GetDatabaseData(){ //saves all rows of desired DataBase tables in sqlArr
  var adOpenDynamic=2;
  var adLockOptimistic=3;
  sqlArr=new Array();
  try{
    var rs = new ActiveXObject("ADODB.Recordset");
    rs.open(query, conn, adOpenDynamic, adLockOptimistic);
    var j=0;var tArr=[];
    if(!rs.bof){rs.MoveFirst();}
    for(var i=0;i<rs.fields.count;++i){tarr[i]>    sqlArr[j]=tArr;j++;
    if(!rs.eof){rs.MoveNext();}
    while(!rs.eof){
      tArr=[];
      for(var i=0;i<rs.fields.count;++i){>
        tArr[i]=rs.fields(i).value;
        if(tArr[i]!=tArr[i]+''){tArr[i]='<object>';}
        if(tArr[i]=='null'){tArr[i]='-';}
      }
      sqlArr[j]=tArr;j++;
      rs.MoveNext();
    }
    rs.close();//record set
  }
  catch(err01){
    conn.close();
    conn=null;// leave DB
    Some error handling...
  }
}
   
Comments
Unareshraju 19-Jul-12 2:26am
   
Thank u sandeep
Sandeep Mewara 19-Jul-12 2:33am
   
Welcome.
Unareshraju 20-Jul-12 4:32am
   
sandeep actually here didn't connect to the database.any suggestion?
Sandeep Mewara 21-Jul-12 12:29pm
   
Did you read this line;
rs.open(query, conn, adOpenDynamic, adLockOptimistic);

//conn: connecion string.
hbghkbjhkljk,.jmkm,.m,mjbghmn m,n,nmn,mnm,nm,hjbvhj vbnv
   
hibbhjbgffgdsxvcvvbchgfghfhgfhfghfhgfghfghgfgfhgghjkhjnm,nmbhgfghjknkm,.m.,m.;kmhyftrdtrsdgvchbvjh
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900