Hallo,

I'm creating a Wizard in VB.NET. In this wizard I establish the
connection to MySQL and then if it's successful then it creates (if
not exists) a database, tables, amoung others.

The question is, how do I insert data to a table only if it's empty.
Logically if it's not empty, then it shouldn't insert anything. I want
to add this in VB.NET code but don't know how to achive this.

To get the total rows I should use this: SELECT count(*) from db.table
t;

But as I had said, I don't know how to make the wizard know if the
table is empty or not.

Hope somebody can help me on this or show me the way.

Thanks in advance.

Regards,
iDesmet

Re: Insert data only if Table is empty in MySQL by Paul

Paul
Wed Jul 23 14:14:41 CDT 2008

On Tue, 22 Jul 2008 18:46:02 -0700 (PDT), iDesmet <idesmet.work@gmail.com> wrote:

¤ Hallo,
¤
¤ I'm creating a Wizard in VB.NET. In this wizard I establish the
¤ connection to MySQL and then if it's successful then it creates (if
¤ not exists) a database, tables, amoung others.
¤
¤ The question is, how do I insert data to a table only if it's empty.
¤ Logically if it's not empty, then it shouldn't insert anything. I want
¤ to add this in VB.NET code but don't know how to achive this.
¤
¤ To get the total rows I should use this: SELECT count(*) from db.table
¤ t;
¤
¤ But as I had said, I don't know how to make the wizard know if the
¤ table is empty or not.
¤
¤ Hope somebody can help me on this or show me the way.

Try using the ExecuteScalar method to query the count:

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx


Paul
~~~~
Microsoft MVP (Visual Basic)

Re: Insert data only if Table is empty in MySQL by iDesmet

iDesmet
Thu Jul 24 03:21:54 CDT 2008

On Jul 23, 2:14=A0pm, Paul Clement
<UseAdddressAtEndofMess...@swspectrum.com> wrote:
> On Tue, 22 Jul 2008 18:46:02 -0700 (PDT), iDesmet <idesmet.w...@gmail.com=
> wrote:
>
> =A4 Hallo,
> =A4
> =A4 I'm creating a Wizard in VB.NET. In this wizard I establish the
> =A4 connection to MySQL and then if it's successful then it creates (if
> =A4 not exists) a database, tables, amoung others.
> =A4
> =A4 The question is, how do I insert data to a table only if it's empty.
> =A4 Logically if it's not empty, then it shouldn't insert anything. I wan=
t
> =A4 to add this in VB.NET code but don't know how to achive this.
> =A4
> =A4 To get the total rows I should use this: SELECT count(*) from db.tabl=
e
> =A4 t;
> =A4
> =A4 But as I had said, I don't know how to make the wizard know if the
> =A4 table is empty or not.
> =A4
> =A4 Hope somebody can help me on this or show me the way.
>
> Try using the ExecuteScalar method to query the count:
>
> http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcomman...
>
> Paul
> ~~~~
> Microsoft MVP (Visual Basic)

Thanks, I figured it out how to do it.