2

NPGSQLデータプロバイダーを使用してPostgreSQLデータベースに接続するスクリプトをVB.NETや.aspで作成しようとしています。

この関数は、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();
}

次に、selectedFT値を使用してPostgreSQLデータベースにクエリを実行するために、ASPスクリプト(または.aspx、または.ashx。最適な方法がわかりません)が必要です。これは私が問題にぶつかるところです。私は次のことをする必要があることを知っていますが、それをすべてまとめる方法がわかりません。

1)AJAXhttpリクエストから値を取得します。たとえば、私はおそらく使用する必要があります:

response.expires=-1
q= request.querystring("q")

2)次に、PostgreSQLデータベースへの接続を確立する必要があります。次のコード(このWebサイトhttp://www.techrepublic.com/article/easily-integrate-postgresql-with-net/6102826から取得)を使用して、標準の.aspxページのPageLoadで実行すると、正常に動作します。データをグリッドビューにバインドします。しかし、私が本当に必要としているのは、結果セットをグリッドビューに接続するのではなく、接続スクリプトをスタンドアロンにして、出力を使用してさまざまなことを実行できるようにすることです。このコードを.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

任意の提案をいただければ幸いです。ありがとう!

4

1 に答える 1

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 ハンドラー (.ashx) ファイルを作成し、AJAX 関数から変数を渡しました。変数 "selectedFT" を Visual Studio Web ハンドラーに渡します。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

これが問題の最善の解決策であるとは言いませんが、うまくいきます。これをよりエレガントにするために必要な提案があれば、私は喜んで受け入れます。ありがとう!

于 2012-11-05T04:25:58.393 に答える