SQL Delete Command??

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi

I need to run a delete query from my application... is there a quick way of
doing this other than setting up an adapter and running the stored
procedure?? etc...

something like the VB6 database.Execute(SQLStatement) would be perfect... if
not i'll just do i the normal way.

Thanks


Regards
 
Hi Darryn,

Yes sure u can excete delete query direct using command object.

'1. Create a connection
Dim myConnection as New SqlConnection("connectionString")

'2. Create the command object, passing in the SQL string
Const strSQL as String = "Delete ..."
Dim myCommand as New SqlCommand(strSQL, myConnection)

myConnection.Open()
myCommand.ExecuteScalar(CommandBehavior.CloseConnection)
 
Darryn Ross said:
Hi

I need to run a delete query from my application... is there a quick way of
doing this other than setting up an adapter and running the stored
procedure?? etc...

something like the VB6 database.Execute(SQLStatement) would be perfect... if
not i'll just do i the normal way.

Thanks


Regards

You need to set up a connection to your database, which here is referenced
by oleDbConnection1, this code is for an OLEDB connection but this can
easily be replaced by the SQL version.


string strDelete = "DELETE FROM [yourDatabase] WHERE [yourField] = '" +
yourArgument + "'";
OleDbCommand cmd = new OleDbCommand(strDelete, oleDbConnection1);
try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
oleDbConnection1.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top