公共连接字符串不可访问

时间:2014-03-12 18:05:00

标签: vb.net

我已导入自己的数据库连接文件,但它不喜欢dbConnectDBOStr字符串

任何想法?

Imports Pirelli.dbPirelli

Partial Class _Default
    Inherits System.Web.UI.Page


    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Load
        Dim strCmd As String
        Dim dbCmd As SqlCommand
        Dim oConn As SqlConnection
        Dim dbReader As SqlDataReader


        oConn = New SqlConnection(dbConnectDBOStr)

连接文件:

Imports System.Data.SqlClient


Namespace Pirelli


    Public Class dbPirelli
        'Database Server - ENABLE ONE ONLY
        Public Const strServerName As String = "[Server]"  'DEV

        Public Const dbConnectDBOStr As String = "uid=[USER];password=[PASS];database=[DB];server=" & strServerName & ";Connection Timeout=60;"

    End Class

End Namespace

我收到此错误:

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: BC30451: Name 'dbConnectDBOStr' is not declared.

 Source Error:

Line 21: 
Line 22: 
Line 23:         oConn = New SqlConnection(dbConnectDBOStr)
Line 24: 
Line 25:         oConn.Open() 

2 个答案:

答案 0 :(得分:1)

由于您没有处理dbPirelli类的实例,因此您应该使您的类静态并使用命名空间调用它:

Namespace Pirelli

    ' In VB a static class is called a "Module"
    Public Module dbPirelli
        'Database Server - ENABLE ONE ONLY
        Public Const strServerName As String = "[Server]"  'DEV

        Public Const dbConnectDBOStr As String = "uid=[USER];password=[PASS];database=[DB];server=" & strServerName & ";Connection Timeout=60;"

    End Module

End Namespace

...

' Since you're importing the namespace Pirelli.dbPirelli, you don't need
' to call the namespace as I had previously indicated
oConn = New SqlConnection(dbConnectDBOStr)

答案 1 :(得分:0)

您可以像以下一样使用它:

oConn = New SqlConnection(dbPirelli.dbConnectDBOStr)