<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Update SQL databace from CSV file</Title>
      <Shortcut>UpdateSQLdatabacefromCSVfile</Shortcut>
      <Description>Update SQL databace from CSV file [VB.NET]</Description>
      <Author>Mischa Princ</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=9bd89501-1b8d-4771-8190-0cffc02fec79</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[Imports System.IO
Imports System.Text
    'Created : Mischa (mischa@piketa.cz)
Module cvs
    'CVS structure : 
    'Uniq ID;col1;col2;....
    '123;bla;bla bla

    Dim conn_path = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path\to\some\access_db.mdb"
    Dim path As String = "export.csv"
    Function hledej(ByVal retez As String, ByVal pole() As String)
        Dim zpet As String = ""
        For Each Str As String In pole
            If Str.Contains(retez) Then
                zpet = Str
            End If
        Next
        Return zpet
    End Function

    Sub Main()
        Using connect As New System.Data.OleDb.OleDbConnection(conn_path)
            connect.Open()

            Dim readText() As String = File.ReadAllLines(path)
            Dim radek As String
            Dim promenne() As String
            promenne = readText(0).Split(";")
            Dim a As Integer = 0
            For Each radek In readText
                If a > 0 Then
                    Dim prikaz As String
                    Dim pole() As String
                    pole = radek.Split(";")
                    Dim c As Integer = 1
                    Dim max As Integer = promenne.Length
                    While c < max
                        If pole(c) <> "" And promenne(c).ToString <> "" Then

                            prikaz = "UPDATE table SET " & "`" & RTrim(LTrim(promenne(c))) & "`='" & pole(c) & "'" & " WHERE `" & RTrim(LTrim(promenne(0))) & "`='" & pole(0) & "'"

                            Dim update As New System.Data.OleDb.OleDbCommand(prikaz, connect)
                            Try
                                update.ExecuteNonQuery()
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.Write("-")
                                Console.ForegroundColor = ConsoleColor.Gray
                            Catch ex As Exception
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.Write("*")
                                Console.ForegroundColor = ConsoleColor.Gray
                            End Try
                        End If
                        c += 1
                    End While

                End If
                a += 1
            Next
            Console.Read()

        End Using
    End Sub
End Module]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>