We help IT Professionals succeed at work.
Get Started

VB.net MySQL to insert record into table if it doesn't exist

Last Modified: 2012-05-10
I am trying to execute the following SQL statement from a VB.net program. I am wanting to insert the record from the tmp table if it doesnt already exist in the main trade table. I can execute this SQL statement fine as an database query, but when I try and execute if from VB.net it doesnt work properly and I get duplicate entries in the master table. Is there a reason this code doesnt work properly in VB.net? Any alternative solutions?

ExecuteSQL("INSERT Into Trades (TradeDate,Account,OrderId,ClientId,PermId,ExecId,Type,Symbol,Contract,Multiplier,Side,FilledQuantity,AvgPrice) SELECT TradeDate,Account,OrderId,ClientId,PermId,ExecId,Type,Symbol,Contract,Multiplier,Side,FilledQuantity,AvgPrice FROM tmp_Trades WHERE NOT EXISTS (SELECT * FROM Trades WHERE tmp_trades.ExecID=trades.ExecID)")

Open in new window

Public Function ExecuteSQL(ByVal sql As String)
        Dim connStr As String = "xxx"
        Dim connection As New MySqlConnection(connStr)
        Dim cmd As New MySqlCommand(sql, connection)


        Dim reader As Integer
        reader = cmd.ExecuteNonQuery


    End Function

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE