SQL Server返回空记录集ADODB.Connection

时间:2012-01-12 11:15:19

标签: sql-server asp-classic ado adodb

我在IIS上运行Classic ASP,并使用以下代码创建与SQL Server数据库的连接的脚本:

Dim adoConn As Object
adoConn = Server.CreateObject("ADODB.Connection")
adoConn.Open ("Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=Engineering_Test; User ID=*********; Password=*******")

然后,我按如下方式从此连接运行SQL select查询:

Dim rs As Object
rs = Server.CreateObject("ADODB.Recordset")

SQL = "SELECT [User ID] FROM [Users] WHERE [Username]='" & username & "' AND [Password]='" & password & "'"
rs.Open(SQL, adoConn, 3, 3)

一切正常,但后来发现我在服务器上有很多睡眠过程,它开始拒绝连接,因为我可能已经将所有允许的连接最大化到服务器。实际上,我在屏幕上看到的是我的SQL查询返回空记录集,实际上它们应该包含多行。

所以我杀死了服务器上的所有休眠进程,重新启动了SQL Server和IIS,但是,我正在执行的SQL查询仍然返回空记录集,没有显示错误,所有内容都按预期编译。

当我使用远程桌面登录服务器并在SQL Server Management Studio中执行完全相同的查询(通过相同的用户凭据访问)时,查询将返回完整的记录集。

还有什么我可以做/检查来解决这个问题吗?这真让我感到困惑!

3 个答案:

答案 0 :(得分:1)

在打开记录集

之前,尝试将此行添加到代码中
rs.CursorLocation = adUseClient

OR

rs.CursorLocation = 3

发布,使用rs.RecordCount检查返回的记录数。

答案 1 :(得分:0)

尝试这样做:

dim adoConn, dsn
set adoConn = server.createobject("adodb.connection")
dsn = "Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=Engineering_Test; User ID=*********; Password=*******"
conn.connectiontimeout = 300
conn.commandtimeout = 300
response.buffer = true

if conn.state = 0 then
  conn.open dsn
end if

sql = "SELECT [User ID] FROM [Users] WHERE [Username]='" & username & "' AND [Password]='" & password & "'"
set rs = conn.execute(sql)

if rs.eof then
  response.write("No user matches the criteries.")
else
  id = rs("User ID")

  response.write("The user's ID: " & id)
end if
如果您需要稍后从数据库中提取大量行,

response.buffer将为您提供帮助,您可以使用response.flush将缓冲区写入用户

答案 2 :(得分:0)

确保在使用后关闭连接,我没有看到代码中的紧密连接或任何对它的引用。如果你每天只打一次数据库一次,你会没事的,但是如果你不断地打到数据库那么你的连接限制超出了你的数量。如果你没有看到有点奇怪的错误,但你可能会在某处压制它们。

Dim rs As Object
rs = Server.CreateObject("ADODB.Recordset")

SQL = "SELECT [User ID] FROM [Users] WHERE [Username]='" & username & "' AND [Password]='" & password & "'"
rs.Open(SQL, adoConn, 3, 3)

...  DO SOMETHING ...

rs.Close '<-- Close the current connection when done using it.
相关问题