【问题标题】:Script can't find database脚本找不到数据库
【发布时间】:2016-03-03 13:10:14
【问题描述】:

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

找不到“dbo”列或用户定义的函数,或 聚合“dbo.certdate”,或者名称不明确

奇怪的是脚本之前使用可以工作。我没有更改代码,唯一改变的是我安装了不同类型的 Office 版本,这对于使脚本正常工作是必要的。会不会犯这个错误?可能是什么问题呢?

我正在使用 SQL Server 2008 R2 和 Office Enterprise 2007

这是数据库代码:

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

结束函数

【问题讨论】:

  • 您说代码在您更改 Office 版本之前可以正常工作,但随后您说您更改了 Office 版本以使其正常工作。这表明代码在您更改 Office 版本之前或之后都不起作用。
  • 如果您阅读How to create a Minimal, Complete, and Verifiable Example,也会有所帮助。
  • 您发布的脚本似乎不完整。您在那里有一个不会导致您看到的错误的选择调用 - 您还有一个未使用的函数试图调用 ExecuteReader() (这可能会导致您看到的错误)。有很多事情可能会导致这种情况 - 可能是 DB 上的权限缺失/不足,或者 SQL 语句中的拼写错误。
  • @DanField 我更新了我的代码。我很抱歉之前的错误。你能看看那个代码并给我你的意见吗?
  • 似乎很可能dbo.certdate 调用了您无权访问的其他一些过程/函数(即dbo.Splitfn)。检查您的数据库权限和/或确保dbo.Splitfn 存在于数据库中。

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


【解决方案1】:

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

【讨论】:

  • 谢谢。我在哪里可以运行exec dbo.certdate('test')? SQL Server 中是否有一个特殊的地方可以做到这一点,还是应该在 SQL 查询中写下来?
  • 我认为 dbo.certdate 是一个数据库函数,但是如何检查它是否存在?
  • SSMS 可能是最直接的方式。或者让您的 DBA 来做。
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多