2

I need to use Javascript to read some data from SQl Server 2008 Database. So I wrote this:(html page code)

<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Database Connect</title>
<script type="text/javascript">
function loadDB(){
var connection = new ActiveXObject("ADODB.Connection");

var connectionstring="Data Source=ИЛЬЯ-ПК;Initial Catalog=C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\SIGMA_Database.mdf;User ID=Илья;Password="";Provider=SQLOLEDB";

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

rs.Open("SELECT Username FROM Users", connection);
rs.MoveFirst();
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.MoveNext();
}

rs.close();
connection.close();

}
</script>
</head>
<body onload="loadDB()">
<div id="main"></div>
</body>
</html>

However, nothing happens! What is wrong with it?There're some cyrillic alphabet symbols in connection string, can it be a source of problem?Or another thing is going wrong?

4
  • What browser are you running this in? Can you view the javascript errors? Commented Apr 11, 2011 at 13:48
  • What environment are you running this in? Web server? Local file? mht? Commented Apr 11, 2011 at 13:51
  • I seriously hope your security will not allow curious onlookers from accessing this file, and that this is an "interior only" file. Commented Apr 11, 2011 at 13:59
  • I use Google Chrome; Everything is local; Security doesn't matter now, I know that javascript isn't the best way to connect to database; Commented Apr 11, 2011 at 14:45

4 Answers 4

2

This is IE only code, and even in IE you have to explicitly allow such thing, see accepted answer here:
ActiveXObject in IE8

Sign up to request clarification or add additional context in comments.

7 Comments

However I use Chrome and Firefox, how to connect to database using javascript if I have these browsers?
@Ilya you can use window.openDatabase just Google it, the first result is: creativepark.net/blog/entry/id/1191
Yes, it is really good!The only thing that I didn't understand is where the database must be situated?I should write a full path in first parametre of window.openDatabase()?
@Ilya you don't give the path, the database is internal to the browser. If you need to work with existing database you will have to use server side language like PHP or ASP using web server software like IIS or Apache.
Another option if the code is to run locally only, is write .vbs file and have the user execute it directly. In there, you can use plain vbscript code.
|
1

Your connectionstring is not escaped properly, it should be:

var connectionstring="Data Source=ИЛЬЯ-ПК;Initial Catalog=C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\SIGMA_Database.mdf;User ID=Илья;Password=\"\";Provider=SQLOLEDB";

Comments

0

Have you tried

s.Open("SELECT Username FROM Users", connection);
rs.MoveFirst();
while(!rs.EOF)
{
   document.write(rs.Fields.Item(1));
   rs.MoveNext();
}

rs.Close();
connection.Close();

Edited rs.Fields

1 Comment

If all else fails have you tried running IE Developer Toolbar microsoft.com/downloads/en/…
0

The problem with your codes is that you are accessing the SQL Server database using physical drive path C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\SIGMA_Database.mdf;

Connect to SQL Server using the following codes

var strconnectionstring = "Data Source=your_server_name;Initial Catalog=your_database_name;User ID=database_user_id;Password=database_password;Provider=SQLOLEDB";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.