Imports System.Data.SqlClient Imports System.Reflection Public Class Class1 Public sAppPath As String = Application.StartupPath 'Public Shared DBConString As String = "Server=NBMIESENBECK\SPIELWIESE;Initial Catalog=Doku;User ID=sa;Password=verag#3;Connection Timeout=5;" Public Shared DBConString As String Public Shared DBConstringDev As String = "Server=DEVELOPER.verag.ost.dmn\DEVSQL;Initial Catalog=Doku;User ID=sa;Password=BmWr501956;Connection Timeout=5;" Public Shared DBADMINBuchhaltung As String = "Data Source=192.168.0.94\SQLEXPRESS;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;" 'Public Shared FilePath As String = "\\192.168.0.253\backup\temp_Sebastian\Spielwiese\Doku" Public Shared FilePath As String ' = "\\192.168.0.90\f\EDV-Wartung\Dokumentation" Public Shared Absender As String = "Absender Standard" 'Public Shared sAppPath As String = Application.StartupPath Public Shared Standorte As New List(Of String) Public Shared AlleStandorte As New List(Of String) Public Shared LoggedOnUser As New cBenutzer Public Shared LoggedOnUserRights As New List(Of cBerechtigungen) Public Shared DebugMode As Boolean = False Public Shared GroupUsers As List(Of cBenutzer) Public Shared Sub EnableDoubleBuffered(ByVal dgv As DataGridView) Dim dgvType As Type = dgv.[GetType]() Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic) pi.SetValue(dgv, True, Nothing) End Sub Public Shared Function CheckDebug() Dim Startparameter As String = "" Dim i As Integer For Each s As String In Environment.GetCommandLineArgs If i > 0 Then If s = "/d" Or s = "-d" Then Startparameter = "debug" Else End If End If i += 1 Next If Main.sAppPath Like "*\bin\Debug" Or Startparameter = "debug" Then DebugMode = True Class1.DBConString = "Server=DEVELOPER\DEVSQL;Initial Catalog=Doku;User ID=sa;Password=BmWr501956;Connection Timeout=5;" Class1.FilePath = "\\192.168.0.253\backup\temp_Sebastian\DokuTest" Else Class1.DBConString = "Server=192.168.0.94\SQLEXPRESS;Initial Catalog=Doku;User ID=sa;Password=BmWr501956;Connection Timeout=5;" Class1.FilePath = "\\192.168.0.90\f\EDV-Wartung\Dokumentation" End If End Function Public Shared Function SQLnQ(command As String) Dim con As New SqlConnection Dim cmd As New SqlCommand con.ConnectionString = DBConString Try con.Open() cmd.Connection = con cmd.CommandText = command cmd.ExecuteNonQuery() con.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Function Public Shared Function dbload() Dim con As New SqlConnection Dim cmd As New SqlCommand con.ConnectionString = DBConString cmd.Connection = con End Function Public Shared Function csvexport(quelldgv As DataGridView) Dim savefiledialog As New SaveFileDialog Dim firstin As String Dim out As String Dim in2 As String Dim header As String savefiledialog.Filter = "CSV files (*.csv)|*.CSV" savefiledialog.InitialDirectory = "C:\Verag" savefiledialog.ShowDialog() Try For r As Integer = 0 To quelldgv.Rows.Count - 1 For c As Integer = 0 To quelldgv.Columns.Count 'test &= r & c & vbCrLf Try 'test &= Replace(quelldgv.Rows(r).Cells(c).Value.ToString, vbCrLf, "vbCrlLf") firstin = quelldgv.Rows(r).Cells(c).Value.ToString 'in2 = """" & firstin & """" & ";" in2 = firstin & ";" out &= Replace(in2, vbCrLf, "##vbCrLf##") Catch ex As Exception out &= "" End Try Next out &= "nZ" Next For c As Integer = 0 To quelldgv.Columns.Count - 1 header &= quelldgv.Columns(c).Name & ";" Next out = Replace(out, "nZ", vbCrLf) 'MsgBox(out) Using sw As New IO.StreamWriter(savefiledialog.FileName) sw.WriteLine(header) sw.WriteLine(out) End Using Catch ex As Exception MsgBox(ex.Message) End Try End Function Public Shared Function hochkomma(textin As String) Dim textout As String = textin.Replace("'", "''") Return (textout) End Function Public Shared Function ip2netname(IP As String, ByRef NWStandort As String, ByRef netname As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader Dim NWAddress As String Dim pointcounter As Integer = 0 Dim digcounter As Integer = 0 con.ConnectionString = DBConString cmd.Connection = con For Each c As Char In IP If c = "." Then pointcounter = pointcounter + 1 End If digcounter = digcounter + 1 Next If pointcounter < 3 Or digcounter < 7 Then MsgBox("Das scheint keine gültige IP Adresse zu sein...") Exit Function End If NWAddress = IP.Substring(0, IP.LastIndexOf(".")) & "." con.Open() cmd.CommandText = "SELECT * FROM Tbl_Netzwerke WHERE NWAddress = '" & NWAddress & "' " cmd.ExecuteNonQuery() reader = cmd.ExecuteReader() Do While reader.Read NWStandort = reader("NWStandort") netname = reader("Netname") Loop reader.Close() con.Close() End Function Public Shared Function IPexist(IP As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT * FROM Tbl_Netzwerkclients WHERE IPAdresse = '" & IP & "'" cmd.ExecuteNonQuery() reader = cmd.ExecuteReader() If reader.Read Then MsgBox("Die IP Adresse " & IP & " ist bereits belegt!") Return True con.Close() Exit Function End If Return False End Function Public Shared Function IP2Host(IP As String) Dim pointcounter As Integer = 0 Try Do If IP.StartsWith(".") Then pointcounter = pointcounter + 1 End If IP = IP.Remove(0, 1) Loop Until pointcounter = 3 'IP.First() = "." Return IP Catch ex As Exception MsgBox(ex.Message) End Try End Function Public Shared Function ip2ipnet(ip As String) Dim lastbutnotleast As String = "." Do ip = ip.Remove((ip.Length - 1), 1) Loop Until ip.EndsWith(".") Return ip End Function Public Shared Function netmaskseperator(nm As String, ByRef a As Integer, ByRef b As Integer, ByRef c As Integer, ByRef d As Integer, ByRef nwbits As Integer, ByRef hostbits As Integer) Dim bitstring As String Try Do a &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do b &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do c &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do d &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.Length = 0 Catch ex As Exception MsgBox("netmaskseperator: " & ex.Message) End Try Dim ai As Integer = Convert.ToString(a, 2) Dim bi As Integer = Convert.ToString(b, 2) Dim ci As Integer = Convert.ToString(c, 2) Dim di As Integer = Convert.ToString(d, 2) bitstring = ai & bi & ci & di nwbits = 0 hostbits = 32 For Each ch As Char In bitstring If ch = "1" Then nwbits = nwbits + 1 hostbits = hostbits - 1 End If Next End Function Public Shared Function mask2nwbit(nm As String, ByRef nwbits As Integer) Dim bitstring As String Dim a, b, c, d, hostbits As Integer Try Do a &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do b &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do c &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.First() = "." nm = nm.Remove(0, 1) Do d &= nm.Substring(0, 1) nm = nm.Remove(0, 1) Loop Until nm.Length = 0 Catch ex As Exception MsgBox("netmaskseperator: " & ex.Message) End Try Dim ai As Integer = Convert.ToString(a, 2) Dim bi As Integer = Convert.ToString(b, 2) Dim ci As Integer = Convert.ToString(c, 2) Dim di As Integer = Convert.ToString(d, 2) bitstring = ai & bi & ci & di nwbits = 0 hostbits = 32 For Each ch As Char In bitstring If ch = "1" Then nwbits = nwbits + 1 hostbits = hostbits - 1 End If Next End Function Public Shared Function bit2mask(bit As String, ByRef mask As String) Dim a, b, c, d As String If bit \ 8 >= 1 Then a = "255" bit = bit - 8 If bit \ 8 >= 1 Then b = "255" bit = bit - 8 If bit \ 8 >= 1 Then c = "255" bit = bit - 8 If bit \ 8 >= 1 Then d = "255" bit = bit - 8 Else d = 256 - (2 ^ (8 - bit)) End If Else c = 256 - (2 ^ (8 - bit)) d = 0 End If Else b = 256 - (2 ^ (8 - bit)) c = 0 d = 0 End If Else a = 256 - (2 ^ (8 - bit)) b = 0 c = 0 d = 0 End If mask = a & "." & b & "." & c & "." & d Return mask End Function Private Shared Function checknull(text As String) Dim Reader As SqlDataReader Try ' If reader.Read() Then If Reader.IsDBNull(0) Then 'MsgBox("Null") Return String.Empty Else Return Reader(text).ToString End If 'End If Catch ex As Exception 'MsgBox("Checknull: " & ex.Message) End Try End Function Public Shared Function NWAddressGenerieren(NWAddress As String, ByRef IPShort As String) Try Do NWAddress = NWAddress.Remove(NWAddress.Length - 1) Loop Until NWAddress.Last() = "." Catch ex As Exception MsgBox("Fehler beim Konvertieren der Netzwerkadresse.") End Try IPShort = NWAddress End Function Public Shared Function getStartEndHost(ByVal ip As String, ByRef starthost As Integer, ByRef endhost As Integer) Dim Standort, Name, subnetmask As String Dim nwbits As Integer Dim nwad As String = IP2ShortNW(ip) NetzwerkadresseInfo(nwad, Standort, Name, subnetmask) mask2nwbit(subnetmask, nwbits) Dim Hostbits As Integer = 32 - nwbits Dim AnzahlHosts As Integer = (2 ^ Hostbits) - 2 starthost = IP2Host(nwad) + 1 endhost = starthost + (AnzahlHosts - 1) End Function Public Shared Function Netzwerkfuellen(NWAdresse As String, NWBits As Integer, Netname As String, NWStandort As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = DBConString cmd.Connection = con Dim AnzahlHosts As Integer Dim Hostbits As Integer = 32 - NWBits AnzahlHosts = (2 ^ Hostbits) - 2 Dim Starthost As Integer = IP2Host(NWAdresse) + 1 Dim IPShort As String NWAddressGenerieren(NWAdresse, IPShort) ' Dim NWStandort, NetName As String 'ip2netname(NWAdresse, NWStandort, NetName) For Host As Integer = Starthost To (Starthost + AnzahlHosts - 1) Dim IP As String = IPShort & Host con.Open() cmd.CommandText = "Select FQDN, Modell FROM Tbl_Netzwerkclients WHERE IPAdresse = '" & IP & "' " reader = cmd.ExecuteReader() Try reader.Read() reader.IsDBNull(0) 'Label2.Text &= "Try" & IP & vbCrLf Catch 'MsgBox("Catch") con.Close() con.Open() cmd.CommandText = "INSERT INTO Tbl_Netzwerkclients (IPAdresse, HOST, Netname, NWStandort, Linked, Netzwerk, LinkedWith) VALUES ('" & IP & "', '" & Host & "','" & Netname & "','" & NWStandort & "', '0', '" & NWAdresse & "', '')" cmd.ExecuteNonQuery() con.Close() 'Label2.Text &= "Catch" & cmd.CommandText & IP & vbCrLf End Try con.Close() con.Open() cmd.CommandText = "UPDATE Tbl_Netzwerkclients SET NETZWERK='" & NWAdresse & "' WHERE NETNAME = '" & Netname & "' AND NWStandort = '" & NWStandort & "'" cmd.ExecuteNonQuery() con.Close() Next End Function Public Shared Function NetzwerkadresseInfo(Netzwerkadresse As String, ByRef Standort As String, ByRef Name As String, ByRef subnetmask As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT * FROM Tbl_Netzwerke WHERE NETZWERK = '" & Netzwerkadresse & "'" reader = cmd.ExecuteReader() Do While reader.Read Standort = reader("NWSTANDORT") Name = reader("NETNAME") subnetmask = reader("Subnetz") Loop reader.Close() con.Close() End Function Public Shared Function getDHCPstartend(Netzwerkadresse As String, ByRef DHCPStart As String, ByRef DHCPEnd As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = DBConString cmd.Connection = con Try con.Open() cmd.CommandText = "SELECT * FROM Tbl_Netzwerke WHERE NETZWERK = '" & Netzwerkadresse & "'" reader = cmd.ExecuteReader() Do While reader.Read DHCPStart = reader("DHCPStart") DHCPEnd = reader("DHCPEnd") 'subnetmask = reader("Subnetz") Loop reader.Close() con.Close() Catch DHCPStart = 0 DHCPEnd = 0 End Try End Function Public Shared Function DHCPFill(dhcpstart As String, dhcpend As String, nw As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = DBConString cmd.Connection = con Try dhcpstart = Convert.ToInt32(dhcpstart) dhcpend = Convert.ToInt32(dhcpend) Catch ex As Exception dhcpstart = 0 dhcpend = 0 'LblInfo.Text = "DHCP nicht anwendbar" 'Exit Function End Try If dhcpstart < 1 Or dhcpend > 254 Then 'MsgBox("der DHCP Bereich kann nicht stimmen...") 'Exit Function End If con.Open() cmd.CommandText = "select IPadresse, DHCP from TbL_Netzwerkclients Where NETNAME = (select NETNAME from TbL_Netzwerke Where Netzwerk = '" & nw & "') AND NWSTANDORT = (select NWSTANDORT from TbL_Netzwerke Where Netzwerk = '" & nw & "') ORDER BY HOST" reader = cmd.ExecuteReader() Do While reader.Read() Dim IPAdresse As String = reader("IPAdresse") Dim Host As Integer = Class1.IP2Host(IPAdresse) If Host = dhcpstart Then DHCPUpdate("Start", IPAdresse) ElseIf Host = dhcpend Then DHCPUpdate("Ende", IPAdresse) ElseIf Host > dhcpstart And Host < dhcpend Then DHCPUpdate("1", IPAdresse) Else DHCPUpdate("0", IPAdresse) End If Loop reader.Close() con.Close() ' LblInfo.Text = "Gespeichert." ' Timer3.Enabled = True End Function Public Shared Function DHCPUpdate(Wert As String, IPAdresse As String) Dim dhcpcon As New SqlConnection Dim dhcpcmd As New SqlCommand dhcpcon.ConnectionString = Class1.DBConString dhcpcmd.Connection = dhcpcon dhcpcon.Open() dhcpcmd.CommandText = "UPDATE TbL_Netzwerkclients SET DHCP = '" & Wert & "' WHERE IPAdresse = '" & IPAdresse & "'" dhcpcmd.ExecuteNonQuery() dhcpcon.Close() End Function Public Shared Function RefillNW(netzwerk As String) Dim NWStandort, NWName, Subnet, dhcpstart, dhcpend As String Dim NWBits As Integer Class1.NetzwerkadresseInfo(netzwerk, NWStandort, NWName, Subnet) Class1.mask2nwbit(Subnet, NWBits) Class1.Netzwerkfuellen(netzwerk, NWBits, NWName, NWStandort) Class1.getDHCPstartend(netzwerk, dhcpstart, dhcpend) Class1.DHCPFill(dhcpstart, dhcpend, netzwerk) End Function Public Shared Function GetFQDNandType(IPAdresse As String, ByRef FQDN As String, ByRef Type As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT * FROM TbL_Netzwerkclients WHERE IPAdresse = '" & IPAdresse & "'" reader = cmd.ExecuteReader() Do While reader.Read FQDN = ReadNullAsEmptyString(reader, "FQDN") Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() End Function Public Shared Function ReadNullAsEmptyString(ByVal reader As IDataReader, ByVal Abfrage As String) If IsDBNull(reader(Abfrage)) Then Return "" Else Return reader(Abfrage) End If End Function Public Shared Function ReadNullAs0(ByVal reader As IDataReader, ByVal Abfrage As String) If IsDBNull(reader(Abfrage)) Then Return "0" Else Return reader(Abfrage) End If End Function Public Shared Function Host2IP(ByVal Host As String, ByVal nwstandort As String, ByVal netname As String, ByRef IP As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT * FROM TbL_Netzwerkclients WHERE HOST = '" & Host & "' AND NWStandort = '" & nwstandort & "' AND NETNAME = '" & netname & "'" reader = cmd.ExecuteReader() Do While reader.Read IP = ReadNullAsEmptyString(reader, "IPAdresse") 'Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() End Function Public Shared Function Name2Netzwerk(ByVal nwstandort As String, ByVal netname As String, ByRef Netzwerk As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT Netzwerk FROM TbL_Netzwerke WHERE NWStandort = '" & nwstandort & "' AND NETNAME = '" & netname & "'" reader = cmd.ExecuteReader() Do While reader.Read Netzwerk = ReadNullAsEmptyString(reader, "Netzwerk") 'Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() End Function Public Shared Function IP2ShortNW(ByVal IPAdresse As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con Dim Netzwerk As String con.Open() cmd.CommandText = "SELECT Netzwerk FROM TbL_Netzwerkclients WHERE IPAdresse = '" & IPAdresse & "' " reader = cmd.ExecuteReader() Do While reader.Read Netzwerk = ReadNullAsEmptyString(reader, "Netzwerk") 'Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() Return Netzwerk End Function Public Shared Function GetDHCPServer(ByVal Netzwerkadresse As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con Dim DHCPServer As String con.Open() cmd.CommandText = "SELECT DHCPServer FROM TbL_Netzwerke WHERE Netzwerk = '" & Netzwerkadresse & "' " reader = cmd.ExecuteReader() Do While reader.Read DHCPServer = ReadNullAsEmptyString(reader, "DHCPServer") 'Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() Return DHCPServer End Function Public Shared Function Autocomplete(ByRef txtbox As TextBox, spalte As String, Tabelle As String) Dim tmp As New AutoCompleteStringCollection() Dim con As New SqlConnection Dim cmd As New SqlCommand con.ConnectionString = Class1.DBConString cmd.Connection = con Try con.Open() Dim ds As New DataSet() Dim dt As New DataTable() Dim dataadapter As New SqlDataAdapter("SELECT Distinct " & spalte & " from " & Tabelle & "", con) dataadapter.Fill(ds) ' txtbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend ' txtbox.AutoCompleteSource = AutoCompleteSource.CustomSource txtbox.AutoCompleteCustomSource = tmp For Each dr As DataRow In ds.Tables(0).Rows Try tmp.Add(dr.Item(0)) Catch End Try 'Items.Add(dr.Item(0)) Next con.Close() Catch ex As Exception MsgBox("Fehler Autocomplete: " & ex.Message) con.Close() End Try con.Close() End Function Public Shared Function GetUserPasswort(ip As String, ByRef User As String, ByRef Password As String) Dim tmp As New AutoCompleteStringCollection() Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con con.Open() cmd.CommandText = "SELECT Benutzer, Passwort FROM TbL_Netzwerkclients WHERE IPAdresse = '" & ip & "' " reader = cmd.ExecuteReader() Do While reader.Read User = ReadNullAsEmptyString(reader, "Benutzer") Password = ReadNullAsEmptyString(reader, "Passwort") Loop con.Close() End Function Public Shared Function MainHost(ip As String) Dim tmp As New AutoCompleteStringCollection() Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con Dim LinkedWith As String con.Open() cmd.CommandText = "SELECT LinkedWith FROM TbL_Netzwerkclients WHERE IPAdresse = '" & ip & "' " reader = cmd.ExecuteReader() Do While reader.Read LinkedWith = ReadNullAsEmptyString(reader, "LinkedWith") Loop con.Close() If LinkedWith = "" Then Return ip Else Return LinkedWith End If End Function Public Shared Function IsLinked(ByVal IPAdresse As String) Dim con As New SqlConnection Dim cmd As New SqlCommand Dim reader As SqlDataReader con.ConnectionString = Class1.DBConString cmd.Connection = con Dim Linked As String con.Open() cmd.CommandText = "SELECT Linked FROM TbL_Netzwerkclients WHERE IPAdresse = '" & IPAdresse & "' " reader = cmd.ExecuteReader() Do While reader.Read Linked = ReadNullAsEmptyString(reader, "Linked") 'Type = ReadNullAsEmptyString(reader, "Type") Loop con.Close() Return Linked End Function Public Shared Function DBNull2emptyString(totest As Object) If IsDBNull(totest) Then Return "" Else Return totest End If End Function Public Shared Function OrderDataTableby(dt As DataTable, orderby As String) Dim dv As New DataView(dt) dv.Sort = orderby Dim returnTable As DataTable = dv.ToTable 'returnTable = dv.Table Return returnTable End Function End Class