'DocMgmt Klasse 'Autor: Stefan Hutter, Unternehmensberatung ' '01.04.2003 ' Imports System Imports System.IO Imports System.Data Imports System.Data.SqlTypes Imports System.Data.SqlClient Imports System.ComponentModel Imports UtilityLibrary.Win32 Public Class DocMgmt #Region "Deklarationen" Dim m_dokumentname As String Dim m_dokumentid As String Property Dokumentname() As String Get Return m_dokumentname End Get Set(ByVal Value As String) m_dokumentname = Value End Set End Property Property DokumentID() As String Get Return m_dokumentid End Get Set(ByVal Value As String) m_dokumentid = Value End Set End Property #End Region #Region "Save" Public Function Save_To_DB(ByVal sDokumentID As String, ByVal sDokumentName As String) As Boolean Try Me.DokumentID = sDokumentID Me.Dokumentname = sDokumentName Dim Connection As New SqlConnection() Dim DA As New SqlDataAdapter("select * from doks where dokumentid='" + Me.DokumentID + "'", Connection) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim fs As New FileStream(Me.Dokumentname, FileMode.Open, FileAccess.Read) Dim mydata(fs.Length) As Byte Try Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Dokument von Filesystem lesen") fs.Read(mydata, 0, fs.Length) fs.Close() Connection.ConnectionString = Globals.sConnectionString Connection.Open() DA.Fill(ds, "docs") Dim myRow As DataRow If ds.Tables(0).Rows.Count = 0 Then ' Neues Dokument speichern myRow = ds.Tables(0).NewRow myRow.Item(0) = Me.DokumentID myRow.Item(1) = mydata 'Release Office 2010 'Try ' Dim fi As New FileInfo(Me.Dokumentname) ' myRow.Item(2) = fi.Extension ' fi = Nothing 'Catch ex As Exception 'End Try ds.Tables(0).Rows.Add(myRow) DA.Update(ds, "docs") Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Neues Dokument gespeichert") Else 'Bestehendes Dokument sichenr myRow = ds.Tables(0).Rows(0) myRow.Item(1) = mydata 'Try ' Dim fi As New FileInfo(Me.Dokumentname) ' myRow.Item(2) = fi.Extension ' fi = Nothing 'Catch ex As Exception 'End Try DA.Update(ds, "docs") Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Bestehendes Dokument ersetzt") End If Catch ex As Exception Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Fehler bei der Dokumentspeicherung") MyMsg.show_standardmessage(86, MsgBoxStyle.Critical) ' MsgBox(ex.Message) Return False End Try fs = Nothing cb = Nothing ds = Nothing DA = Nothing Connection.Close() Connection = Nothing Return True Catch EX As Exception Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Fehler bei der Dokumentspeicherung") MyMsg.show_standardmessage(86, MsgBoxStyle.Critical) Return False End Try End Function Public Function Save_Layout_DB(ByVal profilnr As Integer, ByVal sDokumentName As String) As Boolean Dim Connection As New SqlConnection() Dim DA As New SqlDataAdapter("select * from profillayout where profilnr=" + Str(profilnr), Connection) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim fs As New FileStream(sDokumentName, FileMode.OpenOrCreate, FileAccess.Read) Dim mydata(fs.Length) As Byte fs.Read(mydata, 0, fs.Length) fs.Close() Try Connection.ConnectionString = Globals.sConnectionString Connection.Open() DA.Fill(ds, "profil") Dim myRow As DataRow If ds.Tables(0).Rows.Count = 0 Then ' Neues Dokument speichern myRow = ds.Tables(0).NewRow myRow.Item(0) = profilnr '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sDokumentName, 5)) Case "T.LYT" myRow.Item(2) = mydata Case "M.LYT" myRow.Item(3) = mydata Case "V.LYT" myRow.Item(4) = mydata Case Else myRow.Item(1) = mydata End Select ds.Tables(0).Rows.Add(myRow) DA.Update(ds, "profil") Else 'Bestehendes Dokument sichern myRow = ds.Tables(0).Rows(0) '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sDokumentName, 5)) Case "T.LYT" myRow.Item(2) = mydata Case "M.LYT" myRow.Item(3) = mydata Case "V.LYT" myRow.Item(4) = mydata Case Else myRow.Item(1) = mydata End Select DA.Update(ds, "profil") End If Catch ex As Exception MsgBox(ex.Message) Return False End Try fs = Nothing cb = Nothing ds = Nothing DA = Nothing Connection.Close() Connection = Nothing Return True End Function Public Function Save_SuchLayout_DB(ByVal profilnr As Integer, ByVal sDokumentName As String) As Boolean Dim Connection As New SqlConnection() Dim DA As New SqlDataAdapter("select * from suchprofillayout where suchprofilnr=" + Str(profilnr), Connection) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim fs As New FileStream(sDokumentName, FileMode.OpenOrCreate, FileAccess.Read) Dim mydata(fs.Length) As Byte fs.Read(mydata, 0, fs.Length) fs.Close() Try Connection.ConnectionString = Globals.sConnectionString Connection.Open() DA.Fill(ds, "profil") Dim myRow As DataRow If ds.Tables(0).Rows.Count = 0 Then ' Neues Dokument speichern myRow = ds.Tables(0).NewRow myRow.Item(0) = profilnr '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sDokumentName, 6)) Case "TS.LYT" myRow.Item(2) = mydata Case "MS.LYT" myRow.Item(3) = mydata Case "VS.LYT" myRow.Item(4) = mydata Case Else myRow.Item(1) = mydata End Select ds.Tables(0).Rows.Add(myRow) DA.Update(ds, "profil") Else 'Bestehendes Dokument sichern myRow = ds.Tables(0).Rows(0) '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sDokumentName, 6)) Case "TS.LYT" myRow.Item(2) = mydata Case "MS.LYT" myRow.Item(3) = mydata Case "VS.LYT" myRow.Item(4) = mydata Case Else myRow.Item(1) = mydata End Select DA.Update(ds, "profil") End If Catch ex As Exception MsgBox(ex.Message) Return False End Try fs = Nothing cb = Nothing ds = Nothing DA = Nothing Connection.Close() Connection = Nothing Return True End Function #End Region #Region "Get" Public Function Get_From_DB(ByVal sDokumentID As String, ByVal sDokumentName As String, Optional ByRef DokType As String = "") As Boolean Me.DokumentID = sDokumentID Me.Dokumentname = sDokumentName Dim connection As New SqlConnection() Dim da As New SqlDataAdapter("Select * From doks where DokumentID='" + Me.DokumentID + "'", connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() Try 'Connectionstring zur Datenbank connection.ConnectionString = Globals.sConnectionString connection.Open() da.Fill(ds, "docs") 'Versuchen, ob es sich um ein EDKIMP-Dokument handelt - Rel. 3.73 SHU If ds.Tables(0).Rows.Count = 0 Then da.SelectCommand.CommandText = "Select * from doks where dokumentid='" + "EDKIMP" + Microsoft.VisualBasic.Right(Me.DokumentID, Len(Me.DokumentID) - 6) + "'" da.Fill(ds, "Docs") End If Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte MyData = myRow.Item(1) 'If myRow.Item(2).ToString = "" Then DokType = ".doc" Else DokType = myRow.Item(2) Dim K As Long K = UBound(MyData) Dim fs As New FileStream(sDokumentName, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception If Not DivFnkt.isbck(Me.DokumentID) And Not DivFnkt.BDR_Dokument(Me.DokumentID) Then MyMsg.show_standardmessage(93, MsgBoxStyle.Critical) End If Return False End Try CB = Nothing ds = Nothing da = Nothing connection.Close() connection = Nothing Return True End Function Public Function Get_layout_from_DB(ByVal profilnr As Integer, ByVal sdokumentname As String) As String Me.Dokumentname = LTrim(Str(profilnr)) + ".lyt" Dim connection As New SqlConnection() Dim da As New SqlDataAdapter("Select * From profillayout where profilnr=" + Str(profilnr), connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() Try connection.ConnectionString = Globals.sConnectionString connection.Open() da.Fill(ds, "docs") Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sdokumentname, 5)) Case "T.LYT" MyData = myRow.Item(2) Case "M.LYT" MyData = myRow.Item(3) Case "V.LYT" MyData = myRow.Item(4) Case Else MyData = myRow.Item(1) End Select Dim K As Long K = UBound(MyData) Dim fs As New FileStream(sdokumentname, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception Return False End Try CB = Nothing ds = Nothing da = Nothing connection.Close() connection = Nothing Return True End Function Public Function Get_suchlayout_from_DB(ByVal profilnr As Integer, ByVal sdokumentname As String) As String Me.Dokumentname = LTrim(Str(profilnr)) + ".lyt" Dim connection As New SqlConnection() Dim da As New SqlDataAdapter("Select * From suchprofillayout where suchprofilnr=" + Str(profilnr), connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() Try connection.ConnectionString = Globals.sConnectionString connection.Open() da.Fill(ds, "docs") Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte '*********************************** 'Je nach Layoutfile, Daten aus entsprechendem Feld lesen '29.7.2004 / SHU '*********************************** Select Case UCase(Right(sdokumentname, 6)) Case "TS.LYT" MyData = myRow.Item(2) Case "MS.LYT" MyData = myRow.Item(3) Case "VS.LYT" MyData = myRow.Item(4) Case Else MyData = myRow.Item(1) End Select Dim K As Long K = UBound(MyData) Dim fs As New FileStream(sdokumentname, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception Return False End Try CB = Nothing ds = Nothing da = Nothing connection.Close() connection = Nothing Return True End Function #End Region #Region "CheckDoc" Public Function check_doc(ByVal fnkt As Integer) As DataTable Dim scmCmdToExecute As SqlCommand = New SqlCommand() Dim dtToReturn As DataTable = New DataTable() Dim sdaAdapter As SqlDataAdapter = New SqlDataAdapter(scmCmdToExecute) scmCmdToExecute.CommandText = "dbo.sp_check_dok" scmCmdToExecute.CommandType = CommandType.StoredProcedure scmCmdToExecute.Connection = conn.scoDBConnection Try scmCmdToExecute.Parameters.Add(New SqlParameter("@mitarbeiternr", SqlDbType.Int, 4, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, Globals.MitarbeiterNr)) scmCmdToExecute.Parameters.Add(New SqlParameter("@fnkt", SqlDbType.Int, 4, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, fnkt)) ' scmCmdToExecute.Parameters.Add(New SqlParameter("@stationsname", SqlDbType.VarChar, 255, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, Environ("Computername"))) sdaAdapter.Fill(dtToReturn) Return dtToReturn Catch ex As Exception Throw New Exception("Check_Doc::" & scmCmdToExecute.CommandText & "::Error occured." & ex.Message, ex) Finally sdaAdapter.Dispose() scmCmdToExecute.Dispose() dtToReturn.Dispose() End Try End Function #End Region #Region "Office 2010" Public Function get_office_2010_XML_File(ByVal sdokumentname As String) Dim connection As New SqlConnection() Try If System.IO.File.Exists(sdokumentname) Then System.IO.File.Delete(sdokumentname) End If Catch End Try Dim da As New SqlDataAdapter("Select * From Office_2010_Params where nreintrag=1", connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New DataSet() Try connection.ConnectionString = Globals.sConnectionString connection.Open() da.Fill(ds, "docs") Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte MyData = myRow.Item(2) Dim K As Long K = UBound(MyData) Dim fs As New FileStream(sdokumentname, FileMode.Create, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception Return False End Try CB = Nothing ds = Nothing da = Nothing connection.Close() connection = Nothing Return True End Function #End Region #Region "AVQ_Werte" Public Function save_avq_werte_to_db(ByVal filename As String, dpinszanz As Integer) Dim Connection As New SqlConnection() Dim DA As New SqlDataAdapter("select * from edex_dpinstanz_avqwerte where edex_dp_instanznr=" + Str(dpinszanz), Connection) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read) Dim mydata(fs.Length) As Byte fs.Read(mydata, 0, fs.Length) fs.Close() Try Connection.ConnectionString = Globals.sConnectionString Connection.Open() DA.Fill(ds, "profil") Dim myRow As DataRow If ds.Tables(0).Rows.Count = 0 Then myRow = ds.Tables(0).NewRow myRow.Item(0) = dpinszanz myRow.Item(1) = mydata ds.Tables(0).Rows.Add(myRow) DA.Update(ds, "profil") Else myRow = ds.Tables(0).Rows(0) myRow.Item(1) = mydata DA.Update(ds, "profil") End If Catch ex As Exception MsgBox(ex.Message) Return False End Try fs = Nothing cb = Nothing ds = Nothing DA = Nothing Connection.Close() Connection = Nothing Return True End Function Public Function Get_avq_werte_from_db(ByVal filename As String, ByVal dpinstanz As Integer) As Boolean Dim connection As New SqlConnection() Dim DA As New SqlDataAdapter("select * from edex_dpinstanz_avqwerte where edex_dp_instanznr=" + Str(dpinstanz), connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Try connection.ConnectionString = Globals.sConnectionString connection.Open() DA.Fill(ds, "docs") If ds.Tables(0).Rows.Count < 1 Then Return False Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte MyData = myRow.Item(1) 'If myRow.Item(2).ToString = "" Then DokType = ".doc" Else DokType = myRow.Item(2) Dim K As Long K = UBound(MyData) Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception Return False End Try CB = Nothing ds = Nothing DA = Nothing connection.Close() connection = Nothing Return True End Function #End Region #Region "PDFCheck" Dim tmpkey As String = "" Public Function Check_PDF(filename As String) As Boolean If Save_PDF_To_DB(filename) = False Then Return False End If If Get_PDF_From_DB() = False Then Return False End If Dim i As Integer Dim fn As String = Globals.Applikationsdaten.Rows(Globals.AppldataRow).Item("pfad_temporaer_dokumente") + tmpkey + ".pdf" Dim info As New System.IO.FileInfo(fn) i = info.Length Try File.Delete(fn) Delete_PDF_From_DB() Catch ex As Exception End Try If i < 1 Then Return False Return True End Function Public Function Save_PDF_To_DB(ByVal sDokumentName As String) As Boolean Try Me.Dokumentname = sDokumentName tmpkey = Guid.NewGuid.ToString Dim Connection As New SqlConnection() Dim DA As New SqlDataAdapter("select top 1 * from tmpdok where tmpkey='" + tmpkey + "'", Connection) Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim fs As New FileStream(Me.Dokumentname, FileMode.Open, FileAccess.Read) Dim mydata(fs.Length) As Byte Try Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Dokument von Filesystem lesen") fs.Read(mydata, 0, fs.Length) fs.Close() Connection.ConnectionString = Globals.sConnectionString Connection.Open() DA.Fill(ds, "tmpdocs") Dim myRow As DataRow If ds.Tables(0).Rows.Count = 0 Then ' Neues Dokument speichern myRow = ds.Tables(0).NewRow myRow.Item(0) = tmpkey myRow.Item(1) = mydata ds.Tables(0).Rows.Add(myRow) DA.Update(ds, "tmpdocs") Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Neues Dokument gespeichert") Else 'Bestehendes Dokument sichenr myRow = ds.Tables(0).Rows(0) myRow.Item(1) = mydata 'Try ' Dim fi As New FileInfo(Me.Dokumentname) ' myRow.Item(2) = fi.Extension ' fi = Nothing 'Catch ex As Exception 'End Try DA.Update(ds, "tmpdocs") Globals.PerfMon.insert_entry(Me.DokumentID + " ---- Bestehendes Dokument ersetzt") End If Catch ex As Exception Return False End Try fs = Nothing cb = Nothing ds = Nothing DA = Nothing Connection.Close() Connection = Nothing Return True Catch EX As Exception Return False End Try End Function Public Function Get_PDF_From_DB() As Boolean Dim connection As New SqlConnection() Dim DA As New SqlDataAdapter("select top 1 * from tmpdok where tmpkey='" + tmpkey + "'", connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Dim filename As String = Globals.Applikationsdaten.Rows(Globals.AppldataRow).Item("pfad_temporaer_dokumente") + tmpkey + ".pdf" Try 'Connectionstring zur Datenbank connection.ConnectionString = Globals.sConnectionString connection.Open() DA.Fill(ds, "tmpdocs") Dim myRow As DataRow myRow = ds.Tables(0).Rows(0) Dim MyData() As Byte MyData = myRow.Item(1) 'If myRow.Item(2).ToString = "" Then DokType = ".doc" Else DokType = myRow.Item(2) Dim K As Long K = UBound(MyData) Dim fs As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(MyData, 0, K) fs.Close() fs = Nothing Catch ex As Exception If Not DivFnkt.isbck(Me.DokumentID) And Not DivFnkt.BDR_Dokument(Me.DokumentID) Then MyMsg.show_standardmessage(93, MsgBoxStyle.Critical) End If Return False End Try CB = Nothing ds = Nothing da = Nothing connection.Close() connection = Nothing Return True End Function Public Function Delete_PDF_From_DB() Dim connection As New SqlConnection() Dim DA As New SqlDataAdapter("delete from tmpdok where tmpkey='" + tmpkey + "'", connection) Dim CB As SqlCommandBuilder = New SqlCommandBuilder(DA) Dim ds As New DataSet() Try connection.ConnectionString = Globals.sConnectionString connection.Open() DA.Fill(ds, "tmpdocs") Catch ex As Exception End Try CB = Nothing ds = Nothing DA = Nothing connection.Close() connection = Nothing Return True End Function #End Region End Class