title: Update if exits or Insert if not
body: <p>I am coding an application which will insert if the primary key isn't found or update if it is found. 
  Its the same type of situation that is asked <a href="https://stackoverflow.com/questions/108403/solutions-for-insert-or-update-on-sql-server">here</a> on this site.</p>
  
  <p>Question.</p>
  
  <p>Right now i am using the following  code for a insert. (Names have been changed but same format)</p>
  
  <pre><code> Using con As New SqlConnection
              Using cmd As New SqlCommand
                  Try
                      con.ConnectionString = "removed"
                      con.Open()
                      cmd.Connection = con
                      cmd.CommandText = "INSERT INTO [table] ([primary key],[value]) VALUES (@primary key, @value)"
                      cmd.Parameters.AddWithValue("@primary key", code to grab key)
                      cmd.Parameters.AddWithValue("@value", code for value)
                      cmd.ExecuteNonQuery()
                      con.Close()
                  Catch ex As Exception
                      MessageBox.Show("Error while inserting record on table..." &amp; ex.Message, "Insert Records")
                  End Try
              End Using
          End Using
  </code></pre>
  
  <p>The complete code above works for me. 
  But now i need it to only do inserts of the primary key isn't found. </p>
  
  <p>Based on the above link, is the answer to do something like this </p>
  
  <pre><code>  cmd.CommandText = "Merge Table as target using (values ('key') as source(key) on taget.idfield = code for key...
  </code></pre>
  
  <p>This doesn't feel right to me, (possibly because its all on one line). Whats the best way to accomplish this task?</p>