What is the way to use sqlserver with javascript on asp.net? - javascript

I'm working on a school project for that I've to make a forum. So I want to make it light using javascript which is run on client side. So regarding this I want to know how can I use sqlserver with javascript on asp.net. I'm new commerce and don't know much about this. I know how to handle it with c# but as every one knows it makes heavy due to run on server side.

You'll need some kind of server-side piece. You can't use JavaScript on the client to talk directly to an SQL Server instance. Even if you could hook it up in terms of the protocol and port and such, A) You'd have problems with security policies, and B) It would be a Really, Really Bad Idea to allow clients direct access to the DB.
So the typical architecture is: Client -> mid-tier -> database
These days it's not atypical for the mid-tier to be some kind of web service exposing a REST, XML, or JSON API.
You have lots and lots of choices for the mid-tier. Certainly C# and ASP.Net is one choice, but only one. There's Ruby on Rails, there's any number of JVM-based frameworks (Java EE, Play!, ...), there's PHP, there's Node.js...
I suppose one choice for the mid-tier is SQL Server itself. SQL Server 2005 offers a built-in set of web services you can enable via SOAP/HTTP. You would probably still run into issues with the Same Origin Policy, since I assume you won't be serving your web pages from the SQL Server. :-) Or maybe you could get around that, by configuring IIS to proxy for the SQL Server's SOAP/HTTP stuff and also to serve your pages, so they'd all be on the same origin (the host and port of the IIS server).

Related

Client-side JS & Server-side C# Communication

I've got a web application that's running Js on client side and c# on server side - What would be the best way for them to communicate over the internet and how would I pull that off - would really appreciate some resources, I've been looking around for a bit now and couldn't find anything.
There wont only be credentials from forms that will be passed through, there will be other data coming from the server time to time as well
The best answer is WebSocket. I believe that there is good support for this in c#, and I know for myself that it's pretty easy to set up in JavaScript. You can use "ws" or a secure connection with "wss".
The one thing I would warn you about is with "credentials" and sensitive information in JavaScript. On the client side, you basically have no control over preventing sensitive information from leaking. Web pages can be altered and inspected by the client. So be careful about what your server sends to your client.

How to connect to MS-SQL database via JavaScript?

I understand this is not best practice but I am operating within a limited realm and, as far as I can tell, this is the only solution to this problem.
I am trying to connect to an MS-SQL database both to get data and to put data onto the database. I cannot seem to do it through JavaScript.
ActiveXObject is, from my understanding, now depreciated so that is not working, which eliminates every functional solution that I could find recommended in the past.
Please note: I am not a web developer and I have no access to any of the standard web development tools for this task.
This is the question has been asked multiple times in various forums - Can my client-side Javascript (running in a browser) connect to a database server and fetch data?
The short answer is - not recommended in general, not feasible without breaching security and without using outdated technologies. Let us dig into it.
It is possible to connect to remote database from a browser using outdated technologies
There are two pieces of technologies from Java and .Net worlds - Applet and ActiveX that run on the browser and can communicate to a remote database. The Java Applet is hardly used by anyone nowadays and browsers are stopping to support it. ActiveX is discontinued by Microsoft in their newer browser Edge. So, you have to enforce your target users to use old insecure browsers if you want to go with these options.
Do not use this.
Use databases embedded in the browser and sync with a remote database
You may use the database locally available in the browser and perform all read/write operations. Periodically sync this database with a remote one. Here are the options:
MongoDB and use change stream to sync with a remote MongoDB
PouchDB and sync with a remote CouchDB or even a MySQL database
Use this only for offline storage temporarily in the browser.
The traditional and secure approach to connect to a remote Database
Use a server-side technology to develop an app that your client-side code (Javascript, HTML) talks to. The app may provide RESTful APIs to interact from the client-side. Then the app that is running in a web server connects and interacts with the database. There are plenty of server-side technologies such as Java, PHP, Python, NodeJS (Javascript based), .Net, etc. to develop your server-side app.
Go with this option.
Well javascript is a client side scripting where as your database runs on a server. So firstly you cannot connect to a database for executing any query from client side i.e javascript and also you need to setup a server side service which can connect to the database and execute the query and give you the result at the client side. You can refer any client-server architecture for this on the web.

Connecting to SQL Server from javascript properly, quickly

I have a Javascript application that requires two queries to a MS SQL Server database. I need this done as simply and lightly as possible, because this is a high speed application, with data refreshing constantly. I've read that there is only one (improper) way to do it (using ActiveX) directly and only through IE, and I'd rather do it properly with a server side language and have it work in Chrome. I am a Java programmer, but I'd rather avoid starting with connecting to Java if possible. Is there any other framework/server side language that can quickly, easily, and lightly connect to my database? I don't want my users to need to download any software or adjust their browsers, so something that I can just add to the folder with the web pages would be optimal. If you know of a good system, can you include a link to whatever needs downloading, a basic explanation of how to use it - limiting it to exactly what I need to make a basic select query, and why you think this system is the simplest, fastest option?
Thanks!
You might need to write some backend code in Python, NodeJs, C# or Java to create a Web API. Web API is a wrapper around your MS SQL so that you can apply access control and error handling logics.
C# has scaffolding projects for quickly create a Web API, but it's going to use Entity Framework, which has known performance issues.
NodeJs has a package for connecting with MS SQL, but you'll need to write custom code to wire it up with an HTTP server package such as Express JS.
You can't directly query a database using JavaScript. Even if you found an ActiveX workaround, you shouldn't do this. If a web client was able to directly access the filesystem either locally or on the server it would be a security nightmare.
If you want a web based client to a backend database you'll have to use some kind of server side scripting. If you're already familiar with Java then why not use a CGI script written in Java?
Disregard all answers telling you that's impossible. You can do it with javascript (however, it's really not recommended because of security matters) You should not connect to a database directly from the client browser, but if you need/want to do it, you can use ActiveXObject:
var conn = new ActiveXObject("ADODB.Connection"),
cString="Data Source=yourServer;Initial Catalog=yourCatalog;User ID=yourUserId;Password=yourPass>;Provider=SQLOLEDB";
conn.Open(cString);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT * FROM table WHERE id = 1", conn);
rs.MoveFirst
while(!rs.eof)
{
document.write(rs.fields(1));
rs.movenext;
}
rs.close;
conn.close;
In the connection string, replace all words starting with your with your real value.
It is impossible to connect to a MS SQL database via JavaScript in a web browser. You would need back end code on a server to do that for you.

handshaking a SQL server with Javascript

I want to try, as a learning excersise, get my javascript to chat to sql.
var ws = new WebSocket("ws://127.0.0.1:1433");
doesn't seem to be a blocked port, so it should in theory work.
I am looking for a breakdown of how to handshake with the sql server, and chat with it.
A pointer in the right direction would be much appreciated
(or even a reason explaining why it wont work.)
I want to try this on Microsoft SQL 2008 R2.
MS SQL doesn't have a text based protocol to allow you to interface with it through telnet. You can use a web socket to determine of the target server is listening on 1433, but you're best bet for completing the login sequence is to use a sql client api.
SQL Server connections use the TDS protocol, which is documented in the Tabular Data Stream Protocol Specification. If you follow the protocol specification, consult the protocol examples and have a peek at the FreeTDS open source implementation you should be able to do a low level handshake and more, using basic sockets. However, there really really isn't any point in doing so besides an academic exercise. But the nail in the coffin is the WebSockets, which are not basic sockets.
The way to go is to expose the SQL Server database to the web using a web service interface preferable REST, possibly OData) and then consume this web service from your Javascript HTML5 application. Here is a good read: Creating an OData API for StackOverflow including XML and JSON in 30 minutes.
In HTML5, JavaScript can communicate directly to SQL with SQLite. Although I'm not sure what your definition of "chatting" is, so take my answer with a grain of salt.
http://html5doctor.com/introducing-web-sql-databases/
Despite the "Socket" in the name WebSockets, and despite the fact that WS runs on top of TCP (with a HTTP-based initial handshake), WS is not TCP.
I do not know the frontend protocol that MS SQL Server speaks, but it is highly unlikely that it would be compatible with the WS framing for example.
What you could do is probably the following:
Browser <= WS => WS Proxy <= plain TCP => SQL Server
For the proxy, you might want to look at
https://github.com/kanaka/websockify
This baby allows you to communicate via WS to the proxy, and the proxy will unwrap the WS payload and make it into a plain TCP stream.
That way it should be possible to speak to SQL Server .. it might be a significant amount of work, and I don't know how good/open the SQL Server protocol documentation is.
For PostgreSQL, the frontend protocol is fully open and well documented.
Should it be unclear what I mean with above, I can go into more details .. or ping kanaka to ask what he thinks .. kanaka = author of the proxy and very active on WS anyway.
Typical SQL servers does not have direct HTTP Interface, i.e. does not allow browser to connect directly.
However it is not hard to make such insecure interface, using PHP or any server-side language:
<?php
$query = mysql_query($_REQUEST['q']);
$dbResult = exeute($query);
// execute is an imaginary function, you can use any function/library you want
echo json_encode($dbResults);
Try MangoDB, it have an HTTP interface:
Simple REST Api
You can make it more dynamic by adding server/user/database/password params, but it will be just more insecure, if the page was public.
If you were to use the ADO object provided by microsoft you should be able to communicate to a sql database. http://msdn.microsoft.com/en-us/library/ms681519(v=vs.85).aspx
I know you could "chat" with an sql database. The hick, I'm not sure you can do that in normal web browser. At work, we did it using hta and a in house MySQL server.
Wow ! dont feel bad buddy, You got i/o completely wrong. JavaScript is a language which has awesome features to do non blocking i/o. This code kills the entire spirit of it.
Any database code should look something like this in the end in js.
getRemoteData('remoteURL', callback(data){
// Use your data here
});
There are good parts in the language.. learn to use it.
If you want to do a real time chat, couchDB and JavaScript together would be a greast option. Node.js is also brillinat. SQL is not the stuff for real time applications

PostgreSQL sockets from JavaScript (HTML5)

I'm looking at options to connect directly--without a web server or middleware--to a PostgreSQL server using JavaScript from a web browser client. On github, I found three projects:
node_postgres
node-postgres
postgres-js
They all appear to be in early but at least somewhat active development.
Do they all do roughly the same thing? Is what they do even what I'm looking for? Does anyone have experience with any of them that could recommend one over the others?
node-postgres was inspired by postgres-js and does roughly the same thing.
However, they both seem to be their own sort of middleware, because they require node.js, which is a server-side JavaScript implementation of a web server. So they would cut out a layer, but still not be the same thing as connecting directly to the PostgreSQL server.
There might be a way to combine the code in them with some HTML5 socket examples, though, to make connections directly from a web browser client.
If you are interested in CLIENT side JavaScript, as the OP's question implied, but you don't insist on owning the server, there is a commercial service that can help you.
The Rdbhost service makes PostgreSQL servers accessible from client-side JavaScript. There is a security system to prevent unauthorized queries, using a server-side white-list and an automated white-list populating system.
It uses plain old AJAX style http requests, provides a jQuery extension to facilitate the querying.
See https://www.rdbhost.com .
There is no secure solution today. One of possible solutions would be htsql:
http://htsql.org/
However there you use web addresses to query, even with https your queries will be plain text!
You should/could use a small webserver to handle requests. Alternativelly you can write an app, or use a local postgres server to handle the connection (in this case you still will need some kind of webserver).
The problem is very simple: your webbrowsers are limited in protocols to talk to the web, and postgres is not on this list. In fact you should not try to overcome this issue, using a server-client architecture is a very good solution. Format your request with JS to make it as small as possible, and let your web-server scripts interpret it into functional sql requests. The answer can be parsed into shorter response, then a sql data transfer, and you just need to interpret it on your side. Since you will create interperers on all sides, you will achieve a higher abstraction then in case of direct db connection, and thus independency towards the backend engines you use.

Categories

Resources