Javascript 使用Javascript连接SQL Server数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5622242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:06:34  来源:igfitidea点击:

Using Javascript to connect SQL Server database

javascriptsql-serverdatabaseconnection

提问by Ilya Blokh

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

我需要使用 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>

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?

然而,什么也没有发生!有什么问题吗?连接字符串中有一些西里尔字母符号,这可能是问题的根源吗?还是其他地方出了问题?

回答by Shadow Wizard is Ear For You

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

这是仅限 IE 的代码,即使在 IE 中,您也必须明确允许此类操作,请在此处查看已接受的答案:
IE8 中的 ActiveXObject

回答by tcooc

Your connectionstringis not escaped properly, it should be:

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";

回答by James Kyburz

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

编辑 rs.Fields

回答by olasunkanmi oduwole

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;

您的代码的问题在于您正在使用物理驱动器路径 C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\SIGMA_Database.mdf 访问 SQL Server 数据库;

Connect to SQL Server using the following codes

使用以下代码连接到 SQL Server

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

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