如何使用javascript从azure SQL数据库中检索数据 [英] How to retrieve data from azure SQL database using javascript

查看:68
本文介绍了如何使用javascript从azure SQL数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
Inherits="APIToDatabase.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
 <<head>
  <title></title>
  <script type="text/javascript">
  function connectDb()
  {
  var ConnDB = new ActiveXObject("ADODB.Connection");
  ConnDB.ConnectionString="Driver={ODBC Driver 13 for SQL Server};Server=tcp:yueloongserver.database.windows.net,1433;Database=LocationDatabase;Uid=;Pwd=;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;";
 ConnDB.Open();
 var Rs = new ActiveXObject("ADODB.Recordset");
  var tempstr = "";
 Rs.Open("select Latitude,Longitude from dbo.GPS",ConnDB);
 while(!Rs.EOF === true)
 {
  tempstr = tempstr + Rs("Latitude")  + "" + Rs("Longitude");


 Rs.MoveNext;
}
  test.innerHTML = tempstr;
}
  </script>
  </head>
  <body>


  <p><div id=test> </div>
  <p><input id="button1" type="button"   value="Run"   name="button1" onclick="connectDb();"></p>


  </body>
     </script>  







Hi, i am having the problem where i am able to only retrieve one column from the database. However, i would like to retrieve all the columns from the database table.





我尝试过:



我尝试了很多方法,例如更改变量和阅读代码以了解并找出问题



What I have tried:

I have tried many ways such as change the variables and reading through the code to understand and figure out the problem

推荐答案

ActiveX也是一个巨大的安全风险,默认情况下通常在IE中禁用,其他主流浏览器不支持。现在你正在向世界展示你的连接信息,所以每个人都知道你的数据库密码。



你实际插入的代码是vbscript ,并且需要更改脚本标记以反映这一点;但是,由于ActiveX问题,我不建议采用这种方式。

实际插入的代码与经典ASP的编写方式非常相似;然而,这将在服务器上被解释。



你有什么用的,这可以转换为ASP.NET并放在代码隐藏页面,只有很小的语法更改。如果您选择这条路线,它会将脚本的执行移动到服务器,并在请求该页面时完成。



可能是最好的事情将转到WebForms的文档以及如何访问数据库。

良好的起点:直接从ASP.NET页面访问数据库Microsoft Docs [ ^ ]
ActiveX is also a huge security risk and is generally disabled by default within IE, and is not supported by other mainstream browsers. As it is now you are exposing your connection information to the world so your DB password is known by everyone.

The code you have actually inserted is "vbscript", and the script tag would need to be changed to reflect that; HOWEVER, I do not recommend going that way due to the ActiveX issues.
The code you have actually inserted is very similar to how Classic ASP would be written; however, that would have been "interpreted" on the server.

What you do have going for you is that this could be converted to ASP.NET and be placed on the code-behind page, with only minor changes in the syntax. If you would choose to go this route it would move the execution of the script to the server and be done when that page is requested.

Probably the best thing to do would go to the documention for WebForms and how to access the database.
Good starting point: Accessing the Database Directly from an ASP.NET Page | Microsoft Docs[^]


要从表中检索所有列,您需要做的就是替换
To retrieve all of the columns from a table all you need to do is replace
"select Latitude,Longitude from dbo.GPS"

"select * from dbo.GPS"

但实际上并非如此一个好的做法。最好按名称列出以逗号分隔的列...顺便说一下,您实际上是从表中检索两个列而不是您所说的那个。



您还可以按编号引用Rs中的字段,而不仅仅是名称 - 请参阅 Fields Collection | Microsoft Docs [ ^ ]

But that is actually not a good practice. It is better to list the columns by name separated by commas ... by the way, you are actually retrieving two columns from the table not one as you state.

You can also refer to the fields in Rs by number not just by name - see The Fields Collection | Microsoft Docs[^]


这篇关于如何使用javascript从azure SQL数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆