脚本无法找到数据库

时间:2016-03-03 13:10:14

标签: .net sql-server vb.net sql-server-2008-r2 office-interop

我正在开发一个转换和打印PDF文件的项目。 运行脚本时出现错误:

  

找不到列“dbo”或用户定义的函数或   聚合“dbo.certdate”,或名称含糊不清

奇怪的是脚本使用以前工作。我没有改变代码,唯一改变的是我安装了不同类型的Office版本,这是使脚本运行所必需的。这会出错吗?可能是什么问题呢?

我使用的是SQL Server 2008 R2和Office Enterprise 2007

这是db代码:

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Text
Imports Microsoft.Office.Interop

Public Class ConnectionManager

    Public Enum DbType
        CERT = 1
        ARKIV = 2
    End Enum

    Public Sub New(ByRef _log As Logg)
        Log = _log
    End Sub

    Private _databas As DbType
    Public Property Databas As DbType
        Get
            Return _databas
        End Get
        Set(value As DbType)
            _databas = value
            If Not _connection Is Nothing Then _connection = Nothing
        End Set
    End Property
    Private Property Log As Logg
    Public ReadOnly Property ConnectionStrang As String
        Get
            Select Case Databas
                Case DbType.ARKIV
                    Return ConfigurationManager.ConnectionStrings("databas.arkiv").ConnectionString
                Case DbType.CERT
                    Return ConfigurationManager.ConnectionStrings("databas.cert").ConnectionString
                Case Else
                    Return ""
            End Select

        End Get
    End Property

    Public ReadOnly Property ServerName As String
        Get
            Dim res As String = ""
            Dim start As Integer = ConnectionStrang.IndexOf("(HOST=") + 6
            Dim slut As Integer = ConnectionStrang.IndexOf(")", start)

            res = ConnectionStrang.Substring(start, slut - start)
            Return res
        End Get
    End Property
    Private _connection As SqlConnection
    Public ReadOnly Property Connection(Optional forceNew As Boolean = False) As SqlConnection
        Get
            If _connection Is Nothing Or forceNew = True Then _connection = New SqlConnection(ConnectionStrang)
            If Not _connection.State = ConnectionState.Open Then _connection.Open()
            Return _connection
        End Get
    End Property
    Public ReadOnly Property FilePath As String
        Get
            Dim path As String = System.AppDomain.CurrentDomain.BaseDirectory & "\out"
            If IO.Directory.Exists(path) = False Then IO.Directory.CreateDirectory(path)
            Return path & "\"
        End Get
    End Property
    Private _wrdapp As Word.Application
    Public ReadOnly Property WordApp As Word.Application
        Get
            If _wrdapp Is Nothing Then _wrdapp = New Word.Application
            Return _wrdapp
        End Get
    End Property

    Public Sub TestConnection()
        Try
            Dim com As New SqlCommand("SELECT count(*) FROM sys.tables", Connection(True))
            Log.Add(com.ExecuteScalar & " tabeller hittat i " & Databas.ToString)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub



    Public Function GetReder(sql As String, Optional newConn As Boolean = False) As SqlDataReader
        Try
            Dim cmd As New SqlCommand(sql, Connection(newConn))
            Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class

更新

以下是代码问题:

Private Function createBevakningsRegisterData(pers As String) As PDFData
    Try
        Dim r As SqlDataReader
        Dim ret As New PDFData(PDFData.vissatt.list)
        Dim sql As String = " select "
        sql &= "WATCHCODE as Kod,"
        sql &= "dbo.certdate(WATCHDATE) as [Datum        ],"
        sql &= "WATCHTEXT as [Text],"
        sql &= "watchflag as [Påm.],"
        sql &= "WATCHREMDATE as [tom.]"
        sql &= " from CERTUSR_WATCH "
        sql &= "where PERSNR ='" & pers & "'"

    r = GetReder(sql)

    ret.Rubrik = "BEVAKNINGSREGISTER"

    Dim rowCounter As Integer = 0
    While r.Read
        For i As Integer = 0 To r.FieldCount - 1
            If rowCounter = 0 Then ret.Properties.Add(r.GetName(i))
            ret.Values.Add(r(i).ToString)
        Next
        rowCounter = 1
    End While

    r.Close()

    Return ret
Catch ex As Exception
    Throw ex
End Try

结束功能

1 个答案:

答案 0 :(得分:0)

检查您要连接的数据库。使用与运行脚本的用户相同的凭据进行连接。尝试运行exec dbo.certdate('test')。您可能会收到错误,因为certdate不存在或您没有权限。以管理员身份登录并创建它或为其设置适当的权限。