- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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 中运行,它可以很好地将数据绑定(bind)到 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
如有任何建议,我们将不胜感激。谢谢!
最佳答案
经过一番挖掘,我找到了解决这个问题的方法。这是我使用 NPGSQL 数据连接器、ASP.NET (VB.NET)、Javascript 和 AJAX 从 PostGresql 数据库查询的解决方案。在我的应用程序中,名为“ajaxThing”的 AJAX 函数从 OpenLayers map 中的自定义控件接收值,但您可以将任何值传递给该函数。
这是我的 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 处理程序。以下是网络处理程序的代码:
<%@ 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
我不会声称这是解决问题的最佳方案 - 但它确实有效。我愿意接受您可能提出的任何建议,以使其更加优雅。谢谢!
关于ajax - 使用 VB.NET、AJAX 和 NPGSQL 连接到 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13207000/
如果没有正确设置项目,总会出现这样的错误: 无法加载文件或程序集“Npgsql,版本 = 3.1.2.0,文化 = 中立,PublicKeyToken = 5d8b90d52f46fda7”或其依赖项
我正在尝试将 TransactionScope 与 Npgsql 提供程序一起使用。 我在一个旧问题 ( provider for PostgreSQL in .net with support fo
添加到我的 vs2015 update 3 安装中。到一个全新的控制台项目。 Attempting to gather dependency information for package 'Npgs
我正在尝试将数据从 PostgreSQL 数据库获取到 PowerBI Desktop,但似乎不起作用。我认为问题出在 npgsql 的安装上。当尝试在 PowerBI 中设置新的数据库连接时,我输入
我是 C# 的新手。我在下面的 .UseNpgsql(string connectionString) 上看到了一些红色波浪线(错误下划线)。悬停错误告诉我,我可能没有使用正确的 using 语句。
我正在使用 NLog4.1 版本进行日志记录。 我的“NLog.config”看起来像: PRODUCTION INSERT INTO "MY"."LOG_TABL
我有一个使用 PostgreSQL 并使用 Npgsql 作为数据驱动程序的 ASP.NET 应用程序。我时不时地收到这个错误: Could not load file or assembly 'Np
我正在尝试从 NPGSQL v2.0.11 迁移到 v3.1.7 但是在我的代码中我正在访问 NpgsqlException 的代码属性。在 3.1.7 中,这已经消失了;我现在如何从 postgre
我想将现有的事件实现替换为可以在事件进入时处理事件的实现,如有必要,可以同时处理。不幸的是,我以前从未实现过任何类型的并发,但必须从某个地方开始,嗯? 我一直在阅读 Functional Reacti
当我在我的连接字符串中使用 Pooling=True 时,出现错误“无法在恢复期间执行不监听”。 此错误发生在热备运行的复制/读取服务器上。 最佳答案 这在 3.2.7 版本中仍然存在。应该可以在热备
背景:我正在将我的应用程序从 mssql 移动到 npgsql v2.0.11.92。 运行我的应用程序几分钟或几小时后,我得到一个 System.Exception:从池中获取连接时超时 . 我在
我目前正在开发一个具有 PostgreSQL 后端的 .Net 应用程序。我正在使用 NpgSQL 来连接两者。当我尝试进行简单的数据检索时,NpgSQL 返回以下错误: 42601: syntax
我正在尝试将 Postgres 中的 timestamp[] 字段类型与 NPGSQL 一起使用,这样我就可以在我的 Entity Framework 模型中使用 DateTime[] 类型。 我已将
我使用 PostgreSQL/NpgSQL 将 c# 桌面应用程序连接到 PostgreSQL 数据库。第一个任务是返回我数据库中的所有表名,这工作正常。根据这个结果,我想获取每个表的所有列名。但问题
我们正在为多人游戏开发数据库后端。服务器用 C# 编写,并通过 Npgsql 与 Postgres 数据库对话。 现在,手册展示了如何使用准备好的语句: NpgsqlCommand command =
这可能与我在 FNH 遇到的其他问题有某种联系。 Fluent NHibernate cannot load MySql.Data from GAC in debug mode of a test 通
我无法在程序启动时可靠地检查 NET/Npgsql 中的数据库是否存在。 代码如下: Public Function dbExists(ByVal _dbName As String) As Bool
我编写了一个基本脚本,可将数据从 PostgreSQL 数据库同步到另一个系统,该脚本在我的测试/开发环境中执行时没有问题,使用: PostgreSQL 9.4 Npgsql dll 版本 3.0.3
这是手册中给出的示例。当我尝试同样的操作时,我得到了无数的异常(exception)。这有什么错误。 using(NpgsqlConnection conn = new NpgsqlConnectio
我正在尝试创建一个包含文件路径的记录。使用 NpqSQL 驱动程序插入启用 UTF8 的 Postgres 数据库。 我的表格定义: CREATE TABLE images ( id seri
我是一名优秀的程序员,十分优秀!