如何让 ASP.NET (Framework 4.5) Web 窗体项目识别 SQLServer class?

How can I get an ASP.NET (Framework 4.5) Web Forms project to recognize the SQLServer class?

我已经

所以,我尝试从头开始创建一个新项目,从遗留项目中一个一个地添加文件夹和文件。但是,它 现在正处于构建时生成 106 错误的地步。

于是,我又重新开始了。我创建了一个新项目 > 模板 > Visual Basic > Web > ASP.NET Web 应用程序。并首先添加了引用 SQL 内容的 class,以尝试找到问题的根源。

为此,我模仿遗留项目,向项目添加了一个 App_Code 文件夹,然后向其中添加了 commonClass.vb,并开始一次一个地复制函数。首先,我添加了 Imports 子句,然后添加了几个函数:

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports DAL05.DataAccess
Imports System

Public Class commonClass

    Public Shared Function GetImages(ByVal MemberNo As String, ByVal ImageID As String, ByVal FileName As String) As DataView
        Dim ds As New DataSet()
        Dim SQLstring As String = "EXEC up_GettyImages '" & MemberNo & "','" & ImageID & "','" & FileName & "'"
        Dim Command As New SqlDataAdapter(SQLstring, System.Configuration.ConfigurationManager.AppSettings("OrderEnterConnection"))
        Command.Fill(ds, "xImages")
        Dim dv As DataView
        dv = ds.Tables("xImages").DefaultView()
        Return dv
    End Function

        Public Function GetPortalMenuItems() As DataSet
        Dim dset As New DataSet
        dset.ReadXml(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/PortalMenuItems.xml"))
        Return dset
    End Function

End Class

这里出现了第一个问题:Imports DAL05.DataAccess 中的最后一个 "s" 有一个红色下划线,但是点击 "Error Correction Options" 说,“ 无更正建议

显然它不知道 DAL05.DataAccess 是什么;所以我在我的项目中寻找 "References" 文件夹 - 没有。遗留项目有一个包含 DAL05 的 Bin 文件夹:

我也有关于添加这个的问题with/question,here

那么是不是遗留项目以 .NET Framework 3.5 为目标,而新项目以 4.5 为目标导致了 mismatch/confusion? DAL05.DataAccess 是导入错误的东西吗?它是否已被其他东西取代?

在 Project > Properties > References > Imported namespaces 下,我添加了 "Microsoft.SqlServer",这让我知道我的一些 Imports 是不必要的,所以我现在只有:

Imports System.Data.SqlClient
Imports System.Data

...但是当我添加这个方法时:

Public Function LogAction(ByVal SessionID As String, ByVal userId As String, ByVal action As String, ByVal comment As String) As DataTable
    Dim sqlDAL As New SQLServer(System.Configuration.ConfigurationManager.AppSettings("OrderEnterConnection"))
    Dim SQLstring As String = "Exec fredFlintstone '" & SessionID & "','" & userId & "','" & action & "','" & comment & "'"
    Dim dset As DataSet
    dset = sqlDAL.runSQLDataSet(SQLstring)
    Return dset.Tables(0)
End Function

...SQL服务器红了,那里的"helpful hint"告诉我:

Type 'SQLServer' is not defined.

...具有以下选项:

Change 'SQLServer' to 'SQLError'
Generate 'Class SQLServer'
Generate new type...

None 这些选项对我来说似乎是明智的。

如果真的无法使用SQL服务器class,我应该用什么代替?

更新

更让我觉得陌生的(可能是因为我对VB比较陌生)是,虽然"SQLServer"在编辑器里是红色的,无法识别,但是我重建项目的时候,却成功了?怎么可能呢?

如果我 F5 项目,它会失败:

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 'SQLServer' is not defined.

Source Error:    

Line 21: 
Line 22:     Public Function LogAction(ByVal SessionID As String, ByVal userId As String, ByVal action As String, ByVal comment As String) As DataTable
Line 23:         Dim sqlDAL As New SQLServer(System.Configuration.ConfigurationManager.AppSettings("OrderEnterConnection"))
Line 24:         Dim SQLstring As String = "Exec sp_LogAction_Net '" & SessionID & "','" & userId & "','" & action & "','" & comment & "'"
Line 25:         Dim dset As DataSet

Source File: C:\Projects\MemberOrderEntryNew\MemberOrderEntry3\MemberOrderEntry3\App_Code\commonClass.vb    Line: 23

...但它(重新)构建良好让我感到困惑。

更新 2

我也试过System.Data.SQLClient,但没有用——它和"SQLServer"有同样的问题:

根据您目前提供的信息,完全不清楚您为什么需要 DAL05 dll。如果您可以使用标准 System.Data.SqlClient 对象连接到 SQL 服务器,请使用这些对象并放弃 Dal05。

如果您绝对必须知道 Dal05 的作用及其工作原理,请获取 Telerik's JustDecompile(它是免费的)并且(假设它是一个 .NET dll)您将能够查看源代码以找出答案它在做什么以及它如何 类 工作。

这里有两个替换LogAction方法的选项:

Public Function LogActionOption1(SessionID As String, userId As String, action As String, comment As String) As DataTable
    Dim dt As New DataTable

    Using sqlConn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("OrderEnterConnection"))
        Using sqlCmd As New SqlCommand("sp_LogAction_Net", sqlConn)
            sqlCmd.CommandType = CommandType.StoredProcedure
            'TODO: REQUIRED - set the parameter names, types and sizes to comply with the declarations in the stored procedure.
            ' Note: sessionid is expected to be a 24-character string: 
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@SessionId", .SqlDbType = SqlDbType.Char, .Size = 24, .Value = SessionID})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@userId", .SqlDbType = SqlDbType.NVarChar, .Size = 50, .Value = userId})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@action", .SqlDbType = SqlDbType.NVarChar, .Size = 16, .Value = action})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@comment", .SqlDbType = SqlDbType.NVarChar, .Size = 500, .Value = comment})
            Dim da As New SqlDataAdapter(sqlCmd)
            da.Fill(dt)
        End Using
    End Using

    Return dt

End Function

但是,返回的数据表可能并未实际使用,或者未以证明使用数据表的方式使用。

如果只需要确认数据已添加到数据库中:

Public Function LogActionOption2(SessionID As String, userId As String, action As String, comment As String) As Boolean
    Dim success As Boolean = False
    Dim dt As New DataTable

    Using sqlConn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("OrderEnterConnection"))
        Using sqlCmd As New SqlCommand("sp_LogAction_Net", sqlConn)
            sqlCmd.CommandType = CommandType.StoredProcedure
            'TODO: REQUIRED - set the parameter names, types and sizes to comply with the declarations in the stored procedure.
            ' Note: sessionid is expected to be a 24-character string: 
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@SessionId", .SqlDbType = SqlDbType.Char, .Size = 24, .Value = SessionID})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@userId", .SqlDbType = SqlDbType.NVarChar, .Size = 50, .Value = userId})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@action", .SqlDbType = SqlDbType.NVarChar, .Size = 16, .Value = action})
            sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@comment", .SqlDbType = SqlDbType.NVarChar, .Size = 500, .Value = comment})

            sqlConn.Open()
            success = (sqlCmd.ExecuteNonQuery > 0)
            sqlConn.Close()

        End Using
    End Using

    Return success

End Function

即使不使用最小的反馈,您也可以轻松地将其更改为 Sub。

P.S。如果您最终修改了存储过程,则应避免在它们前面加上 "sp_" 前缀:Is the sp_ prefix still a no-no?

我得到了要构建的 MembesOrderEntry 项目 运行,在浏览器中显示了登录页面。

下一步是更新 Telerik DLL。我应该使用哪些?那些在?