Hallo zusammen,
ich habe ein kleines Programm, mit dem ich eine EXCEL Datei in ein DataGidView rein lade und übertrage das an einen SQL Server. (einmalig)
Jetzt möchte ich diese Daten mit anderen Daten vergleichen und farblich hervorheben bei Änderung im Datagridview, könnt Ihr mir einen Tipp geben wie ich hier am besten vorgehe kann?
Danke für eure Unterstützung vorab.
Anbei mein aktueller Code, bin noch am testen:
Ps. Für Verbesserungsvorschläge wegen meinen Codes, würde ich mich auch freuen. Nur so kann lernen.
ich habe ein kleines Programm, mit dem ich eine EXCEL Datei in ein DataGidView rein lade und übertrage das an einen SQL Server. (einmalig)
Jetzt möchte ich diese Daten mit anderen Daten vergleichen und farblich hervorheben bei Änderung im Datagridview, könnt Ihr mir einen Tipp geben wie ich hier am besten vorgehe kann?
Danke für eure Unterstützung vorab.
Anbei mein aktueller Code, bin noch am testen:
Ps. Für Verbesserungsvorschläge wegen meinen Codes, würde ich mich auch freuen. Nur so kann lernen.
VB.NET-Quellcode
- Option Strict Off
- Imports System.Data.OleDb
- Imports System.IO
- Imports System.Data.SqlClient
- Public Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Dim conn As OleDbConnection
- Dim dtr As OleDbDataReader
- Dim dta As OleDbDataAdapter
- Dim cmd As OleDbCommand
- Dim dts As DataSet
- Dim excel As String
- Dim OpenFileDialog As New OpenFileDialog
- OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
- OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
- If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
- Dim fi As New FileInfo(OpenFileDialog.FileName)
- Dim FileName As String = OpenFileDialog.FileName
- excel = fi.FullName
- conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
- dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn)
- dts = New DataSet
- dta.Fill(dts, "[Sheet1$]")
- DataGridView1.DataSource = dts
- DataGridView1.DataMember = "[Sheet1$]"
- conn.Close()
- End If
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim sqlc As String = "insert into tbl_650 ([Werk],[Auftragsnr.],[Liefernummer],[WarenEm],[Name],[Straßenname],[PLZ Warenempf.],[Ort],[BestNr],[Versandbedingung],[Spalte K],[Liefersperre],[Wunschlieferdatum],[FA Paletten],[Spalte O],[Bruttoauftragsgew.],[Gew-ME]) values(@f1, @f2, @f3, @f4, @f5, @f6, @f7, @f8, @f9, @f10, @f11, @f12, @f13, @f14, @f15, @f16, @f17)"
- Dim dp As New SqlDataAdapter
- Dim p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17 As SqlParameter
- Dim ConString As String = "Data Source=SERVER1;Initial Catalog=Shared;Integrated Security=True;timeout=5"
- Using conn As New SqlConnection(ConString)
- Try
- conn.Open()
- dp.InsertCommand = New SqlCommand(sqlc, conn)
- p1 = dp.InsertCommand.Parameters.Add("@f1", SqlDbType.NVarChar)
- p2 = dp.InsertCommand.Parameters.Add("@f2", SqlDbType.Decimal)
- p3 = dp.InsertCommand.Parameters.Add("@f3", SqlDbType.Decimal)
- p4 = dp.InsertCommand.Parameters.Add("@f4", SqlDbType.Decimal)
- p5 = dp.InsertCommand.Parameters.Add("@f5", SqlDbType.NVarChar)
- p6 = dp.InsertCommand.Parameters.Add("@f6", SqlDbType.NVarChar)
- p7 = dp.InsertCommand.Parameters.Add("@f7", SqlDbType.NVarChar)
- p8 = dp.InsertCommand.Parameters.Add("@f8", SqlDbType.NVarChar)
- p9 = dp.InsertCommand.Parameters.Add("@f9", SqlDbType.NVarChar)
- p10 = dp.InsertCommand.Parameters.Add("@f10", SqlDbType.NVarChar)
- p11 = dp.InsertCommand.Parameters.Add("@f11", SqlDbType.NVarChar)
- p12 = dp.InsertCommand.Parameters.Add("@f12", SqlDbType.NVarChar)
- p13 = dp.InsertCommand.Parameters.Add("@f13", SqlDbType.DateTime)
- p14 = dp.InsertCommand.Parameters.Add("@f14", SqlDbType.Decimal)
- p15 = dp.InsertCommand.Parameters.Add("@f15", SqlDbType.Decimal)
- p16 = dp.InsertCommand.Parameters.Add("@f16", SqlDbType.Decimal)
- p17 = dp.InsertCommand.Parameters.Add("@f17", SqlDbType.NVarChar)
- With DataGridView1
- For i As Integer = 0 To .Rows.Count - 1
- With .Rows(i)
- If .Cells(0).Value = Nothing Then
- p1.Value = ""
- Else
- p1.Value = .Cells(0).Value
- End If
- If .Cells(1).Value = Nothing Then
- p2.Value = 0
- Else
- p2.Value = CDec(.Cells(1).Value)
- End If
- If .Cells(2).Value = Nothing Then
- p3.Value = 0
- Else
- p3.Value = CDec(.Cells(2).Value)
- End If
- If .Cells(3).Value = Nothing Then
- p4.Value = 0
- Else
- p4.Value = CDec(.Cells(3).Value)
- End If
- If .Cells(4).Value = Nothing Then
- p5.Value = ""
- Else
- p5.Value = .Cells(4).Value
- End If
- If .Cells(5).Value = Nothing Then
- p6.Value = ""
- Else
- p6.Value = .Cells(5).Value
- End If
- If .Cells(6).Value = Nothing Then
- p7.Value = ""
- Else
- p7.Value = .Cells(6).Value
- End If
- If .Cells(7).Value = Nothing Then
- p8.Value = ""
- Else
- p8.Value = .Cells(7).Value
- End If
- If .Cells(8).Value = Nothing Then
- p9.Value = ""
- Else
- p9.Value = .Cells(8).Value
- End If
- If .Cells(9).Value = Nothing Then
- p10.Value = ""
- Else
- p10.Value = .Cells(9).Value
- End If
- If .Cells(10).Value = Nothing Then
- p11.Value = 0
- Else
- p11.Value = .Cells(10).Value
- End If
- If .Cells(11).Value = Nothing Then
- p12.Value = ""
- Else
- p12.Value = .Cells(11).Value
- End If
- If .Cells(12).Value = Nothing Then
- p13.Value = "01.01.1900"
- Else
- p13.Value = .Cells(12).Value
- End If
- If .Cells(13).Value = Nothing Then
- p14.Value = 0
- Else
- p14.Value = CDec(.Cells(13).Value)
- End If
- If .Cells(14).Value = Nothing Then
- p15.Value = 0
- Else
- p15.Value = CDec(.Cells(14).Value)
- End If
- If .Cells(15).Value = Nothing Then
- p16.Value = 0
- Else
- p16.Value = CDec(.Cells(15).Value)
- End If
- If .Cells(16).Value = Nothing Then
- p17.Value = ""
- Else
- p17.Value = .Cells(16).Value
- End If
- Try
- dp.InsertCommand.ExecuteNonQuery()
- Catch ex As Exception
- MsgBox(ex.ToString)
- End Try
- End With
- Next
- End With
- Catch ex As Exception
- MsgBox(ex.ToString)
- Finally
- conn.Close()
- End Try
- End Using
- End Sub
- End Class