我怎样才能 运行 SQL 服务器使用 VB 作为 ASPX?
How can I run SQL Server using VB as a ASPX?
我目前正在使用 Classic ASP,以便使用以下代码连接到 SQL Server 2008 R2 数据库:
dim Example_DatabaseName
Example_DatabaseName = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
Set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.Open mySQL1, Example_DatabaseName
Do While Not rs1.eof
response.write rs1("NameOfPerson") & "<br>"
rs1.MoveNext
Loop
rs1.close
set rs1 = Nothing
此代码不适用于 ASPX,但它在本地主机 IIS 上与经典 ASP 一起工作得很好。我在网上研究过我遇到了各种不同的错误,这让我更加困惑...
我想请教您 help/advice 如何使用 VB 脚本为 ASPX 执行此操作 - 但是,我对 ASP.net 不是很熟悉 -我只是想自学如何使用它。
我收到错误消息:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30002: Type 'SqlConnection' is not defined.
以上错误消息适用于:
Using Con As New SqlConnection(sConnection)
直接从这里改编Using SQLDataReader instead of recordset
Dim sConnection As String = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
Using Con As New SqlConnection(sConnection)
Con.Open()
Using Com As New SqlCommand("Select NameOfPerson From Person", Con)
Using RDR = Com.ExecuteReader()
If RDR.HasRows Then
Do While RDR.Read
Response.Write( RDR.Item("NameOfPerson ").ToString())
Loop
End If
End Using
End Using
Con.Close()
End Using
您不能在 aspx (asp.net) 文件中使用 VBScript 作为脚本语言。您可以使用 VB.net,这是一种类似的语言,但绝不相同。
如果您想在 asp.net 页面中使用 COM 对象(包括 ado 连接和记录集),则需要将 aspcompat=true
添加到页面指令中,例如
<%@ Page AspCompat="true" Language="VB" %>
您提供的代码,转换成 VB.net 会像这样
dim Example_DatabaseName
Example_DatabaseName = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
rs1 = Server.CreateObject("ADODB.Recordset")
rs1.Open (mySQL1, Example_DatabaseName)
Do While Not rs1.eof
response.write rs1("NameOfPerson").value & "<br>"
rs1.MoveNext
Loop
rs1.close
rs1 = Nothing
请注意 VB.net 不使用 Let
或 Set
,并且在您的记录集项之后使用 .value
。
我应该补充一点。这提供了调整现有代码以在 ASPX 页面中使用的最短路径,这正是我假设您正在寻找的。 ASP.net 有比 ado 记录集对象更好的连接数据库的方法,但是代码是如此不同以至于你可能需要完全重写它,在这种情况下我建议忘记 VB.net 和使用 C#,您将能够找到更多示例和教程
只是让你知道它现在工作得很好 - 感谢所有指出一些想法的人:)
这是整个页面,以防有人遇到与我类似的问题:
<%@ Page language="vb" debug="true" %>
<% @Import Namespace="System" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<%
Dim sConnection As String = "Server=(local);Database=NameOfDatabase;Trusted_Connection=True;"
Using Con As New SqlConnection(sConnection)
Con.Open()
Using Com As New SqlCommand("Select Name From Test_Table", Con)
Using RDR = Com.ExecuteReader()
If RDR.HasRows Then
Do While RDR.Read
response.write (RDR("Name").ToString)
Loop
End If
End Using
End Using
Con.Close()
End Using
%>
确实那些脚本和经典有很大的不同ASP 呸!!
我目前正在使用 Classic ASP,以便使用以下代码连接到 SQL Server 2008 R2 数据库:
dim Example_DatabaseName
Example_DatabaseName = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
Set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.Open mySQL1, Example_DatabaseName
Do While Not rs1.eof
response.write rs1("NameOfPerson") & "<br>"
rs1.MoveNext
Loop
rs1.close
set rs1 = Nothing
此代码不适用于 ASPX,但它在本地主机 IIS 上与经典 ASP 一起工作得很好。我在网上研究过我遇到了各种不同的错误,这让我更加困惑...
我想请教您 help/advice 如何使用 VB 脚本为 ASPX 执行此操作 - 但是,我对 ASP.net 不是很熟悉 -我只是想自学如何使用它。
我收到错误消息:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30002: Type 'SqlConnection' is not defined.
以上错误消息适用于:
Using Con As New SqlConnection(sConnection)
直接从这里改编Using SQLDataReader instead of recordset
Dim sConnection As String = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
Using Con As New SqlConnection(sConnection)
Con.Open()
Using Com As New SqlCommand("Select NameOfPerson From Person", Con)
Using RDR = Com.ExecuteReader()
If RDR.HasRows Then
Do While RDR.Read
Response.Write( RDR.Item("NameOfPerson ").ToString())
Loop
End If
End Using
End Using
Con.Close()
End Using
您不能在 aspx (asp.net) 文件中使用 VBScript 作为脚本语言。您可以使用 VB.net,这是一种类似的语言,但绝不相同。
如果您想在 asp.net 页面中使用 COM 对象(包括 ado 连接和记录集),则需要将 aspcompat=true
添加到页面指令中,例如
<%@ Page AspCompat="true" Language="VB" %>
您提供的代码,转换成 VB.net 会像这样
dim Example_DatabaseName
Example_DatabaseName = "driver={SQL Server};server=(local);use uid=sa;pwd=; OR Trusted_Connection=yes;database=NameOFDatabaseTesting"
rs1 = Server.CreateObject("ADODB.Recordset")
rs1.Open (mySQL1, Example_DatabaseName)
Do While Not rs1.eof
response.write rs1("NameOfPerson").value & "<br>"
rs1.MoveNext
Loop
rs1.close
rs1 = Nothing
请注意 VB.net 不使用 Let
或 Set
,并且在您的记录集项之后使用 .value
。
我应该补充一点。这提供了调整现有代码以在 ASPX 页面中使用的最短路径,这正是我假设您正在寻找的。 ASP.net 有比 ado 记录集对象更好的连接数据库的方法,但是代码是如此不同以至于你可能需要完全重写它,在这种情况下我建议忘记 VB.net 和使用 C#,您将能够找到更多示例和教程
只是让你知道它现在工作得很好 - 感谢所有指出一些想法的人:)
这是整个页面,以防有人遇到与我类似的问题:
<%@ Page language="vb" debug="true" %>
<% @Import Namespace="System" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<%
Dim sConnection As String = "Server=(local);Database=NameOfDatabase;Trusted_Connection=True;"
Using Con As New SqlConnection(sConnection)
Con.Open()
Using Com As New SqlCommand("Select Name From Test_Table", Con)
Using RDR = Com.ExecuteReader()
If RDR.HasRows Then
Do While RDR.Read
response.write (RDR("Name").ToString)
Loop
End If
End Using
End Using
Con.Close()
End Using
%>
确实那些脚本和经典有很大的不同ASP 呸!!