How to connect to SQL Server database from JavaScript in the browser?

前端 未结 8 2070
萌比男神i
萌比男神i 2020-11-22 00:56

Can anybody give me some sample source code showing how to connect to a SQL Server 2005 database from JavaScript locally? I am learning web programming on my desktop.

<
8条回答
  •  一生所求
    2020-11-22 01:34

    You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:

    var connection = new ActiveXObject("ADODB.Connection") ;
    
    var connectionstring="Data Source=;Initial Catalog=;User ID=;Password=;Provider=SQLOLEDB";
    
    connection.Open(connectionstring);
    var rs = new ActiveXObject("ADODB.Recordset");
    
    rs.Open("SELECT * FROM table", connection);
    rs.MoveFirst
    while(!rs.eof)
    {
       document.write(rs.fields(1));
       rs.movenext;
    }
    
    rs.close;
    connection.close; 
    

    A better way to connect to a sql server would be to use some server side language like PHP, Java, .NET, among others. Client javascript should be used only for the interfaces.

    And there are rumors of an ancient legend about the existence of server javascript, but this is another story. ;)

提交回复
热议问题