我需要使用Javascript从SQl Server 2008数据库中读取一些数据。 所以我写了这个:(html页面代码)
<!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>
然而,没有任何反应! 这有什么问题?连接字符串中有一些西里尔字母符号,它可能是问题的根源吗?还是另一件事是错的?
答案 0 :(得分:2)
这是IE唯一的代码,即使在IE中您也必须明确允许这样的事情,请在此处查看已接受的答案:
ActiveXObject in IE8
答案 1 :(得分:1)
您的connectionstring
未正确转义,应为:
var connectionstring="Data Source=ИЛЬЯ-ПК;Initial Catalog=C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\SIGMA_Database.mdf;User ID=Илья;Password=\"\";Provider=SQLOLEDB";
答案 2 :(得分:0)
你试过吗
s.Open("SELECT Username FROM Users", connection);
rs.MoveFirst();
while(!rs.EOF)
{
document.write(rs.Fields.Item(1));
rs.MoveNext();
}
rs.Close();
connection.Close();
编辑rs.Fields
答案 3 :(得分:0)
您的代码的问题是您使用物理驱动器路径C:\ Program Files \ Microsoft SQL Server \ MSSQL10.MSSQLSERVER \ MSSQL \ DATA \ SIGMA_Database.mdf;
访问SQL Server数据库使用以下代码连接到SQL Server
var strconnectionstring =“Data Source = your_server_name; Initial Catalog = your_database_name; User ID = database_user_id; Password = database_password; Provider = SQLOLEDB”;