Check if a record exists in the database

前端 未结 13 1702
借酒劲吻你
借酒劲吻你 2020-11-28 12:58

I am using these lines of code to check if the record exists or not.

SqlCommand check_User_Name = new SqlCommand(\"SELECT * FROM Table WHERE ([user] = \'\" +         


        
13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 13:36

    I was asking myself the same question, and I found no clear answers, so I created a simple test.

    I tried to add 100 rows with duplicate primary keys and measured the time needed to process it. I am using SQL Server 2014 Developer and Entity Framework 6.1.3 with a custom repository.

    Dim newE As New Employee With {.Name = "e"}
    For index = 1 To 100
      Dim e = employees.Select(Function(item) item.Name = "e").FirstOrDefault()
      If e Is Nothing Then
        employees.Insert(newE)
      End If
    Next  
    

    2.1 seconds

    Dim newE As New Employee With {.Name = "e"}
    For index = 1 To 100
      Try
        employees.Insert(newE)
      Catch ex As Exception
      End Try
    Next  
    

    3.1 seconds

提交回复
热议问题