如何在Asp.Net中使用Jquery从数据库读取数据

问题描述:

我只是在sql server中创建数据库并将数据插入表中。我想在gridview格式中使用jquery在网页中显示数据

i am just creating database in sql server and insert data into the table. i want to display the data in webpage using jquery in gridview format

您可以将ajax请求发送到将通过jQuery生成的服务器。您不需要在请求中附加任何其他标头,但您需要获取您尝试获取的网页。



例如,这是从数据库获取数据的服务器端代码,



You can send ajax request to the server that will be made through jQuery. You donot need to append any additional headers with the request but the web page you're trying to get would be required.

For instance, this is your server-side code to get the data from the database,

var db = Database.Open("database_name");
var selectQuery = "SELECT * FROM table_name WHERE column_name = value";
var result = db.Query(selectQuery);

// now generate the GridView for the result, and return the HTML
// generated from the above code.





..上面的代码会返回数据库表格的GridView的HTML,通常是你想要在网页上显示的结果。您也可以使用任何其他方法(SqlClient方法)从数据库中读取数据。这只是一个例子。



创建一个jQuery ajax请求来捕获这些数据,因为这样,





.. above code would return the HTML for the GridView of your database's table, usually that is the result that you want to show on the web page. You can read the data from database in any other method (SqlClient method) too. This was just an example.

Create a jQuery ajax request to capture that data, as this,


document )。ready( function (){
(document).ready(function () {


.ajax({
url:' yourWebPage'
成功: function (数据){
// 已发送请求,并且已成功
// 服务器的结果保存在'data'
.ajax({ url: 'yourWebPage', success: function (data) { // a request was sent, and was successfull // the result from server is saved in 'data'