簡體   English   中英

如何讓ASP.NET(Framework 4.5)Web窗體項目識別SQLServer類?

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

無法運行遺留的VB.NET ASP站點

因此,我嘗試從頭開始創建一個新的文件夾,從遺留項目中逐個添加文件夾和文件。 但是,它無法識別SQLServer類 ,現在處於構建時生成106錯誤的位置。

因此,我又重新開始了。 我創建了一個新項目>模板> Visual Basic> Web> ASP.NET Web應用程序。 並首先添加引用SQL內容的類,以嘗試找到問題的根源。

為此,模仿遺留項目,我在項目中添加了一個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中的最后一個“s” .DataAccess有一個紅色下划線,但點擊“Error Correction Options”說“ 沒有更正建議

顯然它不知道DAL05.DataAccess是什么; 所以我在我的項目中找了一個“References”文件夾 - 沒有一個。 舊項目有一個帶DAL05的Bin文件夾:

在此輸入圖像描述

我有一個問題/關於添加這個問題, 這里也是

那么遺留項目是針對.NET Framework 3.5的,而新的目標是4.5導致不匹配/混淆? 導入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

... SQLServer被重新編寫,那里的“有用提示”告訴我:

Type 'SQLServer' is not defined.

......有以下選項:

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

這些選項對我來說都不合理。

如果真的不可能使用SQLServer類,我應該使用什么呢?

UPDATE

對我來說更奇怪(也許是因為我對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”有同樣的問題:

在此輸入圖像描述

以下是替換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: http://stackoverflow.com/a/3518523/1115360
            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: http://stackoverflow.com/a/3518523/1115360
            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。

PS如果最終修改存儲過程,則應避免使用“sp_” 作為前綴sp_前綴是否仍為禁止?

根據您目前提供的信息,目前尚不清楚為什么需要DAL05 dll。 如果可以使用標准的System.Data.SqlClient對象連接到SQL Server,請使用這些對象並丟棄Dal05。

如果您必須知道Dal05正在做什么以及它是如何工作的,那么抓住Telerik的JustDecompile (它是免費的)和(假設它是一個.NET dll),您將能夠查看源代碼以了解它正在做什么以及它的類如何工作。

我得到了MembesOrderEntry項目來構建和運行,在瀏覽器中顯示登錄頁面。

下一步是更新Telerik DLL。 我應該使用哪些? 那些?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM