JavaScript connection to sql server database instance + explanation-connection test

This example refers to the content of this blogger:
https://www.cnblogs.com/carekee/articles/5829656.html

Because I will not debug directly in the console, I added the code to connect to the database into the HTML.
details as follows:

<!DOCTYPE html>
<head>
    <title>测试js访问数据库</title>
</head>
<script language="javascript">
    function testdb() {
        var objdbConn = new ActiveXObject("ADODB.Connection");    //见解释①
        var strdsn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=liushiqiang;Initial Catalog=Sale;Data Source=LIUSHIQIANG00";  //见解释②  
        objdbConn.Open(strdsn);       
        document.write("访问成功");      
        objdbConn.Close();                    
    }
</script>
<body onload="InitDB()">
    This is my page.
    <br>
    <input type="button" value="submit" onclick="testdb()" />
</body>
</html>

①: ActiveXObjec("ADODB.Connection")
activeXObject object to realize the browser's reading and writing of local files, while other browsers use XMLHttpRequest objects.
ADO Connection object is used to create an open to a certain data source connection. Through this connection, you can access and operate a database.
For more attributes, please go to: http://www.w3school.com.cn/ado/ado_ref_connection.asp

PS: About the extended application of ActiveXObjec:

//	使用activeXObject判断IE浏览器
    function isIE() {
        if (!!window.ActiveXObject || "ActiveXObject" in window)  
        return true;  
    else  
        return false;  
}
//为什么要加两个"!!",是为将其转换为布尔变量,
//"ActiveXObject" in window是为了兼容IE11,IE11不支持window.ActiveXObject,
//会提示undefined

Please refer to the original question: https://ask.csdn.net/questions/238503

②: Here is a little trick to configure the sql server database path
. Create a text file on the desktop, change the suffix to ".udl", open it again, you can see a graphical SQL configuration interface, configure according to the interface prompts OK, after the test connection is successful, open it with Notepad, copy the configuration information inside and it will be OK.
Insert picture description here
When I entered the information for logging in to the server, I entered the user name and password and couldn't connect, so I chose the first one.
Then open it with Notepad and copy the information in the red box. Insert picture description here
Remember! ! ! ! !
Whether HTML files with ActiveXObject() objects or JS files need to be opened with IE browser, an error will be reported. Also allow ActiveX controls to run.Insert picture description here

Guess you like

Origin blog.csdn.net/zhuyin6553/article/details/91976545