我正在尝试用VB.NET和/或.asp编写一个脚本,它将使用NPGSQL数据提供程序连接到我的PostgreSQL数据库。
此函数使用AJAX获取值(selectedFT)并将该值发送到helloWorld.asp脚本。这部分工作正常。
function ajaxyThing (selectedFT) {
var xmlhttp; //CREATE THE VARIABLE TO HOLD THE XMLHTTPRequest OBJEcT
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari THESE BROWSERS SUPPORT THE XMLHTTPRequest OBJECT
xmlhttp=new XMLHttpRequest(); //CREATE THE XMLHTTPRequest OBJECT
}
else
{// code for IE6, IE5 THESE BROWSERS DO NOT SUPPORT THE XMLHTTPRequest OBJECT AND NEED AND ACTIVEXOBJECT
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //CREATE THE ActiveXObject
}
//When using Async = true specify a function to execute when the reponse is ready in the onreadystatechange event
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("responseText").innerHTML = xmlhttp.responseText;
}
}
//TO SEND A REQUEST TO A SERVER, WE USE THE open() AND send() METHODS OF THE XMLHttpRequest object
xmlhttp.open("GET", "helloWorld.asp?q="+ selectedFT, true);
xmlhttp.send();
}
接下来,我需要ASP脚本(或.aspx,或.ashx。我不知道什么是最好的方法)使用selectedFT值来查询我的PostgreSQL数据库。这是我遇到麻烦的地方。我知道我需要做以下事情,但我不知道如何把它们放在一起:
1)从AJAX http请求中获取值。例如,我应该使用:
response.expires=-1
q= request.querystring("q")
2)然后我需要建立与PostgreSQL数据库的连接。我使用以下代码(取自本网站http://www.techrepublic.com/article/easily-integrate-postgresql-with-net/6102826)在标准.aspx页面的PageLoad中运行,它可以很好地将数据绑定到gridview。但我真正需要的是不将结果集连接到gridview,而是让我的连接脚本独立,这样我就可以使用输出做很多不同的事情。我不确定如何在.asp脚本(或.aspx或.ashx)中实现此代码,并在我从AJAX函数调用.asp脚本(或.aspx或.ashx)时运行它。
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
Dim pgConnection As NpgsqlConnection = New NpgsqlConnection
Dim pgCommand As NpgsqlCommand = New NpgsqlCommand
Dim pgConnectionString As String
Dim sda As NpgsqlDataAdapter
Dim ds As DataSet
ds = New DataSet
pgConnectionString = "Server=localhost;Port=5432;Userid=myUserId;Database=myDatabaseName;password=myPassword;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=15;SslMode=Disable"
pgConnection.ConnectionString = pgConnectionString
pgConnection.Open()
pgCommand.Connection = pgConnection
pgCommand.CommandType = CommandType.Text
pgCommand.CommandText = "SELECT * FROM ""myTable"";"
If pgConnection.FullState = ConnectionState.Open Then
MsgBox("Connection To PostGres is open", MsgBoxStyle.MsgBoxSetForeground)
End If
sda = New NpgsqlDataAdapter(pgCommand)
sda.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
pgConnection.Close()
End sub
任何建议都将不胜感激。谢谢!
答案 0 :(得分:1)
经过一番挖掘后,我能够找到解决这个问题的方法。这是我使用NPGSQL数据连接器,ASP.NET(VB.NET),Javascript和AJAX从PostGresql数据库查询的解决方案。在我的应用程序中,名为“ajaxyThing”的AJAX函数从OpenLayers映射中的自定义控件接收值,但您可以将任何值传递给函数。
以下是我的javascript文件中Ajax函数的代码:
function ajaxyThing (selectedFT) {
var xmlhttp; //CREATE THE VARIABLE TO HOLD THE XMLHTTPRequest OBJEcT
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari THESE BROWSERS SUPPORT THE XMLHTTPRequest OBJECT
xmlhttp=new XMLHttpRequest(); //CREATE THE XMLHTTPRequest OBJECT
}
else
{// code for IE6, IE5 THESE BROWSERS DO NOT SUPPORT THE XMLHTTPRequest OBJECT AND NEED AND ACTIVEXOBJECT
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //CREATE THE ActiveXObject
}
//When using Async = true specify a function to execute when the reponse is ready in the onradystatechange event
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("divAttributes").innerHTML = xmlhttp.responseText;
}
}
//TO SEND A REQUEST TO A SERVER, WE USE THE open() AND send() METHODS OF THE XMLHttpRequest object
xmlhttp.open("GET", "Handler.ashx?q="+ selectedFT, true);
xmlhttp.send();
}
然后在Microsoft Visual Web Developer Express 2010中,我创建了一个Web Handler(.ashx)文件,并将我的AJAX函数中的变量传递给它。 我将变量“selectedFT”传递给Visual Studio Web Handler。以下是Web处理程序的代码:
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports Npgsql
Imports System.Data
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.Expires = -1
Dim q = context.Request.QueryString("q")
Dim pgConnection As NpgsqlConnection = New NpgsqlConnection
Dim pgCommand As NpgsqlCommand = New NpgsqlCommand
Dim pgConnectionString As String
Dim sda As NpgsqlDataAdapter
Dim ds As DataSet
ds = New DataSet
pgConnectionString = "Server=localhost;Port=5432;Userid=myPostGresUserId;Database=myDatabaseName;password=myPassword;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoolSize=20;Encoding=UNICODE;Timeout=15;SslMode=Disable"
pgConnection.ConnectionString = pgConnectionString
pgConnection.Open()
pgCommand.Connection = pgConnection
pgCommand.CommandType = CommandType.Text
pgCommand.CommandText = "SELECT * FROM ""myTable"" WHERE ""myValue"" = '" & q & "';"
sda = New NpgsqlDataAdapter(pgCommand)
sda.Fill(ds)
If pgConnection.FullState = ConnectionState.Open Then
context.Response.Write("<table>")
Dim dr As DataRow
Dim dt As DataTable
dt = ds.Tables(0)
For Each dr In dt.Rows
context.Response.Write("<tr><td><b>Column #1 Name:</b></td><td>" & dr.Item(0) & "</td></tr>")
context.Response.Write("<tr><td><b>Column #2 Name:</b></td><td>" & dr.Item(1) & "</td></tr>")
context.Response.Write("<tr><td><b>Column #3 Name:</b></td><td>" & dr.Item(2) & "</td></tr>")
Next
ds.Dispose()
context.Response.Write("</table>")
Else
context.Response.Write("pgConnection did not open successfully.")
End If
pgConnection.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
我不会声称这是解决问题的最佳方案 - 但它确实有效。我愿意接受你可能要做的任何建议,使其更优雅。谢谢!