2016-03-03 118 views
-2

我正在研究轉換和打印PDF文件的項目。 在運行該腳本,我得到錯誤:腳本無法找到數據庫

Cannot find either column 「dbo」 or the user-defined function or aggregate 「dbo.certdate」, or the name is ambiguous

奇怪的是腳本使用前工作。我沒有更改代碼,唯一改變的是我安裝了不同類型的Office版本,以使腳本能夠正常工作。這可能會導致這個錯誤?可能是什麼問題呢?

我使用的是SQL Server 2008 R2和Office企業版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 

UPDATE:

這裏是代碼的問題是:

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 

End Function

+3

你說的代碼用於工作,直到你改變了Office的版本,但是你說你改變了Office的版本以使其工作。這表明代碼在更改Office版本之前或之後都不起作用。 – Blackwood

+1

如果您閱讀[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve),這也會有所幫助。 – Blackwood

+2

您發佈的腳本似乎不完整。你有一個單一的select調用,不會導致你看到的錯誤 - 你還有一個未使用的函數試圖調用ExecuteReader()(這可能會導致你看到的錯誤)。有很多事情可能會導致這種情況 - 可能缺少/不足的數據庫權限,或SQL語句中的拼寫錯誤。 –

回答

0

檢查您要連接的數據庫。使用與運行腳本的用戶相同的憑據進行連接。嘗試運行exec dbo.certdate('test')。您可能會遇到錯誤,因爲certdate不存在或您沒有權限。以管理員身份登錄並創建它或在其上設置適當的權限。

+0

謝謝。我在哪裏可以運行'exec dbo.certdate('test')'?在SQL服務器中是否有特殊的位置,我可以這樣做,還是應該在SQL查詢中寫下它? –

+0

我認爲dbo.certdate是一個數據庫函數,但是如何檢查它是否存在? –

+0

SSMS可能是最直接的方法。或讓你的DBA做到這一點。 –