.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Steve Jackman
Imran Ghani
Santhakumar Munuswamy

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Connect to a data base and list one table values using javascript and ActiveXObject

Posted By:Kemal AL GAZZAH       Posted Date: June 03, 2016    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

Here a code sample which connects to a sql server data base and lists the content of table using client side javascript and ActiveXObject
 

<html>
<head>
<style>

.data
{
font-size:8px;
}
</style>

<script>
/*Kamel Gazzah */
function getdata()
{

try
{
var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=[ipaddress];Initial Catalog=[table];User ID=testing;password=[password];Provider=SQLOLEDB";

connection.Open(connectionstring);


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

/*rs.Open("SELECT name FROM sysobjects where xtype='u'", connection);
rs.MoveFirst;
if(!rs.eof)
{
alert('ok');
document.write("<select id='ltables'>");
while(!rs.eof)
{
    for(var i=0; i< rs.fields.count-1;i++)
{
    document.write("<option value="+rs.fields(i)+">"+rs.fields(i)+"</option>");
}
   
   rs.movenext;
}

rs.close;
connection.close;
document.write("</select>");
}*/


rs.Open("SELECT  * FROM parameterFECTOperationParameter", connection);



/*rs.Open("
create table #mytable (tab varchar(200),n int)
set nocount on
declare @tab as varchar(200)
declare @script as varchar(max)
declare c cursor for select name from sysobjects where xtype='u'
declare @n as int
open c
fetch c into @tab
while @@fetch_status=0
begin
set @script='insert into #mytable(tab,n) select '''+@tab+''',COUNT(1) from '+@tab;
exec(@script)
fetch c into @tab
end
close c
deallocate c
set nocount off
select * from #mytable order by N desc;
drop table #mytable",connection);*/






rs.MoveFirst;

document.write("<Table width='100%' id='mytable'>") ;
if(!rs.eof)
{
document.write('<tr>') ;
for(var i=0; i< rs.fields.count-1;i++)
{
document.write('<th bgcolor="silver">'+rs.fields(i).name+'</th>');
}
document.write('</tr>') ;

}




while(!rs.eof)
{
document.write('<tr>') 
    for(var i=0; i< rs.fields.count-1;i++)
{
   document.write("<td><span class='data'>"+rs.fields(i)+"</span></td>");
}
   document.write('</tr>') 
   rs.movenext;
}

rs.close;
connection.close;
document.write('<\table>') 
}

catch(err) {
    alert(err.message);
}
}
</script>

</head>
<body onload ="getdata()">
</body>
</html>


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend